RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomEdit.GetSelTextBuf Method

Copies the selected text into a buffer and returns the number of characters copied.

Pascal
function GetSelTextBuf(Buffer: PChar; BufSize: Integer): Integer; virtual;
C++
virtual __fastcall int GetSelTextBuf(const char * Buffer, int BufSize);

Use GetSelTextBuf to copy the selected text into a character buffer. If there is no selection, the buffer receives an empty string. If the selection contains more than (BufSize - 1) characters, only the first (BufSize - 1) characters are copied. GetSelTextBuf returns the number of characters that were actually copied into the buffer.  

C++ Examples: 

 

/*
This example copies selected text from one text edit to
another.  This example requires two text edits and two buttons.
*/
Char *Buffer;

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int Size = Edit1->SelLength; //Get length of selected text in Edit1
  Size++;                      //Add room for null character
  static std::auto_ptr<Char> _BufferCleaner(Buffer = new Char[Size]);
  Edit1->GetSelTextBuf(Buffer, Size); //Puts Edit1->Text into Buffer
  Edit2->Text = 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) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!