Jump to content

Combo box - GUICtrlRead


John117
 Share

Recommended Posts

I have a list that fills a combo box. Instead of "GUICtrlRead($SomeRandomBox)" returning the value of the data in the box, I would like it to return the position in the list.

In that example, the below code would return '2' not "second".

#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Example()
Func Example()
 Local $msg
 GUICreate("My GUI combo")  ; will create a dialog box that when displayed is centered
 GUICtrlCreateCombo("", 10, 10) ; create first item
 GUICtrlSetData(-1, "First|Second|Third", "Second") ; add other item snd set a new default
 GUISetState()
 ; Run the GUI until the dialog is closed
 While 1
  $msg = GUIGetMsg()
  If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 WEnd
EndFunc   ;==>Example

I am trying to avoid:

If GUICtrlRead($SomeRandomBox) = "First" Then $Value = 1

If GUICtrlRead($SomeRandomBox) = "Second" Then $Value = 2

I am guessing there is an easier way, since the list will grow.

Link to comment
Share on other sites

Here is one method of returning the position of an item in a combo box list.

;#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)

Example()


Func Example()
    Local $msg, $idCombo, $idBut, $index, $sList = "First|Second|Third|F"
    GUICreate("My GUI combo") ; will create a dialog box that when displayed is centered
    $idCombo = GUICtrlCreateCombo("", 10, 10) ; create first item
    GUICtrlSetData(-1, $sList, "Second") ; add other item snd set a new default
    $idBut = GUICtrlCreateButton("Read", 220, 10, 50, 22)
    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case -3 ;$GUI_EVENT_CLOSE
                ExitLoop
            Case $idBut

                ; Count the number of "|"s before the selected item in the combo box list, plus one.
                StringReplace(StringLeft("|" & $sList & "|", StringInStr("|" & $sList & "|", "|" & GUICtrlRead($idCombo) & "|")), "|", "|")
                $index = @extended
                MsgBox(0, "Result", "Position in list = " & $index, 3)
        EndSwitch
    WEnd
EndFunc   ;==>Example
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...