Jump to content

Converting a control name into a GUI reference.


Recommended Posts

Here's a code fragment. The three input filelds are implictly named Edit1, Edit2, and Edit3 respectively.

$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25)
$userName = GUICtrlCreateInput("sa", 116, 60, 200, 25)
$password = GUICtrlCreateInput("", 116, 92, 200, 25)
...
...

ControlGetFocus returns the name of the field (ClassNameNN) and depending on which field has the focus, I'll get something like Edit2 for example.

...
$control = ControlGetFocus($mainForm)
...
...

If I later change the order of the fields in the form or add new ones inbetween or delete fields the names will be different! Is is possible to get the GUI of the field named, say, Edit8 if there is one? This way, if I have code that wants to know which control has the focus I can use the GUI rather than a variable control name.

Link to comment
Share on other sites

Let's say I have the 3 fields. The order of creation and the implicit control names will be:

$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25); This is Edit1.
$userName = GUICtrlCreateInput("sa", 116, 60, 200, 25); This is Edit2.
$password = GUICtrlCreateInput("", 116, 92, 200, 25); This is Edit3.

However, if I were to rearrange the order of creation to this:

$userName = GUICtrlCreateInput("sa", 116, 60, 200, 25); This is Edit1.
$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25); This is Edit2.
$password = GUICtrlCreateInput("", 116, 92, 200, 25); This is Edit3.

Note that the control names are now completely different. Where $userName was Edit2 in the first example, it would now be Edit1 in the second example.

My question is how can I cast, say Edit2 into the GUI of the control itself? Or another way, how can I get the control name using the GUI? Providing I don't change the order the controls are created I will know that $userName = Edit1 = $userName. ControlGetFocus seems to be unique in that it returns the ClassNameNN and not the GUI. If I want to manipulate a field in other ways (font, etc) then those calls need the GUI and not the Edit1, Button20, Label25 nomenclature.

Link to comment
Share on other sites

  • Moderators

Let's say I have the 3 fields. The order of creation and the implicit control names will be:

$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25); This is Edit1.
$userName = GUICtrlCreateInput("sa", 116, 60, 200, 25); This is Edit2.
$password = GUICtrlCreateInput("", 116, 92, 200, 25); This is Edit3.

However, if I were to rearrange the order of creation to this:

$userName = GUICtrlCreateInput("sa", 116, 60, 200, 25); This is Edit1.
$instance = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25); This is Edit2.
$password = GUICtrlCreateInput("", 116, 92, 200, 25); This is Edit3.

Note that the control names are now completely different. Where $userName was Edit2 in the first example, it would now be Edit1 in the second example.

My question is how can I cast, say Edit2 into the GUI of the control itself? Or another way, how can I get the control name using the GUI? Providing I don't change the order the controls are created I will know that $userName = Edit1 = $userName. ControlGetFocus seems to be unique in that it returns the ClassNameNN and not the GUI. If I want to manipulate a field in other ways (font, etc) then those calls need the GUI and not the Edit1, Button20, Label25 nomenclature.

You can't, that's the way it is by design. Input/Edit Controls are Edit1, Edit2, etc... by order of creation, Buttons are Button1, Button2 etc... by order of creation, Combo's are ComboBox1, ComboBox2, etc... by order of creation, having this knowledge leaves you in complete control.

Edit:

I should say... Unless you delete the control, and then try to recreate another!!, then it's easy to get lost!

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Note that the control names are now completely different. Where $userName was Edit2 in the first example, it would now be Edit1 in the second example.

this should not matter... the control with have the same names throught the script

if you want to know what was typed into that control ( as you call edit )

just read the control name

GUICtrlRead($Username)

hardly anyone here would choose to use classnames within a script...

why would you want to??

if you can create an example that would allow a classname control to do something we cannot do with a control name within the script....

then we can see your reasoning

8)

NEWHeader1.png

Link to comment
Share on other sites

this should not matter... the control with have the same names throught the script

if you want to know what was typed into that control ( as you call edit )

just read the control name

GUICtrlRead($Username)

