Jump to content

Reading the value in a Label GUI


Recommended Posts

I'm trying to grab the string in a Label using GUICtrlRead and a variable. I always come up with nothing, 0 (zero) actually. In this sample code the results are displayed in a Message Box. Should I be using something other then GUICtrlRead? Should I be using something other then a Label?

CODE
#include <GUIConstantsEx.au3>

$Form = GUICreate("Test Form", 270, 149, 200, 202)

Global $Label1 = GUICtrlCreateLabel("Label1", 30, 40, 99, 17)

Global $Label2 = GUICtrlCreateLabel("Label2", 30, 60, 99, 17)

Global $Label3 = GUICtrlCreateLabel("Lable3", 30, 80, 99, 17)

$Button = GUICtrlCreateButton("Test", 190, 60, 61, 21, 0)

$ButtonExit = GUICtrlCreateButton("Exit", 190, 90, 61, 21, 0)

GUISetState(@SW_SHOW, $Form)

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUICtrlSetOnEvent($ButtonExit, "ExitButton")

GUICtrlSetOnEvent($Button, "Buttonclicked")

While 1

Sleep(1000) ; Idle around

WEnd

Func CLOSEClicked()

Exit

EndFunc ;==>CLOSEClicked

Func ExitButton()

Exit

EndFunc ;==>ExitButton

Func Buttonclicked()

For $LoopAround = 1 to 3

$NextLabelVar = "$Label" & $LoopAround

MsgBox(0,"$NextLabelVar = " & $NextLabelVar,"This command (GUICtrlRead($NextLabelVar) = " & (GUICtrlRead($NextLabelVar)))

Next

EndFunc ;==>Buttonclicked

Link to comment
Share on other sites

What your doing is wrong.

$label & "1" is not the same as $label1 im afraid.

Try using arrays:

#include <GUIConstantsEx.au3>
Global $Label[3]
$Form = GUICreate("Test Form", 270, 149, 200, 202)
$Label[0] = GUICtrlCreateLabel("Label1", 30, 40, 99, 17)
$Label[1] = GUICtrlCreateLabel("Label2", 30, 60, 99, 17)
$Label[2] = GUICtrlCreateLabel("Lable3", 30, 80, 99, 17)
$Button = GUICtrlCreateButton("Test", 190, 60, 61, 21, 0)
$ButtonExit = GUICtrlCreateButton("Exit", 190, 90, 61, 21, 0)

GUISetState(@SW_SHOW, $Form)

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($ButtonExit, "ExitButton")
GUICtrlSetOnEvent($Button, "Buttonclicked")

While 1
Sleep(1000) ; Idle around
WEnd

Func CLOSEClicked()
Exit
EndFunc ;==>CLOSEClicked

Func ExitButton()
Exit
EndFunc ;==>ExitButton

Func Buttonclicked()
For $LoopAround = 0 to 2
    MsgBox(0,"$NextLabelVar = " & $label[$LoopAround],"This command (GUICtrlRead($NextLabelVar) = " & (GUICtrlRead($label[$LoopAround])))
Next
EndFunc ;==>Buttonclicked
Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Link to comment
Share on other sites

Thank you, that explains the errors of my ways.

Another way, similar to your first attempt, that doesn't use arrays, but assumes all the labels you're looping through have been created sequentially

#include <GUIConstantsEx.au3>

$Form = GUICreate("Test Form", 270, 149, 200, 202)
Global $Label1 = GUICtrlCreateLabel("Label1", 30, 40, 99, 17)
Global $Label2 = GUICtrlCreateLabel("Label2", 30, 60, 99, 17)
Global $Label3 = GUICtrlCreateLabel("Lable3", 30, 80, 99, 17)
$Button = GUICtrlCreateButton("Test", 190, 60, 61, 21, 0)
$ButtonExit = GUICtrlCreateButton("Exit", 190, 90, 61, 21, 0)

GUISetState(@SW_SHOW, $Form)

Opt("GUIOnEventMode", 1); Change to OnEvent mode
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($ButtonExit, "ExitButton")
GUICtrlSetOnEvent($Button, "Buttonclicked")

While 1
    Sleep(1000); Idle around
WEnd

Func CLOSEClicked()
    Exit
EndFunc  ;==>CLOSEClicked

Func ExitButton()
    Exit
EndFunc  ;==>ExitButton

Func Buttonclicked()
    $i = 1
    For $LoopAround = $Label1 To $Label3
        MsgBox(0, "$NextLabelVar = " & "Label" & $i, "This command (GUICtrlRead(Label" & $i & ") = " & (GUICtrlRead($LoopAround)))
        $i += 1
    Next
EndFunc  ;==>Buttonclicked
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...