Solved

Add Items to Popupmenu

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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