• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Add Items to Popupmenu

(Hello...Experts)
I have a popupmenu1 component on a form and listbox1 and one button, when i push the button i will read all the text in the listbox and add to popupmenu

I have try using TItem and nothing work, if you have a complete code , then i will be glad
Thanks!!!!
0
vis
Asked:
vis
  • 2
  • 2
1 Solution
 
BigRatCommented:
Each element of a popup menu is a menu item. Therefore you must take the text from each of the ListBox entries and create a menu item for it, then add that to your popup menu. I'll assume that the context for the following code is the Frame which will contain the Popup menu, because on creation of a menu item you need to specify a component (should be OK if you put it in the ButtonClick code) :-
var
    Popup    : TMenuPopup;
    NewItem : TMenuItem;

    for i:=1 to LisBox.Count do
        begin
        NewItem := TMenuItem.Create(Self);
        NewItem.Caption := ListBox.Items[i].Text;
        Popup.Add(NewItem);
        end;

OK? Reject answer if not satisfied or something is unclear.
0
 
kretzschmarCommented:
hi vis, hi BigRat,

BigRat : Have you tested your answer?

vis: try this

procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer;
  NewItem : TMenuItem;

begin

  { Clear PopupItems }

  while popupmenu1.Items.Count > 0 do
    popupmenu1.Items.Delete(0);

  { Add PopupItems }

  for i := 0 to listbox1.Items.Count - 1 do
  begin
    NewItem := TMenuItem.Create(Self);
    NewItem.Caption := listbox1.Items.Strings[i];
    popupmenu1.add(NewItem);
  end;

end;

meikl
0
 
kretzschmarCommented:
hi vis,

sorry, one mistake

the line
 popupmenu1.add(NewItem);

should be
 popupmenu1.items.add(NewItem);

meikl
0
 
BigRatCommented:
Typical Rat! In my Popup Menu I did not extract the Texts from a ListBox but a database (context dependant popup menu) so I changed the "NewItem.Caption:=.." in a hurry. And I ALWAYS forget that these silly ListBoxes have Items which are not indexable, but one has to get Strings which are. Sorry!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now