In my (very simple) application, I have a popupmenu, that contains a short list of monthly bill descriptions like so :
Broadband
Council Tax
Credit Card
Home Insurance
Utilities
(There are others but I’ve shortened the list for the purpose of showing here).
In the rowtag for each item above, I have added the primary key ID (Integer) from the SQLite table that contains the information. So building it up, it now looks like this:
Rowtag Text Popupmenu Listindex
------ ---- -------------------
8 Broadband 0
4 Council Tax 1
6 Credit Card 2
9 Home Insurance 3
3 Utilities 4
Rowtag values then, are always unique but not always in numeric order, and will rarely, if ever, match the listindex value of the popupmenu.
In the program, I also have a property called ‘AccountID’ which is set elsewhere when a particular account is selected, and contains the same primary key ID as described above.
The idea with the code I’m about to show you is to set the listindex of the popupmenu to match the selected bill description, by comparing the rowtag to the AccountID.
Going on the above information, if the user selects for example the ‘Home Insurance’ account elsewhere on the screen, the AccountID property is set to a value of 9.
The code I have to do this is in a method as shown below :
SetTransactionAccount(AccountID As Integer)
For i As Integer = 0 To wMain.pmTransactionAccount.ListCount-1 ' zero based control, so start at 0 and subtract 1 from the total
If wMain.pmTransactionAccount.RowTag(i)=AccountID Then ' does the rowtag match the AccountID I'm interested in ?
wMain.pmTransactionAccount.ListIndex = i ' if it matches, set the ListIndex based on the ListCount value
Exit For i ' Exit the method since no further checks are required
End If ' This comparison is finished
Next ' Check the next ListCount item in the popupmenu
The code above works; my 2 questions are:
- Is this the correct way to set the popupmenu listindex to match the selected account ?
- Is ‘Exit For i’ the correct way to gracefully exit the method, since we dont want to do any more checks once the first (And only) match is found ?