SuggestEdit (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demostrates the use of a buttoned edit. The edit box is configured to have both right and left buttons enabled. The right button adds the current text into a menu and the left button displays that menu. Clicking a menu item will enter the text back to the edit.

Code

void __fastcall TMainForm::edtSuggestRightButtonClick(TObject *Sender)
{
	TMenuItem* item;
	TButtonedEdit* edit;
	TPopupMenu* menu;

	/* Retrieve the edit and the menu. */
	edit = dynamic_cast<TButtonedEdit*>(Sender);
	menu = edit->LeftButton->DropDownMenu;

	/* Check if the text is nonempty and is not already
	   added to the menu. */

	if (edit->Text == "")
		return;

	/* Also note that you need to strip the hot key, 
	   since the menu adds the & character automatically */
	for (int i = 0; i < menu->Items->Count; i++)
		if (StripHotkey(menu->Items[i].Caption) == edit->Text)
			return;

	/* Create a new menu item and add it to the menu. */
	item = new TMenuItem(menu);
	item->Caption = edit->Text;
	item->OnClick = MenuItemClick;

	menu->Items->Add(item);
}

void __fastcall TMainForm::MenuItemClick(TObject *Sender)
{
  /* When a menu item is selected, add its caption
	  to the edit box. */
  edtSuggest->Text =
	StripHotkey(dynamic_cast<TMenuItem*>(Sender)->Caption);
}

Uses