4

why it is not possible to copy selected text in TDBMemo component into clipboard? DELPHI 7, Windows Vista. Following code fails to catch ctrl+c event, whereas ctrl+a works fine.

uses clipbrd;

    procedure THierarchierForm.dbm1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin

    if (Key=Ord('A')) and (ssCtrl IN Shift) then begin
    dbm1.SelectAll;
    Key:=0;
    end;

    if (Key=Ord('C')) and (ssCtrl IN Shift) then begin
    Clipboard.AsText:=dbm1.SelText;
    Key:=0;
    end;

    end;

Thanx

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
lyborko
  • 2,571
  • 3
  • 26
  • 54
  • 2
    I don't know the answer to your question, but you are doing it all wrong. If you want to handle shortcuts you should use a shortcut handler rather than KeyDown. You would copy to the clipboard with CTRL+SHIFT+ALT+C and any combination including CTRL. If you want to copy a memo to the clipboard then I believe you should call `dbm1.CopyToClipboard`. – David Heffernan Feb 07 '11 at 00:02
  • I know it is not correct, but it troubles me a lot, that I can not to catch it. I do not want to copy whole memo, but only selected part. What is "shortcut handler"? – lyborko Feb 07 '11 at 00:07
  • `CopyToClipboard` does what you want. I always try to handle these events with the Shortcut property (e.g. on a menu or an action). Just writing `Shift=[ssCtrl]` would be better, but best of all would be `Shift*[ssShift,ssAlt,ssCtrl]=[ssCtrl]` but that is getting a little pedantic. Anyway, if you do it that way, move it out to a helper function. – David Heffernan Feb 07 '11 at 00:13
  • 1
    I don't know much about `TDBMemo`, but the ordinary `TMemo` handles Ctrl+C (X, V, Z) intrinsically. Anyhow, @David is right about his comments. – Andreas Rejbrand Feb 07 '11 at 00:24

1 Answers1

3

The code you present works in the context of a plain vanilla form. There must be something else interfering.

The most obvious is that your form has KeyPreview set True and so your form handles CTRL+C.

Note that I stand by my reservations expressed in the comment to your question.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanx. But keypreview is false... I am considering the interference should occure, 'cause CTRL+X, CTRL+V is working well. I have one TApplicationEvents object, where I catch some events routing to TDBmemo in OnMessage eventhandler, but after removing this handler I saw no difference. – lyborko Feb 07 '11 at 07:00
  • I'd check where the messages are going using Spy or some such tool. Or you could probably do it in the Delphi debugger with suitable conditional breakpoints. – David Heffernan Feb 07 '11 at 09:15