Can't figure out declaring and using variables with GUI

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
tedfido
Posts: 4
Joined: 28 Jan 2023, 20:29

Can't figure out declaring and using variables with GUI

Post by tedfido » 28 Jan 2023, 20:35

I'm trying to write a script using a GUI that will open a browser tab with the text you put into the gui text box. Here's my code so far.

Code: Select all

go := (*) => ExitApp()

; I didn't include the screen variables

myGui := Gui()
myGui.Opt("+Resize AlwaysOnTop")
myGui.Add("Text",, "Scan QR Code" screenWidth " " screenHeight)
myGui.Add("Edit", "w250 vUrlVar")
myGui.Add("Button", "Default Hidden")

myGui.OnEvent("Close", go)
myGui.OnEvent("Click", UrlHandler())

myGui.Show("X" guiX "Y" guiY "W" guiWidth "H" guiHeight)

UrlHandler(*)
{
    Run("UrlVar")
}

I'm getting an error on 029: Run("UrlVar") saying the system cannot find the file specified, but I can't tell if any of my variables are working. Can I get help on this? :happybday:

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

Re: Can't figure out declaring and using variables with GUI

Post by mikeyww » 28 Jan 2023, 21:54

Functions use expressions. Here is an example with both a quoted string and a variable.

Code: Select all

#Requires AutoHotkey v2.0
myGui := Gui("+Resize AlwaysOnTop")
myGui.Add("Text",, "Scan QR Code")
ed  := myGui.Add("Edit"  , "w250")
btn := myGui.Add("Button", 'Default', 'Go')
btn.OnEvent("Click", UrlHandler)
myGui.Show

UrlHandler(*) {
 myGui.Submit
 Run('chrome.exe ' ed.Value)
}

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Can't figure out declaring and using variables with GUI

Post by just me » 29 Jan 2023, 06:02

If you still want to use named controls in v2:

Code: Select all

go := (*) => ExitApp()
; I didn't include the screen variables

myGui := Gui("+Resize +AlwaysOnTop", "My GUI")
myGui.OnEvent("Close", go)
myGui.Add("Text",, "Scan QR Code")
myGui.Add("Edit", "w250 vUrlVar")
myGui.Add("Button", "Default Hidden vBtnApply").OnEvent("Click", UrlHandler)
myGui.Show()

UrlHandler(*) {
   myGui.Opt("+OwnDialogs")
   MsgBox(myGui["UrlVar"].text) ; instead of Run
}

tedfido
Posts: 4
Joined: 28 Jan 2023, 20:29

Re: Can't figure out declaring and using variables with GUI

Post by tedfido » 29 Jan 2023, 13:47

Thanks for the help, I went with Mikeys answer as it seems cleaner and it works!!
Now I have another question since the GuiControl concept is a bit confusing. I'm trying to update text on my Gui to show the latest edit value if it's not a valid url, but it won't refresh. I read that GuiControl is good for that but can't get it right.

Code: Select all

UrlHandler(*)
{
    myGui.Submit(0)
    if InStr(ed.Value, ".com") || InStr(ed.Value, "www.") || InStr(ed.Value, "http") || InStr(ed.Value, "https")
        Run(ed.Value)
    else
        err := myGui.Add("Text",, "Invalid Url:" ed.Value)
        GuiControl.err := ed.Value
}

tedfido
Posts: 4
Joined: 28 Jan 2023, 20:29

Re: Can't figure out declaring and using variables with GUI

Post by tedfido » 29 Jan 2023, 13:50

Upon testing, my edit line won't submit a second time if it's a valid URL either. Is the best option to just rebuild the Gui after every submit?

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

Re: Can't figure out declaring and using variables with GUI

Post by mikeyww » 29 Jan 2023, 16:28

I don't have a scanner, but this works for typing.

Code: Select all

; This script provides an input box for a URL
#Requires AutoHotkey v2.0
gui1 := Gui("+Resize +AlwaysOnTop", 'Navigate to URL')
gui1.SetFont('s10')
gui1.AddText(, "Scan QR Code")
ed := gui1.AddEdit("w250")
gui1.AddButton('wp Default', 'Go').OnEvent("Click", (*) => (SoundBeep(1500), Run('chrome.exe ' ed.Value)))
gui1.Show

tedfido
Posts: 4
Joined: 28 Jan 2023, 20:29

Re: Can't figure out declaring and using variables with GUI

Post by tedfido » 29 Jan 2023, 20:48

This works for me, the only problem is trying to add error messages, I can't update the err variable, it just adds a new line below it.

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

Re: Can't figure out declaring and using variables with GUI

Post by mikeyww » 29 Jan 2023, 20:59

My script has no such variable.

Post Reply

Return to “Ask for Help (v2)”