Need help: V2 GUI - variable GuiCtrl

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
miwe-video
Posts: 2
Joined: 09 May 2023, 05:38

Need help: V2 GUI - variable GuiCtrl

Post by miwe-video » 09 May 2023, 06:08

Hi all,

this is my first post here as I did not find any solution for following problem in v2 (which worked in v1):

I have a number (not known at the beginning!) of slider + text elements. Therefore I use a Loop to create those.
I assigned new variables "MySlider%index%" and "MyText%index%" to identify the controls and be able at the end to assign the value in slider to the text element below.

v1:

Code: Select all

shift_right = 3
n = 3
startValue = 12
width = 50

Gui, font, bold s14, Arial
Gui, add, text, section xm ym, Example
Gui, font
Loop, %n%
{
    index := A_Index + startValue - 1
    Gui, font, s12, Arial
    Gui, add, groupbox, xs+%shift_right% ys+30 w58 h280 center, Chn-%index% 
    Gui, font, s12, Arial
    Gui, add, slider, xp+9 yp+20 h270 range0-90 vertical center line1 page10 tickinterval10 AltSubmit vMySlider%index% gsliderFunc, 90
    Gui, font, s14, Arial
    Gui, add, text, xp-8 yp+275 r1 w%width% center vMyText%index%, 90
    shift_right += 65
}

Gui, Submit
Gui, Show
return

GuiClose:
GuiEscape:
ExitApp

sliderFunc()
{
    RegExMatch(A_GuiControl, "MySlider(\d+)", num)
    GuiControlGet, %A_GuiControl%
    valueNew := %A_GuiControl%
    controlNew = MyText%num1%
    GuiControl, ,%controlNew%, %valueNew%
}

In v2 some things can be done easier (e.g. MySlider%index% is not needed anymore, function call, ...), but one of the main points which I do not get to work is the assigment in line 19:

Code: Select all

MyText%index% := myGui.Add("Text", "xp-8 yp+275 r1 w" width " center", "90")
without specifying it as "global" (see line 5, without that it will not work!)
But as said I do not know at the beginning which would be "names" of these variables as "startValue" and "n" are normally derived from a config file and are "variable".

v2 (which will not work unless one uncomment line 5):

Code: Select all

shift_right := 3
width := 50
startValue := 12
n := 3
;global MyText12, MyText13, MyText14

myGui := Gui()
myGui.OnEvent("close",closeGui)
myGui.OnEvent("escape",closeGui)
myGui.SetFont("bold s14", "Arial")
myGui.Add("text", "section xm ym", "Example")
Loop n
{
    index := A_Index + startValue - 1
    myGui.SetFont("s10", "Arial")
    myGui.Add("groupbox", "xs+" shift_right " ys+30 w58 h280 center", "Chn-" index)
    myGui.Add("slider", "xp+9 yp+20 h270 range0-90 vertical center line1 page10 tickinterval10 AltSubmit", "90").OnEvent("change", sliderFunc.Bind(,,index))
    myGui.SetFont("bold s12", "Arial")
    MyText%index% := myGui.Add("Text", "xp-8 yp+275 r1 w" width " center", "90")
    shift_right += 65
}
myGui.Submit()
myGui.Show()

closeGui(*) {
    ExitApp
}

sliderFunc(obj, info, idx) {
    MyText%idx%.Text := obj.Value
}
Anyone out who has a solution for that?


The only solution which came to my mind was to assign the Hwnd of the text element (which is an integer value) to an array (which I can specify).
But maybe there is another solution?

Best regards,
Michael

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

Re: Need help: V2 GUI - variable GuiCtrl

Post by just me » 09 May 2023, 06:59

You still can use named controls:

Code: Select all

...
    myGui.Add("Text", "xp-8 yp+275 r1 w" width " center", "90").Name := "TX" . index
...
sliderFunc(obj, info, idx) {
  myGui["TX" . idx].Text := obj.Value
}

miwe-video
Posts: 2
Joined: 09 May 2023, 05:38

Re: Need help: V2 GUI - variable GuiCtrl

Post by miwe-video » 03 Jun 2023, 04:12

Thanks for the hint!
(Was not aware that *.Name can be used like this ;-) )

And sorry for the late reply.

Post Reply

Return to “Ask for Help (v2)”