popupmenus and rowtag

Hi, is it possible to change from code the popupmenu so that it will show a text that has a given rowtag.

I can not use listindex because the list is alfabethic, so when I get the info back from my DB (which has only stored a pointer to the name, that is the pointer is in a popupmenu rowtag as well),

I would therefore use the rowtag as the value for the listindex.

I can do it with a loop, but is there a more direct way of doing it, and not loop through a search ?

popupmenu1.removerow(1) popupmenu1.InsertRow(1,"Two")

Rows are zero based.

But the list have i.e. 10 names, but the list is alphabetical, and the rowTag and names are from a DB.table.
so the insert into the popupmenu is ok. But how do I choose it after.
i.e
I would like to switch to name with rowtag 7, even if this is listed as name no 2…
The info I have from db is 7 and not 2 so how can I use rowtag as the reference ?

did this explain it better ?

sincerely Helge

[quote=164153:@Helge Tjelta]But the list have i.e. 10 names, but the list is alphabetical, and the rowTag and names are from a DB.table.
so the insert into the popupmenu is ok. But how do I choose it after.
i.e
I would like to switch to name with rowtag 7, even if this is listed as name no 2…
The info I have from db is 7 and not 2 so how can I use rowtag as the reference ?

did this explain it better ?

sincerely Helge[/quote]

I see no other way than to loop through values.

Iterate through the RowTag and if the ID matches what you want then you’re good.

for i as integer = 0 to PopupMenu1.Listcount -1 if PopupMenu1.rowtag(i) = SomeID then PopupMenu1.listindex = i exit end next

You only have to make sure that you put the recordID into the RowTag property of the PopupMenu.

As Bob says - looping is the way to do it. If you’ve got so many entries in the PopUpMenu to make the loop slow then you’re probably looking at a fairly horrible user experience anyway! :wink:

Keep in mind that you don’t need to have this code more than once - ever. You can use an extends to make it part of the popupmenu class.

[code]Sub SetId(extends pm as PopupMenu, id as integer)
if id < 1 then
pm.ListIndex = -1
return
end if

for i as Integer = 0 to pm.ListCount - 1
if pm.RowTag(i) = id then
pm.ListIndex = i
return
end if
next
End Sub[/code]

Then, every time you need it, you can simply call it like this:

popupmenu1.SetID someID

No need to subclass and it will show up in auto complete. Extends is really powerful from that aspect.

@Bob Keeney That’s a great tip Bob, thanks. I have been coding loops through each popup menu I use, this code will save me a load of time and keep my code shorter. Thanks again! :slight_smile:

Sure, anytime you find yourself repeating the same code over and over again it’s probably time to either subclass it or use extends. I try really hard to be as lazy a programmer as I can be.

I realy like the lazy part! :slight_smile:

You should read xDev. This was in the Tips&Tricks column :wink: