Generic handling for any Edit menu

By: Gudmundson Steven

Abstract: Many of the data-aware controls in Delphi do not respond to Control C, V, X to copy, paste, and cut. The code below shows the quickest and easiest solution to this problem. By Steven C. Gudmundson.

Use a TActionList with the standard edit actions. Optionally you can add an edit menu item tied to this TActionList.

To handle the execute event of each action, you could create a big case statement to handle each type of editable control on your form:

if (ActiveControl is T...) then
  T...(...).CopyToClipboard.
  etc.
  etc.
  etc.

Or, you can simply add the following code which handles any Edit -> Cut, Copy, or Paste command:

procedure TfrmMain.EditCutCmdExecute(Sender: TObject);
begin
  Application.ProcessMessages;
  keybd_event(VK_RSHIFT,0,0,0);
  keybd_event(VK_DELETE,0,0,0);
  keybd_event(VK_DELETE,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_RSHIFT,0,KEYEVENTF_KEYUP,0);
  Application.ProcessMessages;
end;

procedure TfrmMain.EditCopyCmdExecute(Sender: TObject);
begin
  Application.ProcessMessages;
  keybd_event(VK_LCONTROL,0,0,0);
  keybd_event(VK_LSHIFT,0,0,0);
  keybd_event(VK_INSERT,0,0,0);
  keybd_event(VK_INSERT,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_LSHIFT,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_LCONTROL,0,KEYEVENTF_KEYUP,0);
  Application.ProcessMessages;
end;

procedure TfrmMain.EditPasteCmdExecute(Sender: TObject);
begin
  Application.ProcessMessages;
  keybd_event(VK_RSHIFT,0,0,0);
  keybd_event(VK_INSERT,0,0,0);
  keybd_event(VK_INSERT,0,KEYEVENTF_KEYUP,0);
  keybd_event(VK_RSHIFT,0,KEYEVENTF_KEYUP,0);
  Application.ProcessMessages;
end;

Tip provided by Steven C. Gudmundson

Server Response from: SC4