Re: Delphi -- fill popupmenu and select items


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by webmaster Guido on November 02, 2001 at 18:32:23:

In Reply to: fill popupmenu and select items posted by Stefan Loeners p12167 on November 01, 2001 at 09:34:42:

: Could you please provide some sample code to populate a popupmenu with items from a listbox at runtime and how to get the caption of such an item when selected.

-----

Let's suppose that we have a form with:
 - a listbox LB;
 - a popupmenu PU;
 - a label Label1 to show the caption of the menu-item that was clicked;
 - a button btnAddItem to add items to the listbox;
 - a button btnDeleteLastItem to remove items from the listbox.

The first menu-item should show the first item of the listbox, the second one shows the second listbox item, and so on... But what should we do if we allow the listbox to become empty? If we also empty the popup-menu, it won't pop up anymore. Therefore, I propose to always have at least one item in the popupmenu, and if the listbox is empty, we show a text in the first menu-item saying 'Click button to add items' or something similar.

Ok, let's code :)
Using the menu-editor, add one menu-item to the popupmenu, set its caption to 'Click button to add items' and give it the name MenuItem0. While still in the Object Inspector, doubleclick the OnClick event of MenuItem0 and next complete the event handler in the Editor:

procedure TForm1.MenuItem0Click(Sender: TObject);
begin
  Label1.Caption := (Sender as TMenuItem).Caption;
end;

Or, if you want an empty string when the listbox is empty:

procedure TForm1.MenuItem0Click(Sender: TObject);
begin
  if LB.Items.Count = 0 then 
    Label1.Caption := ''
  else
    Label1.Caption := (Sender as TMenuItem).Caption;
end;

Here's some code to add and remove items to the listbox:

procedure TForm1.btnAddItemClick(Sender: TObject);
begin
  LB.Items.Add('Option ' + IntToStr(LB.Items.Count));
  SyncMenuWithListbox;
end;

procedure TForm1.btnDeleteLastItemClick(Sender: TObject);
begin
  if LB.Items.Count > 0 then begin
    LB.Items.Delete(LB.Items.Count - 1);
    SyncMenuWithListbox;
  end;
end;

And finally, this code will synchronize the popupmenu and the listbox:

procedure TForm1.SyncMenuWithListbox;
var
  NewItem: TMenuItem;
  I: integer;
begin
  // Synchronize the number of items, but...
  // ...always leave one item in the popupmenu
  while (PU.Items.Count > LB.Items.Count) and
        (PU.Items.Count > 1) do
    PU.Items.Delete(PU.Items.Count - 1); // delete last item
  while PU.Items.Count < LB.Items.Count do begin
    NewItem := TMenuItem.Create(Self);
    NewItem.OnClick := MenuItem0Click;
    PU.Items.Add(NewItem);
  end;
  // Show listbox items in menu-items
  if LB.Items.Count = 0 then
    PU.Items[0].Caption := 'Click button to add items'
  else
    for I := 0 to LB.Items.Count - 1 do
      PU.Items[i].Caption := LB.Items[i];
end;

Tip for those of you that haven't worked with TPopupMenu yet: don't forget to set the form's property PopupMenu to the name of the popupmenu, so that the menu "pops up" with a right mouseclick on the form.


Related Articles and Replies


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]