[Gambas-user] Get the index of a menu
Kadaitcha Man
nospam.nospam.nospam at ...626...
Sat Nov 28 03:59:34 CET 2009
2009/11/28 David Dell <davidmdell at ...626...>:
> How can I get the index of a menu in the collection of children of its
> parent? There is no Menu.Index property.
>
> Thank you,
Put the ID in the tag property. That way you don't have to go
searching through a FOR EACH loop to get the ID.
I have a similar issue with ComboBoxes, which don't support tags so
you have to manage the information using collections. I solved the
problem in this way:
[CODE]
PRIVATE $colKeysByLabel AS NEW Collection ' Keeps track of keys for
menu item labels
PRIVATE $colKeysByID AS NEW Collection ' Keeps track of keys for menu
PRIVATE $SelectedID AS Integer ' Holds the currently selected ID for
the combobox
[...]
' This is done at startup if the cbo is static
' and it's done in a refresh routine if the cbo is dynamic
' for dynamic use:
' cboControlPanelServers.Clear
' $colKeysServersByLabel.Clear
' Load the combobox and the collections from a database table
FOR nZ = 1 TO SystemDataManager.rServers.Count
' Build the list in the combobox
cboControlPanelServers.Add(SystemDataManager.rServers!Label)
' The label is the key for this collection
$colKeysByLabel.Add(SystemDataManager.rServers!ID,
SystemDataManager.rServers!Label)
' The id is the key for this collection
$colKeysByID.Add(SystemDataManager.rServers!Label,
SystemDataManager.rServers!ID)
SystemDataManager.rServers.MoveNext
NEXT ' for each
[...]
' The user has clicked on an item
PUBLIC SUB cboControlPanelServers_Click()
IF $bLoading THEN
' Leave if we are starting up
RETURN
ENDIF
' Store the ID
$SelectedID = $colKeysByLabel[cboControlPanelServers.Current.Text]
END
[/CODE]
If you have an ID and you want to find the label then use the
$colKeysByID collection to return the label.
More information about the User
mailing list