Solved

Add Items to Popupmenu

Posted on 1998-11-30
4
245 Views
Last Modified: 2010-04-04
(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
Comment
Question by:vis
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
BigRat earned 50 total points
ID: 1348615
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348616
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348617
hi vis,

sorry, one mistake

the line
 popupmenu1.add(NewItem);

should be
 popupmenu1.items.add(NewItem);

meikl
0
 
LVL 27

Expert Comment

by:BigRat
ID: 1348618
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now