hardly anyone here would choose to use classnames within a script...

why would you want to??

if you can create an example that would allow a classname control to do something we cannot do with a control name within the script....

then we can see your reasoning

8)

My original objective was to do specific actions when a field gets the focus. If I have a button that when I tab off it to the next control I might end up on input field1.

1. If the script is changed then tabbing from the same button might go to input field2.

2. If I tab to field1 I might just want to position the cursor to the end of the text it contains.

3. If it was field2 I might want to highlight all the text.

4. If it was field3 I might want to go to the beginning of the text.

But if the controls are created in a different order I might go to the beginning of field1 and not the end as I intended.

Put another way, ControlGetFocus tells me if it is an Edit button but not which one. As I imply, if I can keep the order of the controls exactly the same then there's no problem. It's only when things get moved around in creation order, deleted, or new controls inserted that Edit4 becomes something else to what it was before the script was edited.

Link to comment
Share on other sites

  • Moderators

Put another way, ControlGetFocus tells me if it is an Edit button but not which one. As I imply, if I can keep the order of the controls exactly the same then there's no problem. It's only when things get moved around in creation order, deleted, or new controls inserted that Edit4 becomes something else to what it was before the script was edited.

That's incorrect, it does tell you which Edit it is: .. Ie.. Edit1 = First Edit control created, Edit2 = Second Edit Control Created, etc, etc, and so on.

Use AutoInfo.exe to show you this to be fact. Or create your own:

While 1
    $WindowName = WinGetTitle('')
    ToolTip(ControlGetFocus($WindowName))
    Sleep(100)
Wend

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

i like your idea...

but there are too many "I might" for me to help you

good luck.. seriously

8)

That's my concern as well. I think I need to simplify the behavior and reduce the expectations of the administrators who will want to use the script I'm developing. If I assume the controls will be created in a precise order then all is well. What could snafu the works is if someone else makes a change to the script and does not consider the creation order if it was previously an important factor. Right now, I'll adopt the K.I.S.S approach.

Link to comment
Share on other sites

That's incorrect, it does tell you which Edit it is: .. Ie.. Edit1 = First Edit control created, Edit2 = Second Edit Control Created, etc, etc, and so on.

Use AutoInfo.exe to show you this to be fact. Or create your own:

While 1
    $WindowName = WinGetTitle('')
    ToolTip(ControlGetFocus($WindowName))
    Sleep(100)
Wend
I understand what it tells me and there's no problem with that. But if I tab into an input field, is Edit1 referring to $x one moment and then if the script input fields are edited and rearranged, Edit1 is pointing to $y the next time it's run? It all comes down to being able to change attributes of specific input fields.

An analogy: I park 5 cars in a line in the lot. I know a dirty green car is in the middle as that's how I parked them. Someone asks where is the car that needs a wash. I tell them it's in the middle. A while later someone else rearranges the cars so the green car is at the left. I'm asked, where is the car that was washed? I tell them where I think it is - in the middle!

Link to comment
Share on other sites

  • Moderators

I understand what it tells me and there's no problem with that. But if I tab into an input field, is Edit1 referring to $x one moment and then if the script input fields are edited and rearranged, Edit1 is pointing to $y the next time it's run? It all comes down to being able to change attributes of specific input fields.

An analogy: I park 5 cars in a line in the lot. I know a dirty green car is in the middle as that's how I parked them. Someone asks where is the car that needs a wash. I tell them it's in the middle. A while later someone else rearranges the cars so the green car is at the left. I'm asked, where is the car that was washed? I tell them where I think it is - in the middle!

Couldn't you just put all the edit controls into an array at the start of the script and do it like that? I think maybe that's the approach I would take if I needed it to be specific.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Good point Ron...

thats what i would do also

8)

The array is a step in the right direction as it makes it easier to group the controls into an easy to get at shopping basket. How do you suggest I go about the array?

Am I doing nothing more complex than using an array of control ids? Just thinking aloud here, something like (syntax uncertain):

