Pasting multiple lines into a TEdit
Asked Answered
W

1

7

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 !

Womanish answered 21/6, 2013 at 3:13 Comment(0)
B
13

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.
Bar answered 21/6, 2013 at 3:22 Comment(1)
And if you have one specific case and you feel subclassing is overkill, you can assign a new message handler to YourEdit.WindowProc.Marguritemargy

© 2022 - 2024 — McMap. All rights reserved.