Search code examples
delphi-xe2delphi-7delphi-2010delphi

Pasting multiple lines into a TEdit


With respect to a TEdit component, would it be possible for the component to handle a multi-line paste from the Windows Clipboard by converting line breaks to spaces?

In other words, if the following data was on the Windows Clipboard:

Hello
world
!

...and the user placed their cursor in a TEdit then pressed CTRL+V, would it be possible to have the TEdit display the input as:

Hello world !


Solution

  • You'd need to subclass the TEdit using an interposer class, and add a handler for the WM_PASTE message:

    unit Unit3;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, adsdata, adsfunc, adstable;
    
    type
      TEdit= class(StdCtrls.TEdit)
        procedure WMPaste(var Msg: TWMPaste); message WM_PASTE;
      end;
    
    type
      TForm3 = class(TForm)
        AdsTable1: TAdsTable;
        Edit1: TEdit;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form3: TForm3;
    
    implementation
    
    {$R *.dfm}
    
    uses
      Clipbrd;
    
    { TEdit }
    
    procedure TEdit.WMPaste(var Msg: TWMPaste);
    var
      TempTxt: string;
    begin
      TempTxt := Clipboard.AsText;
      TempTxt := StringReplace(TempTxt, #13#10, #32, [rfReplaceAll]);
      Text := TempTxt;
    end;
    
    end.