[Gui] Simple Inputbox with DropDownList

Put simple Tips and Tricks that are not entire Tutorials in this forum
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

[Gui] Simple Inputbox with DropDownList

09 Apr 2023, 16:49

This is a simple example how to make a function that displays a Gui and returns a value, similar to an inputbox command, always usefull.
if cancelled, the return value will be "Cancel".

The same concept can be used for every control, or multiple controls (it that case, I would return an object containing the values.)

If somebody has a different approach, please share it, I use an object to be able to modify the return value when cancelling, but maybe there is a more proper way.

Code: Select all

#Requires AutoHotkey v2.0
InputBoxDropDownList(Prompt,Title?,Options?,aChoices?, VarDefault?){
	oValues := Object() ; allows to return
	DDLGui := Gui(Options ?? Unset, Title ?? Unset)
	(IsSet(Prompt)) ? DDLGui.AddText(,Prompt) : ""
	ogDDL := DDLGui.AddDropDownList("w160 vNewValue", aChoices ?? [] )
	(IsSet(VarDefault)) ? ogDDL.Text := VarDefault : ""
	DDLGui.AddButton("w75","OK").OnEvent("Click", (*)=>(oValues := DDLGui.Submit()))
	DDLGui.AddButton("yp x+10 w75","Cancel").OnEvent("Click", (*)=>(oValues.NewValue:="Cancel", DDLGui.Destroy()))
	DDLGui.OnEvent("Close", (*)=>(oValues.NewValue:="Cancel", DDLGui.Destroy()))
	DDLGui.Show()
	WinWaitClose(DDLGui)
	return oValues.NewValue
}
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: [Gui] Simple Inputbox with DropDownList

09 Apr 2023, 18:52

Nice. Very handy!
14.3 & 1.3.7
AlmaAmulek
Posts: 1
Joined: 25 May 2023, 11:00

Re: [Gui] Simple Inputbox with DropDownList

25 May 2023, 11:02

Can you provide an example of using this function?
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: [Gui] Simple Inputbox with DropDownList

25 May 2023, 12:35

I had tested it with this:

Code: Select all

#Requires AutoHotkey v2.0

res := InputBoxDropDownList("Enter something",,"-caption +border",["a","b","c"],"c")			;-caption +border
msgbox res

InputBoxDropDownList(Prompt,Title?,Options?,aChoices?, VarDefault?)
	{
	oValues := Object() 																		; allows to return
	DDLGui := Gui(Options ?? Unset, Title ?? Unset)
	(IsSet(Prompt)) ? DDLGui.AddText(,Prompt) : ""
	ogDDL := DDLGui.AddDropDownList("w160 vNewValue", aChoices ?? [] )
	(IsSet(VarDefault)) ? ogDDL.Text := VarDefault : ""
	DDLGui.AddButton("w75","OK").OnEvent("Click", (*)=>(oValues := DDLGui.Submit()))
	DDLGui.AddButton("yp x+10 w75","Cancel").OnEvent("Click", (*)=>(oValues.NewValue:="Cancel", DDLGui.Destroy()))
	DDLGui.OnEvent("Close", (*)=>(oValues.NewValue:="Cancel", DDLGui.Destroy()))
	DDLGui.Show()
	WinWaitClose(DDLGui)
	return oValues.NewValue
	}
14.3 & 1.3.7

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 76 guests