Jump to content

How do I create a GUI with dynamic labels based on an array?


PowerCat
 Share

Recommended Posts

I've tried to do this in the past but always failed.

I have an array of items, and I want my GUI to be created based on the content of the file, for example

I'm obviously doing it wrong, but what I'd like is for the GUI to have 1 label for each item in my array, and one button that will launch a single function, by sending the label's text as argument.

How can I have the $Label variable be created as $Label1, $Label2, etc and so on, so that I can actually use them as they won't all have the same name?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Dim $ItemArray[5]=["this is item 1", "here is item 2", "this line contains a third one", "I don't know what's going on","ZIMBABWE"]

$AmountItems = ubound($ItemArray)-1

$Form1 = GUICreate("A Simple GUI", 400, (30*$AmountItems)+5)
$top = 0

For $i = 0 to $AmountItems
ConsoleWrite($ItemArray[$i] & @CRLF)

$Label = GUICtrlCreateLabel($ItemArray[$i], 5, $top+5)
;
; $Label & $i = GUICtrlCreateLabel($ItemArray[$i], 5, $top+5)
; sort of what I want to end up with, but I'm doing it wrong!

$top = $top + 25

Next

GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

EndSwitch
WEnd
Link to comment
Share on other sites

There are many ways to do it, here is a quick one :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Dim $ItemArray[5] = ["this is item 1", "here is item 2", "this line contains a third one", "I don't know what's going on", "ZIMBABWE"]

$AmountItems = UBound($ItemArray) - 1

$Form1 = GUICreate("A Simple GUI", 400, (30 * $AmountItems) + 5)
$top = 0

Local $iStartId = 0, $iEndId = 0

For $i = 0 To $AmountItems
    $Label = GUICtrlCreateLabel($ItemArray[$i], 5, $top + 5)
    If $i = 0 Then $iStartId = $Label
    If $i = $AmountItems Then $iEndId = $Label
    ;
    ; $Label & $i = GUICtrlCreateLabel($ItemArray[$i], 5, $top+5)
    ; sort of what I want to end up with, but I'm doing it wrong!

    $top = $top + 25

Next

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If $nMsg >= $iStartId And $nMsg <= $iEndId Then
                ConsoleWrite($ItemArray[$nMsg - $iStartId] & @CrLf)
            EndIf
    EndSwitch
WEnd

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Dim $ItemArray[5] = ["this is item 1", "here is item 2", "this line contains a third one", "I don't know what's going on", "ZIMBABWE"]

$AmountItems = UBound($ItemArray) - 1

Func Create()
Local $iStartId = 0, $iEndId = 0
For $i = 0To $AmountItems
     $Label =GUICtrlCreateLabel($ItemArray[$i], 5, $top + 5)
     If $i = 0 Then $iStartId = $Label
         If $i = $AmountItems Then $iEndId = $Label

             $Label & $i = GUICtrlCreateLabel($ItemArray[$i], 5, $top+5)

$top = $top + 25

Next
EndFunc

Func GUI()


$Form1 = GUICreate("A Simple GUI", 400, (30 * $AmountItems) + 5)
$top = 0

GUISetState(@SW_SHOW)
EndFunc


GUI()
Create()
While 1
$nMsg = GUIGetMsg()
     Switch $nMsg
         Case $GUI_EVENT_CLOSE
             Exit
         Case Else
             If $nMsg >= $iStartId And $nMsg <= $iEndId Then
                 ConsoleWrite($ItemArray[$nMsg - $iStartId] & @CrLf)
             EndIf
     EndSwitch
WEnd

Edited by JonBMN
Link to comment
Share on other sites

@JonBMN

It's quite simple, you need to change the scope of the variables used inside your functions to global and create the functions.

#include <GUIConstantsEx.au3>

Global $aItem[5] = ["this is item 1", "here is item 2", "this line contains a third one", "I don't know what's going on", "ZIMBABWE"]
Global $iAmountItems = UBound($aItem) - 1

Global $iStartId = 0, $iEndId = 0, $iTop = 0
Global $hForm1 = 0

_CreateGUI()
_CreateLabel()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If $nMsg >= $iStartId And $nMsg <= $iEndId Then
                ConsoleWrite($aItem[$nMsg - $iStartId] & @CrLf)
            EndIf
    EndSwitch
WEnd

Func _CreateGUI()
    $hForm1 = GUICreate("A Simple GUI", 400, (30 * $iAmountItems) + 5)
EndFunc

Func _CreateLabel()
    For $i = 0 To $iAmountItems
        $Label = GUICtrlCreateLabel($aItem[$i], 5, $iTop + 5)
        If $i = 0 Then $iStartId = $Label
        If $i = $iAmountItems Then $iEndId = $Label

        $iTop = $iTop + 25
    Next

    GUISetState(@SW_SHOW, $hForm1)
EndFunc

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

  • 8 months later...

GUICtrlSetData will update the label's contents.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

GUICtrlSetData will update the label's contents.

 

I know this but how do you update the dynamic labels created from the array using the example (post #6) posted in this thread? I guess another way to ask is, do these dynamic labels have their own variable?

Edited by quake101
Link to comment
Share on other sites

If you're referring to Firefox's code, then no, his code doesn't expose the control IDs outside of the function. But if you modify the code a bit it shouldn't be hard to do that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Actually taking a closer look at the code, the label ids are contained in $iStartID and $iEndID. $iStartID is the control ID of the first label, and $iEndID is the control ID of the last label. So, using this information, you should be able to identify which label goes with which ID by either adding the label count to $iStartID or subtracting from $iEndID. That means that label 3's control ID would be $iStartID + 2 (+2 and not +3 because the first label is $iStartID).

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Actually taking a closer look at the code, the label ids are contained in $iStartID and $iEndID. $iStartID is the control ID of the first label, and $iEndID is the control ID of the last label. So, using this information, you should be able to identify which label goes with which ID by either adding the label count to $iStartID or subtracting from $iEndID. That means that label 3's control ID would be $iStartID + 2 (+2 and not +3 because the first label is $iStartID).

 

Thanks! :)

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...