Jump to content

Dependent Combo Boxes example


solistic
 Share

Recommended Posts

Here is a demo for dealing with dependent combo boxes

CODE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Demonstrate how to deal with three dependent combo boxes

; where the list data in combo box 2 and 3 is dependent on

; the selection within combo box 1 and 2, respectively

#include <GUIConstants.au3>

#include <GuiCombo.au3>

Dim $location [3] = ["Swansea", "Cardiff", "Bristol"]

Dim $list1 [6] = ["Select ...", 1,2,3,4,5]

Dim $list2 [6] = ["Select ...", 10,20,30,40,50]

Dim $list3 [6] = ["Select ...", 100,200,300,400,500]

Dim $list4 [6] = ["Select ...", 1000,2000,3000,4000,5000]

Dim $list5 [6] = ["Select ...", 10000,20000,30000,40000,50000]

Dim $list6 [6] = ["Select ...", 100000,200000,300000,400000,500000]

GUICreate("Triple Combo box demo", 220, 220)

; Create first combo box

$c1 = GUICtrlCreateCombo ("Select ...", 10,10) ; create first item

GUICtrlSetData($c1,$location[0] & "|" & $location[1] & "|" & $location[2],"Select ...") ; add other items snd set a new default

; Create second and third combo boxes

$c2 = GUICtrlCreateCombo ("", 10,35)

$c3 = GUICtrlCreateCombo ("", 10,60)

GUICtrlSetState($c2, $GUI_DISABLE)

GUICtrlSetState($c3, $GUI_DISABLE)

; Create a label to display results

$output = GUICtrlCreateLabel("", 10, 85, 200, 20, $SS_SUNKEN )

GUISetState ()

$h1 = GUICtrlCreateLabel("", 10, 125, 200, 75, $SS_CENTER )

GUICtrlSetData($h1, "Demonstrating how to deal with three dependent combo boxes where the list data in combo box 2 and 3 is dependent on the selection within combo box 1 and 2, respectively")

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $c1

$cr = GUICtrlRead($c1) ; read the combo box 1 value

GUICtrlSetState($c2, $GUI_ENABLE)

GUICtrlSetData($c2, "") ; clear current value from combo box 2

GUICtrlSetData($c3, "") ; clear current value from combo box 3

GUICtrlSetState($c3, $GUI_DISABLE)

If $cr = $location[0] Then

populate($c2, $list1)

ElseIf $cr = $location[1] Then

populate($c2, $list2)

ElseIf $cr = $location[2] Then

populate($c2, $list3)

EndIf

Case $msg = $c2 and $cr = $location[0]

$cr2 = GUICtrlRead($c2) ; read the combo box 2 value

GUICtrlSetState($c3, $GUI_ENABLE)

GUICtrlSetData($c3, "") ; clear current value from combo box 3

If $cr2 = "1" Then

populate($c3, $list1)

ElseIf $cr2 = "2" Then

populate($c3, $list2)

ElseIf $cr2 = "3" Then

populate($c3, $list3)

ElseIf $cr2 = "4" Then

populate($c3, $list4)

ElseIf $cr2 = "5" Then

populate($c3, $list5)

EndIf

GUICtrlSetState($c2, $GUI_DISABLE)

Case $msg = $c2 and $cr = $location[1]

$cr2 = GUICtrlRead($c2) ; read the combo box 2 value

GUICtrlSetState($c3, $GUI_ENABLE)

GUICtrlSetData($c3, "") ; clear current value from combo box 3

If $cr2 = "10" Then

populate($c3, $list2)

ElseIf $cr2 = "20" Then

populate($c3, $list3)

ElseIf $cr2 = "30" Then

populate($c3, $list4)

ElseIf $cr2 = "40" Then

populate($c3, $list5)

ElseIf $cr2 = "50" Then

populate($c3, $list6)

EndIf

GUICtrlSetState($c2, $GUI_DISABLE)

Case $msg = $c2 and $cr = $location[2]

$cr2 = GUICtrlRead($c2) ; read the combo box 2 value

GUICtrlSetState($c3, $GUI_ENABLE)

GUICtrlSetData($c3, "") ; clear current value from combo box 3

If $cr2 = "100" Then

populate($c3, $list3)

ElseIf $cr2 = "200" Then

populate($c3, $list4)

ElseIf $cr2 = "300" Then

populate($c3, $list5)

ElseIf $cr2 = "400" Then

populate($c3, $list6)

ElseIf $cr2 = "500" Then

populate($c3, $list1)

EndIf

GUICtrlSetState($c2, $GUI_DISABLE)

Case $msg = $c3

$cr3 = GUICtrlRead($c3)

