Page 1 of 1

[Gui] Simple Inputbox with DropDownList

Posted: 09 Apr 2023, 16:49
by AHK_user
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
}

Re: [Gui] Simple Inputbox with DropDownList

Posted: 09 Apr 2023, 18:52
by flyingDman
Nice. Very handy!

Re: [Gui] Simple Inputbox with DropDownList

Posted: 25 May 2023, 11:02
by AlmaAmulek
Can you provide an example of using this function?

Re: [Gui] Simple Inputbox with DropDownList

Posted: 25 May 2023, 12:35
by flyingDman
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
	}