RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomEdit.SelText Property

Specifies the selected portion of the edit control's text.

Pascal
property SelText: string;
C++
__property AnsiString SelText;

Read SelText to determine the value of the selected text. Set SelText to replace the selected text with a new string. If there is no selection, but the edit control has focus, set SelText to insert a new string into the text at the cursor.  

C++ Examples: 

 

/*
This example copies selected text from one text edit to
another.  This example requires two text edits and two buttons.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int Size = Edit1->SelLength; //Get length of selected text in Edit1
  Size++;                      //Add room for null character
  char *Buffer = new char[Size];  //Creates Buffer dynamic variable
  Edit1->GetSelTextBuf(Buffer,Size); //Puts Edit1->Text into Buffer
  Edit2->Text = Buffer;
  delete Buffer;
}
//---------------------------------------------------------------------------
/*
Note: the same thing could be accomplished more simply as follows:
*/
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Edit2->Text = Edit1->SelText;
}

 

Delphi Examples: 

{
This example copies selected text from one text edit to
another.  This example requires two text edits and two buttons.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: PChar;
  Size: Integer;
begin
  Size := Edit1.SelLength; {Get length of 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 Pascal-style string}
  FreeMem(Buffer, Size);{Frees memory allocated to Buffer}
end;

{
Note: the same thing could be accomplished more simply as follows:
} 
procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit2.Text := Edit1.SelText;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!