Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I intercept the OnPaste event in a TMemo

Component Writing

How do I intercept the OnPaste event in a TMemo

by  Nordlund  Posted    (Edited  )
First, read the FAQ describing how to create a simple Delphi component. Then, you are ready to procced with this component.

Code:
unit MemoEx;

interface

uses
  ClipBrd, Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  TStringEvent = procedure(Sender: TObject; var s: String) of object;
  TMemoEx = class(TMemo)
  private
    FOnBeforePaste: TStringEvent;
    FOnAfterPaste: TNotifyEvent;
    procedure WMPaste(var Message: TMessage); message WM_PASTE;
  protected
  public
  published
    property OnBeforePaste: TStringEvent read FOnBeforePaste write FOnBeforePaste;
    property OnAfterPaste: TNotifyEvent read FOnAfterPaste write FOnAfterPaste;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Andreas Nordlund', [TMemoEx]);
end;

{ TMemoEx }

procedure TMemoEx.WMPaste(var Message: TMessage);
var
  strClpText: String;
begin
  // Check if the OnBeforePaste event is assigned and check if the clipboard contains plain text.
  if Assigned(FOnBeforePaste) and Clipboard.HasFormat(CF_TEXT) then
  begin
    // Save the clipboard text into a local variable
    strClpText := ClipBoard.AsText;
    // Fire the OnBeforePaste event
    FOnBeforePaste(self, strClpText);

    // Clear the clipboard and replace it with the new text
    Clipboard.Clear;
    Clipboard.AsText := strClpText;
  end;

  // Perform the actual paste
  inherited;
  
  // if the OnAfterPaste event is assigned, fire it.
  if Assigned(FOnAfterPaste) then
    FOnAfterPaste(self);
end;

end.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top