自动允许 Ctrl+A 选择 TMemo 中的所有内容?
在 Delphi 7 的 TMemo 控件中,尝试使用组合键 Ctrl + A
来选择全部不会执行任何操作(不会选择全部)。所以我做了这个程序:
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
C: String;
begin
if ssCtrl in Shift then begin
C:= LowerCase(Char(Key));
if C = 'a' then begin
Memo1.SelectAll;
end;
end;
end;
有什么技巧可以让我不必执行这个程序吗?如果没有,那么这个过程看起来还可以吗?
In Delphi 7's TMemo control, an attempt to do the key combo Ctrl + A
to select all does not do anything (doesn't select all). So I've made this procedure:
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
C: String;
begin
if ssCtrl in Shift then begin
C:= LowerCase(Char(Key));
if C = 'a' then begin
Memo1.SelectAll;
end;
end;
end;
Is there a trick so that I don't have to do this procedure? And if not, then does this procedure look OK?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是更优雅的:
This is more elegant:
虽然 Andreas Rejbrand 接受的答案是正确的,但这不是预期的 Windows 视觉行为。它使光标位置保持不变。 Ctrl-A(全选)应将光标保留在文本底部并滚动控件,以便光标位于视图中。
如果不这样做,控件就会表现出奇怪的行为。例如,假设文本超出了窗口的容纳范围,并且窗口没有滚动到底部。按 Ctrl-A,所有文本都会被选中。 Ctrl-C 现在会将所有文本复制到剪贴板。尽管您看不到它,但光标现在位于视图的底部,该视图尚未滚动。如果您现在按下 Ctrl-Down,所选文本将变为视图中的文本,然后光标向下移动,窗口向下滚动一行。新的底线未被选择。这使得“全选”看起来只选择了可见文本。
解决方法是将插入符号移至 SelectAll 之前的文本末尾。
请注意,“吃掉钥匙”仅适用于 OnKeyPress 事件,不适用于 OnKeyDown 或 OnKeyUp 事件。
While the accepted answer by Andreas Rejbrand is correct, it is not the expected Windows visual behavior. It leaves the cursor position unchanged. Ctrl-A (Select All) should leave the cursor at the bottom of the text and scroll the control so the cursor is in view.
If this is not done, the control exhibits odd behavior. For example, assume there is more text than fits the window, and the window is not scrolled to the bottom. You press Ctrl-A, and all text is Selected. Ctrl-C will now copy all text to the clipboard. Although you can't see it the cursor is now at the bottom of the View, which has not scrolled. If you now press Ctrl-Down the Selected Text becomes just the text in view, then the cursor moves down and window scrolls down one line. The new bottom line is not selected. This makes it look like the Select All only selected the visible text.
The fix is simply to move the caret to the end of text before SelectAll.
Note that 'Eat the key' only works in the OnKeyPress event, not the OnKeyDown or OnKeyUp events.
我使用前面的答案和讨论来创建一个独立的组件,用于处理我在小型测试程序中使用的 KeyPress 事件。
...
向表单上的所有组件添加“全选”行为的另一种方法是使用标准全选操作将操作列表添加到表单。
I used the previous answer and discussion to create a standalone component which handles the KeyPress event which I use in small test programs.
...
Another way of adding "select all" behavior to all components on a form is to add an action list to your form with a standard select all action.