GUICtrlSetData($output, $cr3)

GUICtrlSetState($c3, $GUI_DISABLE)

EndSelect

Wend

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Populate a combo box with list data

Func populate($ctrl, $l)

GUICtrlSetData($ctrl, $l[0], $l[0]) ; put the first item in the list

For $a = 1 to UBound($l) - 1

_GUICtrlComboAddString($ctrl,$l[$a]) ; add all other items to the list

Next

EndFunc

Link to comment
Share on other sites

(8) : ==> Error opening the file.:

#include <GuiCombo.au3>

(123) : ==> Unknown function name.:

_GUICtrlComboAddString($ctrl,$l[$a])

This is something to do with the version 3.2.12 updates.

The example was written using v3.2.8.1.

Errors relating to use of GuiCombo.au3 now seem to have proliferated.

_GuiCtrlComboAddString() is a UDF included with v3.2.8.1

Perhaps someone else can work out what's wrong?

Link to comment
Share on other sites

Here it is fixed.. GuiComboBox.au3 changed in new load...

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Demonstrate how to deal with three dependent combo boxes
; where the list data in combo box 2 and 3 is dependent on 
; the selection within combo box 1 and 2, respectively

#include <GUIConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Dim $location [3] = [\"Swansea\", \"Cardiff\", \"Bristol\"]

