Automatically allowing Ctrl+A to select all in a TMemo?

半腔热情 提交于 2019-12-20 19:05:13

问题


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?


回答1:


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;



回答2:


I used the previous answer and discussion to create a standalone component which handles the KeyPress event which I use in small test programs.

TSelectMemo = class(TMemo)
protected
  procedure KeyPress(var Key: Char); override;
end;

...

procedure TSelectMemo.KeyPress(var Key: Char);
begin
  inherited;
  if Key = ^A then
    SelectAll;
end;

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.



来源:https://stackoverflow.com/questions/8466747/automatically-allowing-ctrla-to-select-all-in-a-tmemo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!