$x[3]
$x[1] = GUICtrlCreateInput("MSDE01", 116, 28, 200, 25); This is Edit1.
$x[2] = GUICtrlCreateInput("sa", 116, 60, 200, 25); This is Edit2.
$x[3] = GUICtrlCreateInput("", 116, 92, 200, 25); This is Edit3.

???

Or do you have another idea in mind? I've not used the arrays in AutoIT yet (it's barely a week since I discovered it!) :">

Link to comment
Share on other sites

thats the right idea... how ever

i prefer to "DIM" ... thats globally stating the array

Dim $Control_[10] ; or how many you want

example

#include <GUIConstants.au3>
#include <Date.au3>

Dim $Header_1 = "Computer"
Dim $Header_2 = "Phone"
Dim $Header_3 = "Office"
Dim $Name, $TotalTime, $Unit, $count
Dim $TimerActive_[50], $Label_[50], $TButton_[50], $SButton_[50], $Input_[50], $Label_[50]
Dim $Time_[50], $Timer_[50], $sTime_[50], $xk, $ck, $tk, $pk, $Left = 0, $Top = 20, $X, $Radx, $Rad_[11]

$Today_File = @MON & "-" & @MDAY & "-" & @YEAR & ".txt"
$Log = FileOpen($Today_File, 1)
FileWriteLine($Log, "Logfile started: " & _DateTimeFormat( _NowCalc(), 0) & @CRLF & @CRLF)
FileClose($Log)

$Toy_Logo = @TempDir & "\Toy2-logo.jpg"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box2-jpg.jpg", $Toy_Logo)
$Toy_Banner = @TempDir & "\Toy-banr.jpg"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box-jpg.jpg", $Toy_Banner)
$Logo_icon = @TempDir & "\Toy-Icon.ico"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box-Icon.ico", $Logo_icon)

Setup()

AdlibEnable("AllTimers", 500)

GUICreate("   Toy BOX  -  Multi-Station-Timer", (110 * $pk), 470)
GUISetIcon($Logo_icon)


;Top computer label
GUICtrlCreateLabel($Header_1 & "s", 0, 0, (110 * $pk), 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

;Middle phones label
GUICtrlCreateLabel($Header_2 & "s", 0, 220, (110 * $pk), 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

;Bottom PC label
GUICtrlCreateLabel($Header_3 & "s", 0, 340, (110 * $pk), 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

For $X = 1 To $pk
; row 1
    GUICtrlCreateLabel($Header_1 & " " & $X, $Left, $Top, 100, 20, $SS_CENTER)
    $Label_[$X] = GUICtrlCreateLabel("", $Left, $Top + 20, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[$X], 16)
    GUICtrlSetData($Label_[$X], "00:00:00")
    $TButton_[$X] = GUICtrlCreateButton("Start", $Left, $Top + 50, 50, 20)
    $SButton_[$X] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 50, 50, 20)
    $Input_[$X] = GUICtrlCreateInput("", $Left, $Top + 70, 100, 20, 0x1000)
;MsgBox(0,$pk, $Radx & @CRLF & $count)
; row 2
    GUICtrlCreateLabel($Header_1 & " " & ($X + (($count + 1) * .25)), $Left, $Top + 100, 100, 20, $SS_CENTER)
    $Label_[ ($X + (($count + 1) * .25)) ] = GUICtrlCreateLabel("", $Left, $Top + 120, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[ ($X + (($count + 1) * .25)) ], 16)
    GUICtrlSetData($Label_[ ($X + (($count + 1) * .25)) ], "00:00:00")
    $TButton_[ ($X + (($count + 1) * .25)) ] = GUICtrlCreateButton("Start", $Left, $Top + 150, 50, 20)
    $SButton_[ ($X + (($count + 1) * .25)) ] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 150, 50, 20)
    $Input_[ ($X + (($count + 1) * .25)) ] = GUICtrlCreateInput("", $Left, $Top + 170, 100, 20, 0x1000)
    
; row 3
    GUICtrlCreateLabel($Header_2 & " " & $X, $Left, $Top + 220, 100, 20, $SS_CENTER)
    $Label_[ ($X + (($count + 1) * .5)) ] = GUICtrlCreateLabel("", $Left, $Top + 240, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[ ($X + (($count + 1) * .5)) ], 16)
    GUICtrlSetData($Label_[ ($X + (($count + 1) * .5)) ], "00:00:00")
    $TButton_[ ($X + (($count + 1) * .5)) ] = GUICtrlCreateButton("Start", $Left, $Top + 270, 50, 20)
    $SButton_[ ($X + (($count + 1) * .5)) ] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 270, 50, 20)
    $Input_[ ($X + (($count + 1) * .5)) ] = GUICtrlCreateInput("", $Left, $Top + 290, 100, 20, 0x1000)
    
