GetTextBuf (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example requires two edit boxes and a button. This example copies the text from an edit box into a null-terminated string and puts this string in another edit box when you click the button on the form.

Code

#include <memory>       //For STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int Size = Edit1->GetTextLen(); //Get the length of the 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;
}

Uses