Dim $list1 [6] = [\"Select ...\", 1,2,3,4,5]
Dim $list2 [6] = [\"Select ...\", 10,20,30,40,50]
Dim $list3 [6] = [\"Select ...\", 100,200,300,400,500]
Dim $list4 [6] = [\"Select ...\", 1000,2000,3000,4000,5000]
Dim $list5 [6] = [\"Select ...\", 10000,20000,30000,40000,50000]
Dim $list6 [6] = [\"Select ...\", 100000,200000,300000,400000,500000]

GUICreate(\"Triple Combo box demo\", 220, 220) 

; Create first combo box
$c1 = GUICtrlCreateCombo (\"Select ...\", 10,10) ; create first item

GUICtrlSetData($c1,$location[0] & \"|\" & $location[1] & \"|\" & $location[2],\"Select ...\") ; add other items snd set a new default

; Create second and third combo boxes
$c2 = GUICtrlCreateCombo (\"\", 10,35) 
$c3 = GUICtrlCreateCombo (\"\", 10,60) 
GUICtrlSetState($c2, $GUI_DISABLE)
GUICtrlSetState($c3, $GUI_DISABLE)

; Create a label to display results
$output = GUICtrlCreateLabel(\"\", 10, 85, 200, 20, $SS_SUNKEN )
GUISetState ()

$h1 = GUICtrlCreateLabel(\"\", 10, 125, 200, 75, $SS_CENTER )
GUICtrlSetData($h1, \"Demonstrating how to deal with three dependent combo boxes where the list data in combo box 2 and 3 is dependent on the selection within combo box 1 and 2, respectively\")
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE 
ExitLoop

Case $msg = $c1 
$cr = GUICtrlRead($c1) ; read the combo box 1 value
GUICtrlSetState($c2, $GUI_ENABLE)
GUICtrlSetData($c2, \"\") ; clear current value from combo box 2
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
GUICtrlSetState($c3, $GUI_DISABLE)
If $cr = $location[0] Then 
populate($c2, $list1)
ElseIf $cr = $location[1] Then 
populate($c2, $list2)
ElseIf $cr = $location[2] Then 
populate($c2, $list3)
EndIf
Case $msg = $c2 and $cr = $location[0]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"1\" Then 
populate($c3, $list1)
ElseIf $cr2 = \"2\" Then 
populate($c3, $list2)
ElseIf $cr2 = \"3\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"4\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"5\" Then 
populate($c3, $list5)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c2 and $cr = $location[1]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"10\" Then 
populate($c3, $list2)
ElseIf $cr2 = \"20\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"30\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"40\" Then 
populate($c3, $list5)
ElseIf $cr2 = \"50\" Then 
populate($c3, $list6)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c2 and $cr = $location[2]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"100\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"200\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"300\" Then 
populate($c3, $list5)
ElseIf $cr2 = \"400\" Then 
populate($c3, $list6)
ElseIf $cr2 = \"500\" Then 
populate($c3, $list1)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c3
$cr3 = GUICtrlRead($c3)
GUICtrlSetData($output, $cr3)
GUICtrlSetState($c3, $GUI_DISABLE)
EndSelect 
Wend

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Populate a combo box with list data
Func populate($ctrl, $l) 
GUICtrlSetData($ctrl, $l[0], $l[0]) ; put the first item in the list
For $a = 1 to UBound($l) - 1
_GUICtrlComboBox_AddString($ctrl,$l[$a]) ; add all other items to the list
Next 
EndFunc

Edited by gesller
Link to comment
Share on other sites

Here it is fixed.. GuiComboBox.au3 changed in new load...

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Demonstrate how to deal with three dependent combo boxes
; where the list data in combo box 2 and 3 is dependent on 
; the selection within combo box 1 and 2, respectively

#include <GUIConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Dim $location [3] = [\"Swansea\", \"Cardiff\", \"Bristol\"]

Dim $list1 [6] = [\"Select ...\", 1,2,3,4,5]
Dim $list2 [6] = [\"Select ...\", 10,20,30,40,50]
Dim $list3 [6] = [\"Select ...\", 100,200,300,400,500]
Dim $list4 [6] = [\"Select ...\", 1000,2000,3000,4000,5000]
Dim $list5 [6] = [\"Select ...\", 10000,20000,30000,40000,50000]
Dim $list6 [6] = [\"Select ...\", 100000,200000,300000,400000,500000]

GUICreate(\"Triple Combo box demo\", 220, 220) 

; Create first combo box
$c1 = GUICtrlCreateCombo (\"Select ...\", 10,10) ; create first item

GUICtrlSetData($c1,$location[0] & \"|\" & $location[1] & \"|\" & $location[2],\"Select ...\") ; add other items snd set a new default

; Create second and third combo boxes
$c2 = GUICtrlCreateCombo (\"\", 10,35) 
$c3 = GUICtrlCreateCombo (\"\", 10,60) 
GUICtrlSetState($c2, $GUI_DISABLE)
GUICtrlSetState($c3, $GUI_DISABLE)

; Create a label to display results
$output = GUICtrlCreateLabel(\"\", 10, 85, 200, 20, $SS_SUNKEN )
GUISetState ()

$h1 = GUICtrlCreateLabel(\"\", 10, 125, 200, 75, $SS_CENTER )
GUICtrlSetData($h1, \"Demonstrating how to deal with three dependent combo boxes where the list data in combo box 2 and 3 is dependent on the selection within combo box 1 and 2, respectively\")
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE 
ExitLoop

Case $msg = $c1 
$cr = GUICtrlRead($c1) ; read the combo box 1 value
GUICtrlSetState($c2, $GUI_ENABLE)
GUICtrlSetData($c2, \"\") ; clear current value from combo box 2
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
GUICtrlSetState($c3, $GUI_DISABLE)
If $cr = $location[0] Then 
populate($c2, $list1)
ElseIf $cr = $location[1] Then 
populate($c2, $list2)
ElseIf $cr = $location[2] Then 
populate($c2, $list3)
EndIf
Case $msg = $c2 and $cr = $location[0]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"1\" Then 
populate($c3, $list1)
ElseIf $cr2 = \"2\" Then 
populate($c3, $list2)
ElseIf $cr2 = \"3\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"4\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"5\" Then 
populate($c3, $list5)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c2 and $cr = $location[1]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"10\" Then 
populate($c3, $list2)
ElseIf $cr2 = \"20\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"30\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"40\" Then 
populate($c3, $list5)
ElseIf $cr2 = \"50\" Then 
populate($c3, $list6)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c2 and $cr = $location[2]
$cr2 = GUICtrlRead($c2) ; read the combo box 2 value
GUICtrlSetState($c3, $GUI_ENABLE)
GUICtrlSetData($c3, \"\") ; clear current value from combo box 3
If $cr2 = \"100\" Then 
populate($c3, $list3)
ElseIf $cr2 = \"200\" Then 
populate($c3, $list4)
ElseIf $cr2 = \"300\" Then 
populate($c3, $list5)
ElseIf $cr2 = \"400\" Then 
populate($c3, $list6)
ElseIf $cr2 = \"500\" Then 
populate($c3, $list1)
EndIf
GUICtrlSetState($c2, $GUI_DISABLE)
Case $msg = $c3
$cr3 = GUICtrlRead($c3)
GUICtrlSetData($output, $cr3)
GUICtrlSetState($c3, $GUI_DISABLE)
EndSelect 
Wend

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Populate a combo box with list data
Func populate($ctrl, $l) 
GUICtrlSetData($ctrl, $l[0], $l[0]) ; put the first item in the list
For $a = 1 to UBound($l) - 1
_GUICtrlComboBox_AddString($ctrl,$l[$a]) ; add all other items to the list
Next 
EndFunc

To gseller: Thanks, that's just what I needed to know.

To HAL9000: Try removing the back slashes from the quoted strings ...

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