abberration Posted October 14, 2013 Share Posted October 14, 2013 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 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
Solution czardas Posted October 14, 2013 Solution Share Posted October 14, 2013 (edited) 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 October 14, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
abberration Posted October 14, 2013 Author Share Posted October 14, 2013 Very interesting, czardas. Before you edited to post an example, I didn't know how your advice would help. I understand now and think your idea is a viable solution. Thank you for your help! czardas 1 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
czardas Posted October 14, 2013 Share Posted October 14, 2013 No worries. Play around and see what modifications you can make. This is perhaps the simplest solution. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now