Jump to content

Working with Assigned variables and Case


Go to solution Solved by czardas,

Recommended Posts

I am creating a GUI that will have a varying amount of labels on it. I would like to use Assign to give dynamic variables to the labels. When it comes to to the Case, is it possible to get the label's variable using something like GuiCtrlGetHandle? Or will I have to create a Case for each label that is created?

In the past, I always created all possible pieces on the gui, but I want to learn to code more efficiently.

In my short example, I have coded only the first two labels to respond to clicks. Ideally, the Case would detect what you clicked on and I would pass that variable (or the text of the label) to a function. Does anyone know if this is possible?

#include <GUIConstants.au3>

$Form2 = GUICreate("Form2", 413, 298, 320, 125)
GUISetState(@SW_SHOW)

$startCoordY = 8
For $i = 1 to 10 Step 1
    Assign("Label" & $i, GUICtrlCreateLabel("This is line #" & $i, 16, $startCoordY + $i * 22, 252, 17), 2)
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Label1
            MsgBox(0, "", "you clicked label #1")
        Case $Label2
            MsgBox(0, "", "You clicked label 2")
    EndSwitch
WEnd
Link to comment
Share on other sites

  • Solution

One option is to create controls as array elements. You can then loop through the array to see if the GUI message matches an array element. There are other methods, but this way you can add or remove elements in the array however you wish (to match changes on the GUI). Be aware that when you destroy a control it is unlikely to be assigned the same handle by Windows the next time it is created. If you do not intend to make changes after the GUI exists, then reassigned handles after control destruction is not going to be an issue.

There are probably several other ways to do this and probably someone will mention an alternative. I tend to use the array method I mentioned.

Here's an example:

#include <GUIConstants.au3>
Global $Form2 = GUICreate("Form2", 413, 298, 320, 125)
Global $startCoordY = 8

Global $aCtrl[11]
$aCtrl[0] = 2 ; The number of labels to check
For $i = 1 to UBound($aCtrl) -1 ; Do you really need all these inactive labels to display?
    $aCtrl[$i] = GUICtrlCreateLabel("This is line #" & $i, 16, $startCoordY + $i * 22, 252, 17)
Next
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()

    If $nMsg = $GUI_EVENT_CLOSE Then Exit
    For $i = 0 To $aCtrl[0]
        If $nMsg = $aCtrl[$i] Then MsgBox(0, "", "you clicked label # " & $i)
    Next
WEnd
Edited by czardas
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...