VCLStringHandling (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example is a VCL Form application that shows how to handle strings in a VCL Form. The application uses basic string operations and methods to modify the Caption property of a TStaticText.

To build and test this example, create a VCL Form Application - C++, then add the next objects to the form:

  • A TStaticText control.
  • Two TPanel controls. Add the following components to the first TPanel control:
    • Two TLabel controls. Set the Caption properties to Insert characters: and Text:.
    • Two TEdit controls. Set the Name properties to textEdit and insPositionEdit.
    • Three TRadioButton controls. Set the Name properties to beginingRadio, endRadio, insPositionRadio, and respectively the Caption properties to At the begining, At the end, and At a specified position.
    • A TButton control. Set the Name property to insertButton, and the Caption property to Insert.
  • Add the following components to the first TPanel control:
    • Three TLabel controls. Set the Caption properties to Delete characters:, Position:, and Count:.
    • Three TEdit controls. Set the Name properties to delPositionEdit, countEdit, and substringEdit.
    • Two TRadioButton controls. Set the Name properties to delPositionRadio and substringRadio, and respectively the Caption properties to Specify position and Specify substring.
    • A TButton control. Set the Name property to deleteButton and the Caption property to Delete.

At this moment the form should look like in the following image.

Error creating thumbnail: Unable to save thumbnail to destination

Code

Add the following code to the OnCreate event handler of the VCL Form:

// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
	beginingRadio->Checked = true;
	delPositionRadio->Checked = true;
	staticText->Caption = "This text will be modified";
}

Add the following code to the OnClick event handler of the insertButton:

// ---------------------------------------------------------------------------
void __fastcall TForm1::insertButtonClick(TObject *Sender) {
	int position;

	if (beginingRadio->Checked) {
		staticText->Caption = textEdit->Text + staticText->Caption;
	}
	else if (endRadio->Checked) {
		staticText->Caption = staticText->Caption + textEdit->Text;
	}
	else if (insPositionRadio->Checked) {
		try {
			position = StrToInt(insPositionEdit->Text);
			staticText->Caption = staticText->Caption.Insert(textEdit->Text,
				position);
		}
		catch (Exception& e) {
			ShowMessage(e.ToString());
		}
	}
}

Add the following code to the OnClick event handler of the deleteButton:

// ---------------------------------------------------------------------------
void __fastcall TForm1::deleteButtonClick(TObject *Sender) {
	if (delPositionRadio->Checked) {
		try {
			int position, count;
			position = StrToInt(delPositionEdit->Text);
			count = StrToInt(countEdit->Text);
			staticText->Caption = staticText->Caption.Delete(position, count);
		}
		catch (Exception& e) {
			ShowMessage(e.ToString());
		}
	}
	else if (substringRadio->Checked) {
		staticText->Caption = StringReplace(staticText->Caption,
			substringEdit->Text, "", TReplaceFlags() << rfReplaceAll);
	}
}

Uses

See Also