Solved

dynamic popupmenu creation

Posted on 1998-12-01
14
317 Views
Last Modified: 2008-09-22
I need to create a popup menu dynamicly. It has to change the number of items in it, every time it loads. I have been having a lot of trouble with it because if it has a number of items; the next time it loads, if it has less items, it has the first number of items, it just show the new items in the bottom slots with the old items on top.

Here is the procedure I have right now:

procedure TMainfrm.MenuChange;
var
  PopUpItems: array[0..99] of TMenuItem;
  i: integer;
begin
 for i := 1 to {always changing variable} do begin
    popupitems[i].free;
    PopUpItems[i] := TMenuItem.Create(Self);
    PopUpItems[i].Caption := IntToStr(i);
    PopUpItems[i].Tag := i;
    PopupItems[i].hint := popupitems[i].caption;  
    Popupitems[i].Radioitem := true;
    popupItems[i].OnClick := PopUpevent;
    PopUpMenu1.Items.add(PopUpItems[i]);
  end;
end;

A solution to this problem is greatly appreciated.
thanks
spat  
0
Comment
Question by:spat
  • 4
  • 3
  • 2
  • +4
14 Comments
 

Author Comment

by:spat
ID: 1348806
Edited text of question
0
 

Author Comment

by:spat
ID: 1348807
Edited text of question
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1348808
The solution is to clear the list of items before added them. Do this before your main loop:

 popupitems.items.clear;

or this if clear does not compile:

     for I := 1 to popupitems.items.count do
        popupitems.items.remove(0);    // remove may need to be delete

cheers,

Raymond.
 
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:spat
ID: 1348809
I have tried to delete the items but i recieve an error message that says the menu index is out of range
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348810
hi spat,

use this to delete the items:

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


meikl
0
 
LVL 1

Expert Comment

by:Gerhard100198
ID: 1348811
for I := 1 to popupitems.items.count do
       popupitems.items.remove(0);    // remove may need to be delete

You will receive a "menu index is out of range" error when trying to run the above code.

Try the following which should do the job:

 for I := 0 to popupitems.items.count - 1 do

Here is something else you might want to consider:

There is no reason to store the created menu items into an array (well there is one small situation where you would need to do it which I'll get to a bit later).

You could use the following code and it will work fine:


     var
       i: integer;
     begin
      for i := 1 to {always changing variable} do begin
        With TMenuItem.Create(Self) do begin
           //Though you don't need to assign a name I like to do it anyway since you
           //might want to access the object at a later stage
           Name := 'mi_' + IntToStr(i);
           Caption := IntToStr(i);
           PopUpItems[i].Tag := i;
           PopupItems[i].hint := popupitems[i].caption;  
           Popupitems[i].Radioitem := true;
           popupItems[i].OnClick := PopUpevent;
       end;
      end;

       //If you would like to change any of the meny item's properties at a later stage
       //you can get access to it's properties in the following way. This is the reason
       //I assigned my own component name earlier so that I can reference them by name
       //when needed. The FindComponent function is extremely handy since you can pass
       //it a component's name as a string and it will return the TObject (just remember to
       //typecast to TMenuItem since you received a TObject.
       With TMenuItem(FindComponent('mi_1')) do Enabled := False;
       With TMenuItem(FindComponent('mi_3')) do Visible := False;
       ...etc.
     end;

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348812
gerhard is correct,

your items start from index 0!!!!!!!

thus you need to iterate like : for i := 0 to popitems.items.count-1

Zif.
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1348813
Hmm, it shouldn't make any difference, as Raymond goes from 1 to count and allways are deleting entry 0..
(1 to count = 0 to count-1 :-)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348814
? Blackman,

let say count = 5

 if you go from i = 1 to 5

 or from i = 0 to 4

and use remove(i)

Then remove(5) will give a problem! Because i=5 doesn't exist! the list goes till i=4!! Or am I not awake enough?

Zif.
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1348815
That's right, but Raymond is allways deleting item 0!, so all he wants, is to iterate 5 times...
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348816
:-) Yes, now I see it too. Possibly a typo-error? :-))))

0
 
LVL 3

Accepted Solution

by:
Ronald Buster earned 80 total points
ID: 1348817
I just used a similar function a month ago here is how I did this :

add following to tmainfrm private section

fPopUpMenu : TPopUpMenu

your procedure

procedure TMainfrm.MenuChange;
var tm : tmenuitem;
      i   : integer;
begin
   if assigned(fPopUpMenu) then
     fPopUpMenu.Free;

   fPopUpMenu := TPopUpMenu.Create(Self);
   for i := 1 to {always changing value}  do
     begin
         tm  := TMenuItem.Create(Self);
         tm.Caption := IntToStr(i)];
         tm.Tag     := i;
         tm.OnClick := PopUpEvent;
         fPopUpMenu.Items.Add(tm);
     end;
end;

This should work regards,

Cono



0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1348818
Thanks Blackman - after reading half a dozen comments regarding how it should be 0 to count -1 I was going to post a sarcastic comment regarding whether anybody bothered to read the code!

ZifNab: Nope, not a typo, just an alternative form of while popupitems.items.count >0 do popupitems.items.delete(0) (though I admit the while loop is a little cleaner!)

spat: Did you try the code I suggested (with delete in place of remove in the loop)?

Cheers,

Raymond.
0
 

Author Comment

by:spat
ID: 1348819
I tried Gerhard's code, it worked perfect. Thank you for all of your assistance.

rwilson, I did try your code with delete instead of remove. Thanks anyway
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 121
Delphi application Soap connection 5 104
Create a path if not exists 7 77
Twebbrowser in Fire monkey android scrolling to bottom 1 30
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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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