SuggestEdit (Delphi)

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 enters the text back to the edit.

Code

procedure TMainForm.edtSuggestRightButtonClick(Sender: TObject);
var
  I: Integer;
  Item: TMenuItem;
  Edit: TButtonedEdit;
  Menu: TPopupMenu;
begin
  { Retrieve the edit and the menu. }
  Edit := Sender as TButtonedEdit;
  Menu := Edit.LeftButton.DropDownMenu;

  { Check if the text is nonempty and is not already
    added to the menu. }

  if Edit.Text = '' then
    Exit;

  { Also note that you need to strip the hot key,
    since the menu will add the ampersand character automatically. }
  for I := 0 to Menu.Items.Count - 1 do
    if StripHotkey(Menu.Items[I].Caption) = Edit.Text then
      Exit;

  { Create a new menu item and add it to the menu. }
  Item := TMenuItem.Create(Menu);
  Item.Caption := Edit.Text;
  Item.OnClick := MenuItemClick;

  Menu.Items.Add(Item);
end;

procedure TMainForm.MenuItemClick(Sender: TObject);
begin
  { When a menu item is selected, add its caption
    to the edit box. }
  edtSuggest.Text :=
    StripHotkey((Sender as TMenuItem).Caption);
end;

Uses