Popup list management

Somehow I’ve made this hard. I’ve got a popup and the list is derived from a database. The rows of the popup are populated when the window opens. A user can create a new record and select a value from the popup. However, if it’s an existing record I want to set the popup to show what the current value is. That removes the original list and just leaves the one item in the popup. So if the user wants to change the popup to another value there’s nothing there to choose from.

The only solution I can think of is to populate the popup rows from the database and then use a for/next loop to find the index of the selected value and then set the popup to that index. Does that sound right?

I did this a few days ago. I populated the list with every row from the database, and at the same time I added the PKID from each record into the rowtag. If the record is existing, you can set its value by either searching the PKID held in the rowtag or search the text values until you find a match, then set the listindex to that of the matching row.

Have a look here : https://forum.xojo.com/19690-set-popupmenu-listindex/p1#p165475

I was thinking that storing something in the row tag would be a solution. I like putting the PKID in the row tag. Thanks!

Its lends itself particularly well when working with lookup tables which is where I originally started looking at it. Keeping the PKID in the rowtag meant I didnt have to store it elsewhere, and when I commit a new record, the lookup value is instantly available from pm.RowTag(pm.Listindex).

The method you’re talking about is what we use all the time. In the open event we load the popup menu and set the row tag to either the primary key of the table or load the ActiveRecord object into it.

We have several extends methods for the PopupMenu that allows us to set and get the id from the popup very easily so you don’t have to loop through the list every single time. We have another one that allows us to set the text and row tag in one step too. Don’t underestimate the power of extends.

Can you elaborate? Or is it top secret :wink:

There’s no secret. Pretty basic stuff. This is for a WebPopupMenu but it’s identical code for a regular Popupmenu.

[code]Sub SetId(extends pm as WebPopupMenu, 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) isa Data.ActiveRecordBase then
if Data.ActiveRecordBase(pm.RowTag(i)).id = id then
pm.ListIndex = i
return
end if
end if
next
End Sub[/code]

[code]Function GetID(extends pm as WebPopupMenu) As Integer
if pm.ListIndex = -1 then return 0

if pm.RowTag(pm.ListIndex) isa Data.ActiveRecordBase then
dim id as integer = Data.ActiveRecordBase(pm.rowtag(pm.ListIndex)).id
return id
end if
return 0

End Function
[/code]

i am using the same method mention by Duane. Bob, can u elaborate on your method??

Forgot my AddRowAndTab extends:

Sub AddRowAndTag(extends pm as WebPopupMenu, Text as string, Tag as Variant) pm.AddRow Text pm.RowTag(pm.ListCount-1) = Tag End Sub

Example usage in a control open event:

[code] pmAddressType.DeleteAllRows

for each oType as Data.TR_AddressType in Data.TR_AddressType.List
pmAddressType.AddRowAndTag oType.sAddressTypeName, oType
next[/code]

In Data Load:

pmAddressType.SetId m_oAddress.iAddressType_ID

In Data Save:

m_oAddress.iAddressType_ID pmAddressType.GetID

In the long run, all of these methods exist to eliminate typing. I am, after all, a lazy programmer and you should strive to be lazy too. :slight_smile:

Hi Bob, I am not using ActiveRecord, so what would i use instead??

i guess most good programmer are also lazy programmer… if we do certain things more than twice, we make a method or function instead instead of having the code in multiple place.

Just use the RecordID instead.

cool…

Thanks Bob, I have the same. I was more intrigued by “so you don’t have to loop through the list every single time” which made it sound as if you had found a way to not having to loop. But you still loop, you just moved the loop into an extension (like I did), so I misunderstood the sentence.

No worries. It’s really ‘you don’t want to write the code to loop through it every time you need it’. I’ve worked on some projects where the original developer wrote the same looping code a thousand times by copying and pasting code and changing variable names. And then wondered why it was so hard to update his own code when it changed.

Be lazy. If you find yourself copy/pasting/changing variable names you might want to think about subclassing or using extends.

I have 2 extends going on this now too. One to set the listindex value, and another to get it. They both work really well.

#buffoonalert
For reasons known only to the devil himself, I started the loop with ‘for i as integer = 0 to pm.listindex - 1’ (Instead of listcount-1) and had the customary period of head scratching and wondering why it didn’t do a thing.

There’s a lot of info here. I don’t quite understand it all but I’m working through the techniques. Thanks.

Here is a suggestion:
After loading into a record set:

if rs.Field(“Sex”).StringValue = “Female” then
SexPopupMenu.ListIndex = 1
elseif rs.Field(“Sex”).StringValue = “Male” then
SexPopupMenu.ListIndex = 0
else
SexPopupMenu.ListIndex = -1
end if
Regards,

Phil