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?
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.
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = ^A then begin
With Sender as TMemo do begin
SelStart := Length(Text);
Perform(EM_SCROLLCARET, 0, 0);
SelectAll;
end;
Key := #0; //Eat the key to suppress the beep
end;
end;
Note that 'Eat the key' only works in the OnKeyPress event, not the OnKeyDown or OnKeyUp events.
This is more elegant:
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = ^A then
begin
(Sender as TMemo).SelectAll;
Key := #0;
end;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With