SelText (Delphi)

From RAD Studio Code Examples

Description

This example copies selected text from one text edit to another. This example requires two text edits and two buttons.

Code

procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: PChar;
  Size: Integer;
begin
  Size := Edit1.SelLength; { Get the length of the selected text in Edit1. }
  Inc(Size); { Add room for null character. }
  GetMem(Buffer, Size); { Creates Buffer dynamic variable. }
  Edit1.GetSelTextBuf(Buffer,Size); { Puts Edit1.Text into Buffer. }
  Edit2.Text := StrPas(Buffer);{ Converts Buffer into a Pascal-style string. }
  FreeMem(Buffer, Size);{Frees the memory allocated to Buffer. }
end;

{
Note: The same effect can be obtained, more simply, as follows:
procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit2.Text := Edit1.SelText;
end;

Uses