http://delphi.about.com/od/adptips2004/a/bltip0804_4.htm

Here's how to implement the code for the CTRL+A key combination ("Select All") for TMemo or TDBMemo:

~~~~~~~~~~~~~~~~~~~~~~~~~
Just drop a memo (Memo1:TMemo) on a form (Form1:TForm) and handle the OnKeyDown event for Memo1 as:

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
   Shift: TShiftState) ;
begin
   if (Key = Ord('A')) and (ssCtrl in Shift) then
   begin
     TMemo(Sender).SelectAll;
     Key := 0; //这里即使为0,Windows系统还是能继续接到按键的信息,然后就会导致 BEEP 声音(win7是当当的声音),因为这里只是 KeyDown。
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Note: here's how to check the state of CTRL, ALT and SHIFT keys.

 

 

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

 

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?

share|edit
 
3  
Personally I'd sooner create a component derived from the standard memo and handle the key press there so that you don't need to pollute all your forms with special handling code. –  David Heffernan Dec 11 '11 at 19:33
2  
@David: Do you know if a standard Windows edit control in multiline mode disallows the Ctrl+A command, or if there is a problem with the VCL wrapper? (TheTEdit handles Ctrl+A as one would expect.) –  Andreas Rejbrand Dec 11 '11 at 19:35
1  
@Andreas Once I can get to a machine with a compiler I'll try and produce a raw win32 petzold prog and check it out. –  David Heffernan Dec 11 '11 at 19:51
1  
It seems to be an OS issue with no documented explanation. Many posts about it are just based on code this by your own. –  TLama Dec 11 '11 at 19:54
2  
@David: If I am not mistaken, Raymond asks his readers every now and then about things to write about. This would be an interesting topic. –  Andreas Rejbrand Dec 11 '11 at 20:56

1 Answer

up vote 20down voteaccepted

This is more elegant:

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^A then  牛人搞了一个更简单的
  begin
    (Sender as TMemo).SelectAll;
    Key := #0;  这里 为#0后,就没有BEEP声音了,Windows系统接不到按了什么键。
  end;
end;
share|edit
 
4  
I know I'm going on a bit but could you possibly explain the ^A for the uninitiated (e.g. me!) –  David Heffernan Dec 11 '11 at 19:44
9  
The Ctrl+A keystroke is send as a character with ordinal value 1 (Ctrl+B as 2, Ctrl+C as 3, etc.). Basically I think that this is a remnant from older times. These 'characters' are usually written ^A^B, etc., and Delphi supports these. You can see them in ASCII tables, like at Wikipedia. –  Andreas Rejbrand Dec 11 '11 at 19:48
2  
Haven't seen that for years, must be from TP days. –  Tony Hopkinson Dec 11 '11 at 20:01
5  
@Jerry: The result is not exactly the same. Your code will not handle the annoying 'beep' sound! –  Andreas Rejbrand Dec 11 '11 at 21:46 这个结果和你的代码效果,不是完全相同的,你的代码没有处理这个烦人的 BEEP声音。
2  
@Jerry - AFAIK there's no fix to this. This is the default behavior of a windows edit control, you can read about expected behavior of an edit control here. –  Sertac Akyuz Dec 11 '11 at 22:43

Your Answer

 

 

How to add the "Select All (CTRL+A)" functionality to TMemo/TDBMemo

Title: How to add the "Select All (CTRL+A)" functionality to TMemo/TDBMemo

Question: How to add the "Select All (CTRL+A)" functionality to TMemo/TDBMemo.

Answer:
To implement the code for the CTRL+A key combination ("Select All") for TMemo, TDBMemo or any of it's descendants implement the following code on the OnKeyDownEvent:


procedure OnKeyDown(Sender: TObject; var Key: Word;
   Shift: TShiftState) ;
begin
   if (Key = Ord('A')) and (ssCtrl in Shift) then
   begin
     TMemo(Sender).SelectAll;
     Key := 0;
   end;
end;


from: http://delphi.cjcsoft.net//viewthread.php?tid=45347
 
 
=========================================
if ssCtrl in shift then
  begin
    if (Key = Ord('c')) or (Key = Ord('C')) then
      ShowMessage('Ctrl+C');

  end;

先判断是否按下ctrl才是正确的次序。
 posted on 2015-06-10 19:32  宝兰  阅读(279)  评论(0编辑  收藏  举报