Sign in to follow this  
Followers 0
John117

Combo box - GUICtrlRead

5 posts in this topic

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

_GUICtrlComboBox_FindString() and _GUICtrlComboBox_FindStringExact() are also useful functions for this. Look in the help file.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Look in the help file for:

_GUICtrlComboBox_FindString

:)

Edit: looks like some people beat me to it, a minute early ;)

Edited by mischieftoo

Share this post


Link to post
Share on other sites

Thank you very much! Will get to it!

Share this post


Link to post
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
Sign in to follow this  
Followers 0