RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TControl.GetTextBuf Method

Retrieves the control's text, copies it into a buffer, and returns the number of characters copied.

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

Call GetTextBuf to retrieve the text of a control into a fixed size buffer. The text copied is the value of the Text property. GetTextBuf returns the number of characters that were actually copied, which is either the length of the Text property, or BufSize - 1, whichever is smaller. 

To find out how many characters the buffer needs to hold the entire text, call the GetTextLen method before allocating a buffer for GetTextBuf.

Note: GetTextBuf is available for compatibility with 16-bit code. Where backward compatibility is not an issue, use the Text property.
Note: To obtain the control's text as an AnsiString, use the Text property instead.
 

C++ Examples: 

 

/*
This example requires two edit boxes and a button.  This example
copies the text in an edit box into a null-terminated string, and
puts this string in another edit box when the user clicks the
button on the form.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int Size = Edit1->GetTextLen(); //Get length of string in Edit1
  Size++;                      //Add room for null character
  char *Buffer = new char[Size];  //Creates Buffer dynamic variable
  Edit1->GetTextBuf(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->Text;
}

 

Delphi Examples: 

{
This example requires two edit boxes and a button.  This example
copies the text in an edit box into a null-terminated string, and
puts this string in another edit box when the user clicks the
button on the form.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: PChar;
  Size: Byte;
begin
  Size := Edit1.GetTextLen;       {Get length of string in Edit1}
  Inc(Size);                      {Add room for null character}
  GetMem(Buffer, Size);           {Creates Buffer dynamic variable}
  Edit1.GetTextBuf(Buffer,Size);  {Puts Edit1.Text into Buffer}
  Edit2.Text := StrPas(Buffer);   {Converts Buffer to a 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.Text;
end;

 

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