How can I set the focus in my GUI on a specific button after a submit? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Rosto
Posts: 30
Joined: 06 Jan 2016, 08:54

How can I set the focus in my GUI on a specific button after a submit?

24 Apr 2023, 08:03

I have a GUI (MoveM) with some buttons on it to move other windows on my screen.

After clicking on a button:

Code: Select all

MoveM.Submit(0)
MoveM.Minimize()

; the WinMove(....) code

MoveM.Show("Restore")
Everything works fine except that the last clicked button Focus is not updated

How can I set the focus in my GUI on a specific button after a submit() ?

e.g. my last clicked button is "H_L_12"
How can I set the focus on button "H_L_12"?

I tried this without success:

Code: Select all

ControlFocus("H_L_12", MoveM)
Rosto
Posts: 30
Joined: 06 Jan 2016, 08:54

Re: How can I set the focus in my GUI on a specific button after a submit?

24 Apr 2023, 08:13

I tried this also without success.

Code: Select all

GuiCtrl.Focus()
GuiCtrl.Focus("H_L_12")
Still the same error:
This variable appears to never be assigned a value
User avatar
mikeyww
Posts: 27188
Joined: 09 Sep 2014, 18:38

Re: How can I set the focus in my GUI on a specific button after a submit?

24 Apr 2023, 08:26

Code: Select all

#Requires AutoHotkey v2.0
MoveM := Gui(, 'Buttons')
b1    := MoveM.AddButton('w230 Default', 1), b1.OnEvent('Click', btn_Click)
b2    := MoveM.AddButton('wp'          , 2), b2.OnEvent('Click', btn_Click)
b3    := MoveM.AddButton('wp'          , 3), b3.OnEvent('Click', btn_Click)
MoveM.Show

btn_Click(btn, info) {
 MsgBox btn.Text, 'You pressed', 64
 b3.Opt('+Default')
 b3.Focus
}
Rosto
Posts: 30
Joined: 06 Jan 2016, 08:54

Re: How can I set the focus in my GUI on a specific button after a submit?

24 Apr 2023, 08:47

mikeyww wrote:
24 Apr 2023, 08:26

Code: Select all

#Requires AutoHotkey v2.0
MoveM := Gui(, 'Buttons')
b1    := MoveM.AddButton('w230 Default', 1), b1.OnEvent('Click', btn_Click)
b2    := MoveM.AddButton('wp'          , 2), b2.OnEvent('Click', btn_Click)
b3    := MoveM.AddButton('wp'          , 3), b3.OnEvent('Click', btn_Click)
MoveM.Show

btn_Click(btn, info) {
 MsgBox btn.Text, 'You pressed', 64
 b3.Opt('+Default')
 b3.Focus
}
Thank you for your answer.
I create my GUI dynamically.
This is a part of my code:

Code: Select all

    for j, m in n {
        if j > 2
        {
           label := StartVal . m
           default := MFocusLast = label ? "Default" : ""
           link := MoveM.Add("Button", "x+5 " . "v" . label . " " . default, m)
           link.OnEvent("Click", MoveStarter.Bind(label, "Normal"))
        }
    }
I read MFocusLast value from an ini file.
And when I close the GUI, MFocusLast value is written in an ini file.

I had no problems when I reload the GUI every time after clicking a button, but I decided not to reload the script every time, but minimize and restore the GUI, but MFocusLast is no longer updated every time.
User avatar
mikeyww
Posts: 27188
Joined: 09 Sep 2014, 18:38

Re: How can I set the focus in my GUI on a specific button after a submit?  Topic is solved

24 Apr 2023, 09:13

Code: Select all

#Requires AutoHotkey v2.0
n          := [1, 2, 3, 4]
StartVal   := 'a'
MFocusLast := StartVal 3
link       := Map()
MoveM      := Gui(, 'Test'), MoveM.SetFont('s10')
MoveM.AddText('w230', 'Test')
MoveM.AddButton('wp', 'Button')
For j, m in n
 If j > 2
    label       := StartVal m
  , default     := MFocusLast = label ? 'Default' : ''
  , link[label] := MoveM.AddButton('x+5 ' default, m)
  , link[label].OnEvent('Click', MoveStarter.Bind(label, 'Normal'))
MoveM.Show

MoveStarter(label, mode, btn, info) {
 saved := MoveM.Submit(False)
 link['a4'].Opt('+Default')
 link['a4'].Focus()
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], CoffeeChaton, Draken, Giresharu, Google [Bot], mikeyww, songdg and 57 guests