FileSelectBtnEdit (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This exemple demonstrates the use of a TButtonedEdit edit control. The code loads two bitmaps located as resources in the executable and generate an image list at run time. The two loaded images are then used for the left and right buttons of the edit control.

Code

__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
	TEditButton *RightBtn, *LeftBtn;

	/* Assign a new image list, loaded from resources. */
	edtSelectFile->Images = makeImageList(
		OPENARRAY(String, ("OpenFile_Image", "ClearFile_Image")));

	/* Register right and left buttons for the edit. */
	RightBtn = edtSelectFile->RightButton;
	RightBtn->ImageIndex = 0;
	RightBtn->Visible = true;

	LeftBtn = edtSelectFile->LeftButton;
	LeftBtn->ImageIndex = 1;
	LeftBtn->Visible = true;

	/* Register an OnClick events for the buttons. */
	edtSelectFile->OnRightButtonClick = EditRightButtonClick;
	edtSelectFile->OnLeftButtonClick = EditLeftButtonClick;
}

TImageList* __fastcall TMainForm::makeImageList(OpenArray<String> resNames, int resCount)
{
	Graphics::TBitmap* resBmp;
	TImageList* imgList;

	/* Create an image list. */
	imgList = new TImageList(this);

	for (int i = 0; i <= resCount; ++i)
	{
		/* Create a new bitmap image. */
		resBmp = new Graphics::TBitmap();

		/* Try to load the bitmap from the resource. */
		try
		{
			resBmp->LoadFromResourceName((unsigned int)HInstance,
				resNames[i]);

			resBmp->Transparent = true;
		}
		catch(...)
		{
			delete resBmp;
			delete imgList;
			return NULL;
		}

		imgList->Add(resBmp, NULL);
	}

	return imgList;
}


void __fastcall TMainForm::EditRightButtonClick(TObject* Sender)
{
	/* Get the text from the edit. */
	OpenDialog->FileName = edtSelectFile->Text;

	if (OpenDialog->Execute())
		edtSelectFile->Text = OpenDialog->FileName;
}

void __fastcall TMainForm::EditLeftButtonClick(TObject* Sender)
{
	/* Clear the contents of the edit box. */
	edtSelectFile->Clear();
}

Uses