; row 4
    
    If ($X + (($count + 1) * .75)) = ($count + 1) Then
        $Icon_1 = GUICtrlCreatePic($Toy_Banner, $Left, $Top + 360, 100, 50)
        GUICtrlSetState(-1, $GUI_DISABLE)
        ExitLoop
    EndIf
    GUICtrlCreateLabel($Header_3 & " " & $X, $Left, $Top + 340, 100, 20, $SS_CENTER)
    $Label_[ ($X + (($count + 1) * .75)) ] = GUICtrlCreateLabel("", $Left, $Top + 360, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[ ($X + (($count + 1) * .75)) ], 16)
    GUICtrlSetData($Label_[ ($X + (($count + 1) * .75)) ], "00:00:00")
    $TButton_[ ($X + (($count + 1) * .75)) ] = GUICtrlCreateButton("Start", $Left, $Top + 390, 50, 20)
    $SButton_[ ($X + (($count + 1) * .75)) ] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 390, 50, 20)
    $Input_[ ($X + (($count + 1) * .75)) ] = GUICtrlCreateInput("", $Left, $Top + 410, 100, 20, 0x1000)
    
    $Left = $Left + 110
Next

GUISetState()

While 1
    $msg = GUIGetMsg()
    
    For $xk = 1 To $count
        If $msg = $TButton_[$xk] And GUICtrlRead($Input_[$xk]) > "" Then
            GUICtrlSetState($TButton_[$xk], $GUI_DISABLE)
            GUICtrlSetState($Input_[$xk], $GUI_DISABLE)
            $TimerActive_[$xk] = 1
            $Timer_[$xk] = TimerInit()
        ElseIf $msg = $TButton_[$xk] Then
            MsgBox(64, "User Error:  " & $xk, "Please Type in a User Name   ", 3)
        EndIf
        If $msg = $SButton_[$xk] And GUICtrlRead($SButton_[$xk]) = "Reset" Then
            GUICtrlSetData($Label_[$xk], "00:00:00")
            GUICtrlSetData($Input_[$xk], "")
            GUICtrlSetData($SButton_[$xk], "Stop")
            GUICtrlSetState($TButton_[$xk], $GUI_ENABLE)
            GUICtrlSetState($Input_[$xk], $GUI_ENABLE)
        EndIf
        If $msg = $SButton_[$xk] And GUICtrlRead($Input_[$xk]) > "" And GUICtrlRead($SButton_[$xk]) = "Stop" Then
            $TimerActive_[$xk] = 0
            GUICtrlSetData($SButton_[$xk], "Reset")
            
        ;
            GUICtrlSetColor($Label_[$xk], 0x000000)
            $Name = GUICtrlRead($Input_[$xk])
        ;
            If $xk <= (($count + 1) * .5) Then
                $Unit = $Header_1 & "  " & $xk
            EndIf
            If $xk > (($count + 1) * .5) And $xk <= (($count + 1) * .75) Then
                $Unit = $Header_2 & "   " & ($xk - (($count + 1) * .5))
            EndIf
            If $xk > (($count + 1) * .75) Then
                $Unit = $Header_3 & "   " & ($xk - (($count + 1) * .75))
            EndIf
            RecordStuff()
        EndIf
    Next
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func AllTimers()
    Local $Secs, $Mins, $Hour
    For $ck = 1 To $count
        
        If $TimerActive_[$ck] Then
            _TicksToTime(Int(TimerDiff($Timer_[$ck])), $Hour, $Mins, $Secs)
            $Time_[$ck] = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
            If $sTime_[$ck] <> $Time_[$ck] Then GUICtrlSetData($Label_[$ck], $Time_[$ck])
            If $Mins > 29 Then GUICtrlSetColor($Label_[$ck], 0xff0000)
        EndIf
    Next
