TButtonedEdit (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TButtonedEdit class. This example implements a component like "TButtonedEdit search", that can be found in the tool palette window. Entering the control enables the right button; the left button displays a MessageDlg and the right button clears the input text.

Code

__fastcall TForm4::TForm4(TComponent* Owner)
	: TForm(Owner)
{
	//Declaring an instance
	btnEdit = new TButtonedEdit(this);

	//Setting the parent so that it will autodelete
	btnEdit->Parent = this;

	//Positioning
	btnEdit->Left = 10;
	btnEdit->Top = 10;

	//Creating a TImageList and using it for the button
	TImageList* imList = new TImageList(this);
	Graphics::TBitmap* bmap = new Graphics::TBitmap();
	bmap->LoadFromFile("..\\littleB_16.bmp");
	imList->Add(bmap,NULL);
	bmap->LoadFromFile("..\\little_logo_16.bmp");
	imList->Add(bmap,NULL);
	btnEdit->Images = imList;

	//Selecting the image for the left button and enabling it
	btnEdit->LeftButton->Visible = true;
	btnEdit->LeftButton->Enabled = true;
	btnEdit->LeftButton->ImageIndex = 0;
	btnEdit->LeftButton->DisabledImageIndex = 0;
	btnEdit->LeftButton->PressedImageIndex = 0;
	btnEdit->LeftButton->HotImageIndex = 0;

	//Selecting the image for the right button
	//This will be enabled when the content changes.
	btnEdit->RightButton->Enabled = true;
	btnEdit->RightButton->ImageIndex = 1;
	btnEdit->RightButton->DisabledImageIndex = 1;
	btnEdit->RightButton->PressedImageIndex = 1;
	btnEdit->RightButton->HotImageIndex = 1;

	//Selecting an action for the buttoned edit
	btnEdit->OnEnter = btnEditEnter;
	btnEdit->OnRightButtonClick = btnEditRightClick;
	btnEdit->OnLeftButtonClick = btnEditLeftClick;

	//Default text in the edit
	btnEdit->Text = "Default";

	btnEdit->Constraints->MinHeight = 16;
	btnEdit->Height = 16;

	//Finally, displaying the button
	btnEdit->Show();

}

void __fastcall TForm4::btnEditEnter(TObject* Sender){
   btnEdit->Text = "";
   btnEdit->RightButton->Visible = true;
}

void __fastcall TForm4::btnEditRightClick(TObject* Sender){
   //Resetting to default state
   btnEdit->Text = "Default";
   btnEdit->RightButton->Visible = false;
}

void __fastcall TForm4::btnEditLeftClick(TObject* Sender){
   MessageDlg("Left button pressed with: " + btnEdit->Text,
  			 mtInformation, TMsgDlgButtons() << mbOK, 0);

}

Uses