Solved

Add Items to Popupmenu

Posted on 1998-11-30
4
246 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 81
Delphi XE10 Round Image 2 79
How to create virtual printer while installing my application 5 24
Installshield for Embarcadero EX 10.1 Berlin 4 20
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

15 Experts available now in Live!

Get 1:1 Help Now