Jump to content

Easy way to retrieve combobox selection?


TheBG
 Share

Recommended Posts

Hello all,

I have and tried out many of the already posted threads, but there has got to be an easier way to return the value of the combobox selection.

I do not like using the Opt("GUIoneventmode", 1) Mode. Why? because I have to go back and redo all the code for the buttons and checkboxes that I already have up and running.

All I really need is the ability to select a combo box and return it's value.

Is there any other easy way?

Here's a working sample:

#include <GUIConstants.au3>
Opt("GUIoneventmode", 1)

$Form1 = GUICreate("Dialog", 316, 231, 227, 306)
GUISetIcon("D:\003.ico")
$GroupBox1 = GUICtrlCreateGroup("", 8, 1, 297, 193)
$Combo1 = GUICtrlCreateCombo("Combo1", 16, 16, 121, 25)
GUICtrlSetData(-1, "None|1|1 + 2|1 + 3")
GUICtrlSetOnEvent(-1,"_comboboxselect")
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 40, 48, 129, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 40, 80, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Checkbox3", 40, 104, 105, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("&OK", 65, 203, 75, 25, 0)
GUICtrlSetOnEvent(-1, "OKPressed")
$Button2 = GUICtrlCreateButton("&Cancel", 162, 203, 75, 25, 0)
GUICtrlSetOnEvent(-1, "CancelPressed")
GUISetState(@SW_SHOW)



; Retrieve the combobox selection and set checkboxes
Func _comboboxselect()
 Local $sel = GUICtrlRead($Combo1)
   Select 
    Case $sel = "None"
       GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
       GUICtrlSetState($Checkbox2, $GUI_UNCHECKED)
       GUICtrlSetState($Checkbox3, $GUI_UNCHECKED)
     Case $sel = "1"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
     Case $sel = "1 + 2"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
       GUICtrlSetState($Checkbox2, $GUI_CHECKED)
     Case $sel = "1 + 3"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
       GUICtrlSetState($Checkbox3, $GUI_CHECKED)
   EndSelect
EndFunc

; Cancel then Exit the program
Func CancelPressed()
  Local $Cancel 
    $Cancel = MsgBox(262452,"Cancel","Are you sure you want to Cancel and then Exit?")
    If $Cancel = 6 Then
    Exit
    Endif
EndFunc

; Cancel then Exit the program
Func OKPressed()
  Local $Cancel 
    $Cancel = MsgBox(262452,"Cancel","Are you sure you want to Run the program and then Exit?")
    If $Cancel = 6 Then
    Exit
    Endif
EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

I have a pretty extensive menu in my real application. The main goal of the combobox is to let the user pick a predefined selection set. But I will have to completely revamp most of the code by using the Opt("GUIoneventmode", 1) Mode

Link to comment
Share on other sites

  • Moderators

TheBG,

Here is how I would do it:

#include <GUIConstants.au3>
#Include <GuiComboBox.au3>

$Form1 = GUICreate("Dialog", 316, 231, 227, 306)
GUISetIcon("D:\003.ico")
$GroupBox1 = GUICtrlCreateGroup("", 8, 1, 297, 193)
$Combo1 = GUICtrlCreateCombo("", 16, 16, 121, 25)
GUICtrlSetData(-1, "None|1|1 + 2|1 + 3")
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 40, 48, 129, 25)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 40, 80, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Checkbox3", 40, 104, 105, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("&OK", 65, 203, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Cancel", 162, 203, 75, 25, 0)
GUISetState(@SW_SHOW)

$sCurr_Combo_Sel = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            OKPressed()
        Case $Button2
            CancelPressed()
        Case $Combo1
            ; Wait until combo is closed
            While _GUICtrlComboBox_GetDroppedState($Combo1)
                Sleep(10)
            WEnd
            ; See if combo slection has changed
            If GUICtrlRead($Combo1) <> $sCurr_Combo_Sel Then
                GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
                GUICtrlSetState($Checkbox2, $GUI_UNCHECKED)
                GUICtrlSetState($Checkbox3, $GUI_UNCHECKED)
                Switch GUICtrlRead($Combo1)
                    Case "1"
                        GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                    Case "1 + 2"
                        GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                        GUICtrlSetState($Checkbox2, $GUI_CHECKED)
                    Case "1 + 3"
                        GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                        GUICtrlSetState($Checkbox3, $GUI_CHECKED)
                EndSwitch
            EndIf
    EndSwitch
WEnd

; Retrieve the combobox selection and set checkboxes
Func _comboboxselect()
 Local $sel = GUICtrlRead($Combo1)
   Select
    Case $sel = "None"
       GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
       GUICtrlSetState($Checkbox2, $GUI_UNCHECKED)
       GUICtrlSetState($Checkbox3, $GUI_UNCHECKED)
     Case $sel = "1"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
     Case $sel = "1 + 2"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
       GUICtrlSetState($Checkbox2, $GUI_CHECKED)
     Case $sel = "1 + 3"
       GUICtrlSetState($Checkbox1, $GUI_CHECKED)
       GUICtrlSetState($Checkbox3, $GUI_CHECKED)
   EndSelect
EndFunc

; Cancel then Exit the program
Func CancelPressed()
  Local $Cancel
    $Cancel = MsgBox(262452,"Cancel","Are you sure you want to Cancel and then Exit?")
    If $Cancel = 6 Then
    Exit
    Endif
EndFunc

; Cancel then Exit the program
Func OKPressed()
  Local $Cancel
    $Cancel = MsgBox(262452,"Cancel","Are you sure you want to Run the program and then Exit?")
    If $Cancel = 6 Then
    Exit
    Endif
EndFunc

A couple of points:

1. Set all the checkboxes to unchecked each time - try your code going from "1 + 3" to "1" and see what happens >_< .

2. This code waits for the combo to close - good sign a selection has been made - and then sees if the selection has changed.

3. The initial "Combo1" text has been removed from the combo creation code - or it shows up in the list of possible selections. If you want to have some text there, use _GUICtrlComboBox_SetEditText - it vanishes once a selection is made.

I hope this helps.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...