How do I change dropdown list options based on a radio button I select?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

How do I change dropdown list options based on a radio button I select?

Post by alpha-centauri » 08 Feb 2023, 04:40

Hey guys in this code I have a T1, T2 and T3 as 3 different radio buttons, how do I make it so that the alphaSpecList drop down changes according to the radio button that is selected?

Code: Select all

myGui := Gui()
myGuiTabs := myGui.Add("Tab3",, ["ALPHA", "BETA", "GAMMA"])
myGuiTabs.UseTab(1)
    myGui.Add("GroupBox","Section w180 r3.25","Type")        
        myT1 := myGui.Add("Radio", "vRadio1 xs+10 ys+20", "T1")
        myT2 := myGui.Add("Radio", "vRadio2 xs+10 ys+40", "T2")
        myT3 := myGui.Add("Radio", "vRadio3 xs+10 ys+60", "T3")
    myGui.Add("Text", ,"Specification List")
    if(myT1.Value==1){
        aphaSpecList := myGui.Add("DropDownList", "vAlphaSpec xs+10 w100", [
           "Spec1",
           "Spec2" 
        ])
    }

    if (myT2.Value == 1){
        alphaSpecList := myGui.Add("DropDownList", "vAlphaSpec xs+10 w100", [
            "Spec3",
            "Spec4"
         ])
    }
    if (myT3.Value == 1) {
        aplhaSpecList := myGui.Add("DropDownList", "vAlphaSpec xs+10 w100", [
            "Spec5",
            "Spec6"
        ])
    }
::my-make:: {
    myGui.Show()
}

User avatar
mikeyww
Posts: 26598
Joined: 09 Sep 2014, 18:38

Re: How do I change dropdown list options based on a radio button I select?

Post by mikeyww » 08 Feb 2023, 06:20

See: https://www.autohotkey.com/docs/v2/lib/GuiControl.htm#Add
Add: Appends list items to a ListBox, DropDownList or ComboBox, or tabs to a Tab control.

Code: Select all

; This script alters a dropdown list according to a radio button
#Requires AutoHotkey v2.0
list := [[1, 2, 3], [4, 5, 6]]
gui1 := Gui(, 'Test'), gui1.SetFont('s10')
rad1 := gui1.AddRadio(, 'R1'), rad1.OnEvent('Click', change)
rad2 := gui1.AddRadio(, 'R2'), rad2.OnEvent('Click', change)
ddl  := gui1.AddDropDownList(, list[1])
gui1.Show

change(btn, info) {
 ddl.Delete
 ddl.Add(list[2 - rad1.Value])
}

alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

Re: How do I change dropdown list options based on a radio button I select?

Post by alpha-centauri » 08 Feb 2023, 07:09

@mikeyww
Hey man thanks for the help. This gave me some clarity regarding what I need to do. The script you provided seems to work with two lists and I have three so I just made separate OnEvent classes for each radio button, so it looks something like this:

Code: Select all

#Requires AutoHotkey v2.0
list := [[1, 2, 3], [4, 5, 6],[7,8,9]]
gui1 := Gui(, 'Test'), gui1.SetFont('s10')
rad1 := gui1.AddRadio(, 'R1'), rad1.OnEvent('Click', change1)
rad2 := gui1.AddRadio(, 'R2'), rad2.OnEvent('Click', change2)
rad3 := gui1.AddRadio(, 'R3'), rad3.OnEvent('Click', change3)
ddl  := gui1.AddDropDownList(, list[1])
gui1.Show

change1(btn, info) {
 ddl.Delete
 ddl.Add(list[1])
}

change2(btn, info) {
 ddl.Delete
 ddl.Add(list[2])
}

change3(btn, info) {
 ddl.Delete
 ddl.Add(list[3])
}

User avatar
mikeyww
Posts: 26598
Joined: 09 Sep 2014, 18:38

Re: How do I change dropdown list options based on a radio button I select?

Post by mikeyww » 08 Feb 2023, 07:26

That works. An alternative is submitting the GUI and using a single function to process the results, or the individual buttons can also be assessed within one function.

Post Reply

Return to “Ask for Help (v2)”