EndFunc  ;==>AllTimers

Func RecordStuff()
    $LogStuff = FileOpen($Today_File, 1)
    FileWriteLine($LogStuff, "Station: " & $Unit & @CRLF)
    FileWriteLine($LogStuff, "User name: " & (GUICtrlRead($Input_[$xk])) & @CRLF)
    FileWriteLine($LogStuff, "Total time: " & (GUICtrlRead($Label_[$xk])) & @CRLF & @CRLF)
    FileClose($LogStuff)
    
EndFunc  ;==>RecordStuff

Func Setup()
    $Set_win = GUICreate("   Toy BOX  -  Multi-Station-Timer", 350, 310)
    GUISetIcon($Logo_icon)
    $Icon_1 = GUICtrlCreatePic($Toy_Logo, 240, 10, 100, 250)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateGroup("3 Group Names", 10, 10, 220, 135)
    $Head_1 = GUICtrlCreateInput($Header_1, 30, 40, 150, 20)
    $Head_2 = GUICtrlCreateInput($Header_2, 30, 75, 150, 20)
    $Head_3 = GUICtrlCreateInput($Header_3, 30, 110, 150, 20)
    GUICtrlCreateGroup("Number of Timers", 10, 155, 220, 150)
    $Note = GUICtrlCreateLabel("19 Total Timers", 30, 175, 150, 20, $SS_SUNKEN + $SS_CENTER)
    $Rad_[1] = GUICtrlCreateRadio("3", 40, 200, 30, 20)
    $Rad_[2] = GUICtrlCreateRadio("7", 40, 220, 30, 20)
    $Rad_[3] = GUICtrlCreateRadio("11", 40, 240, 30, 20)
    $Rad_[4] = GUICtrlCreateRadio("15", 40, 260, 30, 20)
    $Rad_[5] = GUICtrlCreateRadio("19", 40, 280, 30, 20)
    GUICtrlSetState(-1, $GUI_CHECKED)
    $Rad_[6] = GUICtrlCreateRadio("23", 140, 200, 30, 20)
    $Rad_[7] = GUICtrlCreateRadio("27", 140, 220, 30, 20)
    $Rad_[8] = GUICtrlCreateRadio("31", 140, 240, 30, 20)
    $Rad_[9] = GUICtrlCreateRadio("35", 140, 260, 30, 20)
    $Rad_[10] = GUICtrlCreateRadio("39", 140, 280, 30, 20)
    $Create = GUICtrlCreateButton("Create", 250, 270, 80, 30)
    
    GUISetState()
    
    While 1
        $msg2 = GUIGetMsg()
        If $msg2 = $Create Then
            $Header_1 = GUICtrlRead($Head_1)
            $Header_2 = GUICtrlRead($Head_2)
            $Header_3 = GUICtrlRead($Head_3)
            For $tk = 1 To 10
                If GUICtrlRead($Rad_[$tk]) = $GUI_CHECKED Then
                    $pk = $tk
                    $Radx = $tk * 2
                    $count = (($tk * 4) - 1)
                    ExitLoop
                EndIf
            Next
            ExitLoop
        EndIf
        For $t = 1 To 10
            If GUICtrlRead($Rad_[$t]) = $GUI_CHECKED Then
                GUICtrlSetData( $Note, (($t * 4) - 1) & " Total Timers")
                ExitLoop
            EndIf
        Next
        If $msg2 = $GUI_EVENT_CLOSE Then ExitLoop
        Sleep(70)
    WEnd
    
    GUIDelete($Set_win)
EndFunc  ;==>Setup

8)

NEWHeader1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...