OnSetText (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example illustrates the OnSetText and OnGetText events for TField. This project requires a DBGrid, a DataSource, and a ClientDataset. Connect the DBGrid to the DataSource and the DataSource to the DataSet. Assign a FileName for the DataSet component (an .XML database, for instance). Right-click the DataSet component and select Fields Editor. We assume that the database contains a field named fFirst, which holds people's first names. Select the fFirst field and then select the Events tab in the Object Inspector. There are the OnSetText and OnGetText events.

Code

void __fastcall TMain::ClientDataSet1fFirstGetText(TField *Sender, UnicodeString &Text,
		  bool DisplayText)
{
  try
  {
	/* Dynamically display the contents of the fFirst field into the Label. */
	Text = Sender->AsString;
	Label1->Caption = Format("Person: %s", ARRAYOFCONST((Sender->AsString)));
  }
  catch(Exception &E)
  {
	/* Ensure Text remains the same. */
	Text = Sender->AsString;
  }
}

void __fastcall TMain::ClientDataSet1fFirstSetText(TField *Sender, const UnicodeString Text)
{
  /* We provide this OnSetText event to enter a custom string after each data
	 we input in the fFirst field. */
  try
  {
	/* Add the (First Name) string after the User input. */
	Sender->AsString = Format("%s (First Name)", ARRAYOFCONST((Text)));
	Label1->Caption = Format("%s (First Name) was successfully added.", ARRAYOFCONST((Text)));
  }
  catch(Exception &E)
  {
	/* If the try block fails, just add the data in the standard way. */
	Sender->AsString = Text;
	Label1->Caption = Format("%s was successfully added.", ARRAYOFCONST((Text)));
  }
}

Uses