Switching to v2 made such a huge difference for me when it comes to creating Guis. I find it much cleaner looking and easier to read. I'm learning by searching for Gui questions/requests. Here is yours in v2.
Code: Select all
#Requires Autohotkey v2
#SingleInstance Force ; prevents an error message if you run it again
Names := ["Aron", "Billy", "Caitlyn", "Donna"] ; create an array of names
Fruits := ["Apple", "Banana", "Mango", "Orange"] ; create an array of fruits
; Create the Gui ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bananaGui := Gui() ; Call the Gui object
bananaGui.Title := "Fruits and Hoes" ; name the window
bananaGui.AddText(, "Name:") ; add column of text controls
bananaGui.AddText(, "Name:")
bananaGui.AddText(, "Fruits:")
EditName := bananaGui.AddEdit("ym vMyName") ; start a new column with ym - notice these controls seem to have 2 names - this will come into play later
DropDownListNames := bananaGui.AddDropDownList("vThemName", Names) ; create dropdownlists
DropDownListFruits := bananaGui.AddDropDownList("vFruits", Fruits)
ButtonSubmit := bananaGui.AddButton(, "Submit") ; create the submit button
; Create Gui Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DropDownListFruits.OnEvent("Change", FruitsChangeHandler) ; calls the FruitsChangeHandler function whenever a fruit is chosen
ButtonSubmit.OnEvent("Click", SubmitHandler) ; the event for a button is a click
bananaGui.OnEvent("Close", (*) => ExitApp()) ; this event is for the Gui itself, not a control - terminates the script instead of leaving it running hidden
; Gui Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bananaGui.Show ; make Gui visible
; Define your functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FruitsChangeHandler(*)
{
if(DropDownListFruits.Text = "banana") ; disable the other fields when banana is chosen
{
EditName.Enabled := 0
DropDownListNames.Enabled := 0
}
else ; without an else statement the other fields are not reenabled when you choose a different fruit
{
EditName.Enabled := 1
DropDownListNames.Enabled := 1
}
}
SubmitHandler(*)
{
if(EditName.Text = "")
{
MsgBox("Enter a name to continue", "Error!", "iconx")
return
}
Saved := bananaGui.Submit() ; once the Gui is submitted, you start using the vNames
MsgBox "My name is " Saved.MyName "`n" ; sends a message box with a summary of the field values
. "His name is " Saved.ThemName "`n"
. "I love eating " Saved.Fruits
}
This second code is a demonstration of as many guicontrol techniques as I could fit and does a lot more. It may look like a lot but it is just explanation and example of various Gui control techniques. You should also check out EasyAuto Gui if you are interested. It basically makes Guis drag and drop.
Code: Select all
#Requires Autohotkey v2
#SingleInstance Force ; prevents an error message if you run it again
Names := ["Aron", "Billy", "Caitlyn", "Donna"] ; create an array of names
Fruits := ["Apple", "Banana", "Mango", "Orange"] ; create an array of fruits
; Create the Gui ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bananaGui := Gui() ; Call the Gui object
bananaGui.Title := "Fruits and Hoes" ; name the window
bananaGui.AddText(, "Name:") ; add column of text controls
bananaGui.AddText(, "Name:")
bananaGui.AddText(, "Fruits:")
EditName := bananaGui.AddEdit("ym vMyName") ; start a new column with ym - notice these controls seem to have 2 names - this will come into play later
DropDownListNames := bananaGui.AddDropDownList("vThemName", Names) ; create dropdownlists
DropDownListFruits := bananaGui.AddDropDownList("vFruits", Fruits)
ButtonSubmit := bananaGui.AddButton(, "Submit") ; create the submit button
bananaGui.AddText("x5","") ; create a hint section at the bottom - use x5 to align it left instead of with second column - blank text for spacer
bananaGui.AddText("x5","Want to add your own fruit?")
bananaGui.AddText("x5","Hint: try typing 'secret' in the name field")
bananaGui.AddText("x5","")
bananaGui.AddText("x5","Can you unlock the special StarFruit?")
bananaGui.AddText("x5","Hint: check the code!")
; Create events to call a different function when each field is changed ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EditName.OnEvent("Change", NameHandler) ; calls the function NameHandler whenever there is a change to the name edit box
DropDownListNames.OnEvent("Change", NamesChangeHandler)
DropDownListFruits.OnEvent("Change", FruitsChangeHandler)
ButtonSubmit.OnEvent("Click", SubmitHandler) ; the event for a button is a click
bananaGui.OnEvent("Close", (*) => ExitApp()) ; this event is for the Gui itself, not a control - terminates the script instead of leaving it running hidden
; Gui Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bananaGui.Show ; make Gui visible
; Define your functions for each field ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NameHandler(*)
{
; some example if statements for this field
; note not using the vName but the named variable: .Text gets you the text and .Value the index for DDL control
; this function allows the user to enter a custom, "secret" fruit when the name is "secret"
; it then adds the fruit to the dropdownlist - with special handling for "banana"
if(EditName.Text = "Secret")
{
Input := InputBox("Enter your special fruit`n...not banana please!", "Special Fruit") ; prompt user to enter a custom fruit
if(Input.Value = "Banana")
{
MsgBox("How dare you!?") ; if they try to enter bananas send this message box
DropDownListFruits.Text := "banana"
DropDownListFruits.Delete(DropDownListFruits.Value) ; conspicuously deletes banana from the options when hovered over
DropDownListFruits.Add(["Looking for something?"]) ; otherwise add their fruit to the dropdownlist
}
else
{
DropDownListFruits.Add([Input.Value]) ; otherwise add their fruit to the dropdownlist
DropDownListFruits.Text := Input.Value
}
}
if(EditName.Text = "Sorry!") ;
{
try
{
DropDownListFruits.Text := "banana" ; if banana is in the list, changes option to banana -> otherwise throws an error
MsgBox("Cheer up!`n`nHave a banana!", "Yayy bananas!!") ; add a title to this msgbox
}
catch ; catches the error if thrown
{
MsgBox "And let's hope you mean it too!" ; you really better be sorry
DropDownListFruits.Text := "Looking for something?" ; sets dropdownlist to "looking for something"
DropDownListFruits.Delete(DropDownListFruits.Value) ; deletes "looking for something" after setting it
DropDownListFruits.Add(["Banana"]) ; adds banana back to the list
DropDownListFruits.Text := "banana"
}
}
}
NamesChangeHandler(*)
{
; Some example if statements for each value in this field
if(DropDownListNames.Text = "Aron")
{
MsgBox('Here you dropped this:`n`n`t"a"', "A-A-R-O-N", "icon!") ; add a title and an icon to this msgbox
DropDownListNames.Delete(DropDownListNames.Value) ; have to use the index for delete - .value
DropDownListNames.Add(["Aaron"])
DropDownListNames.Text := "Aaron"
}
if(DropDownListNames.Text = "Donna")
{
Result := MsgBox("Donna won't you blow your horn!?", "Feeling Horny?", "YesNo icon?") ; add title, icon and yes/no options to this msgbox
if(Result = "Yes")
{
SoundPlay "*48" ; exclamation sound
}
if(Result := "No")
{
SoundPlay "*16" ; error sound
}
}
if(DropDownListNames.Text = "Billy")
{
MsgBox("You again!?",, "iconx")
}
if(DropDownListNames.Text = "Caitlyn")
{
for value in Fruits ; uses a loop to guess each fruit
{
Result := MsgBox(value "?", "Let me guess?", "YesNo")
if(Result = "No") ; continue to next value if result is no
{
continue
}
if(Result = "Yes") ; stop the loop if the result is yes
{
DropDownListFruits.Text := Value ; sets dropdownlist to chosen value
if(value = "banana")
{
MsgBox("Banana nanna nanna... fee fie foe fanna", "It's Always Banana!")
}
break
}
}
}
}
FruitsChangeHandler(*)
{
; Some example if statements for this field
if(DropDownListFruits.Text = "orange" and EditName.Text = "") ; if name is blank, changes names when orange is chosen
{
EditName.Text := "Donald Trump"
DropDownListNames.Add(["Vladamir Putin"])
DropDownListNames.Text := "Vladamir Putin"
MsgBox("Who coulda seen that comin'?", "World War 3")
}
if(DropDownListFruits.Text = "apple" and EditName.Text = "") ; same for apples
{
EditName.Text := "Adam"
DropDownListNames.Add(["Eve"])
DropDownListNames.Text := "Eve"
MsgBox("You were warned!", "Adam & Eve")
}
if(DropDownListFruits.Text = "banana") ; disable the other fields when banana is chosen
{
EditName.Enabled := 0
DropDownListNames.Enabled := 0
}
else ; without an else statement the other fields are not reenabled when you choose a different fruit
{
EditName.Enabled := 1
DropDownListNames.Enabled := 1
}
if(DropDownListFruits.Text = "mango" and EditName.Text = "secret") ; same for apples
{
EditName.Text := "Adam"
DropDownListFruits.Add(["StarFruit"])
DropDownListFruits.Text := "StarFruit"
MsgBox("You've unlocked the StarFruit!", "Congratulations!")
}
if(DropDownListFruits.Text = "Looking for something?") ; prompt for apology
{
MsgBox("Feeling sorry... want a banana?`n`nHINT: type sorry! in name field", "Congratulations!")
}
}
SubmitHandler(*)
{
if(EditName.Text = "")
{
MsgBox("Enter a name to continue", "Error!", "iconx")
return
}
Saved := bananaGui.Submit() ; once the Gui is submitted, you start using the vNames
MsgBox "My name is " Saved.MyName "`n" ; sends a message box with a summary of the field values
. "His name is " Saved.ThemName "`n"
. "I love eating " Saved.Fruits
for name, value in Saved.OwnProps() ; or you can access the names and values one at a time using .OwnProps
{
MsgBox(name ": " value)
}
If(Saved.Fruits = "banana")
{
FileName := A_Desktop "\Grocery List.txt" ; if the chose fruit is banana, creates a shopping list on the desktop
FileObj := FileOpen(FileName, "w")
FileObj.WriteLine("Grocery List:")
loop 10
{
FileObj.WriteLine("banana")
}
FileObj.Close
}
}