0 0 Votes

Delphi: Select all with CTRL+A in Memo

Question by Guest | 10/09/2012 at 17:42

I have a TMemo on a form. When I press the key combination CTRL and A in the memo, I expect that everything will be marked. But nothing happens with this shortcut.

How can that be? Would Windows not have to ensure that CTRL-A is working? And how can I achieve it, that these keys are working?

ReplyPositiveNegativeDateVotes

Progger99

Show Profile | Message
Avatar
5Best Answer
5 Votes

A memo field only shows this behavior, if you add the following code in the OnKeyDown event of the memo:

procedure TForm1.Memo1KeyDown(Sender: TObject; 
var Key: Word; Shift: TShiftState);
begin          
  if ((ssCtrl in Shift) and (Key = 65)) then
    TMemo(Sender).SelectAll;  //select content of memo
end;

With this, at every keystroke, it will be looked, whether the user hits the key A while the CTRL key is presed. The 65 is the code of A, which you can also retrieve with ord(A).
10/09/2012 at 22:20

ReplyPositive Negative
11 Vote

Here's yet a better way to handle it...

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^A then begin
    (Sender as TMemo).SelectAll;
    Key := #0;
  end;
end;

10/03/2019 at 16:51

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.