VTautomate

Dynamically created GUI controls - How do I activate them properly?

8 posts in this topic

Dear AutoIt community,

I'm pretty new to AutoIt and to programming in general. I started with AutoIt roughly a week ago and have been programming in other programming languages for a few months now. Thanks btw for this awesome forum -its such a great source for learners like me. So, this is my first post and I kindly ask for your help. Perhaps it's no big deal, if so my apologies.

What I'm trying to do:

create a few controls at runtime, like several new rows of controls. Here is the thing: The column of open buttons should show a file open dialog, the choose button column should always show a choose folder dialog. So specific actions for each button column in my code represented by diffenrent MsgBoxes.

The problem:

The choose button column works fine, but the open button column doesn't work properly and I can't figure out why.

My code:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <Process.au3>
#include <_ControlGetHandleByPos.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SliderConstants.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>

$i = 0 ; Array Index
$k = 1 ; Array Size

; Create arrays for each group of the controls
Global $ModelPathsInputs[$k]
Global $OpenButtons[$k]
Global $MaterialComboBoxes[$k]
Global $FilenameInputs[$k]
Global $AngleComboBoxes[$k]
Global $StorageLocationInputs[$k]
Global $ChooseButtons[$k]


$BeginPos = 32 ; Startposition of the controls
$NewLinePos = 64
$PlusTop = 50 ; Moving distance for the controls

$Form1_1 = GUICreate("Render Machine", 1412, 600, 231, 165)

; Some Labels for the GUI
$Label3 = GUICtrlCreateLabel("Model", 80, 8, 37, 17)
$Label5 = GUICtrlCreateLabel("Material", 440, 8, 47, 17)
$Label1 = GUICtrlCreateLabel("Filename", 616, 8, 55, 17)
$Label2 = GUICtrlCreateLabel("Angle", 960, 8, 46, 17)
$Label4 = GUICtrlCreateLabel("Storage Location", 1032, 8, 102, 17)

; Create the 1st line of controls and write every single control in each array at index 0
$ModelPathsInputs[$i] = GUICtrlCreateInput("", 80, $BeginPos, 297, 21)
$OpenButtons[$i] = GUICtrlCreateButton("open", 384, $BeginPos, 41, 17)
$MaterialComboBoxes[$i] = GUICtrlCreateCombo("", 440, $BeginPos, 161, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Fos 1922 LEDER|Fos 1922 STOFF|FJ LEDER|FJ STOFF|v.W.C LEDER|v.W.C STOFF|vonW LEDER|vonW STOFF|zzz_SAMTSTOFFE")
$FilenameInputs[$i] = GUICtrlCreateInput("", 616, $BeginPos, 329, 21)
$AngleComboBoxes[$i] = GUICtrlCreateCombo("", 960, $BeginPos, 57, 25)
GUICtrlSetData(-1, "000|035|045")
$StorageLocationInputs[$i] = GUICtrlCreateInput("", 1032, $BeginPos, 273, 21)
$ChooseButtons[$i] = GUICtrlCreateButton("choose", 1320, $BeginPos, 73, 17)

; Some other controls
$StartRendering = GUICtrlCreateButton("Start rendering", 1264, 545, 137, 41)
$Duplicate = GUICtrlCreateButton("duplicate", 16, 32, 57, 25)
$NewLineButton = GUICtrlCreateButton("New Line", 80, $NewLinePos, 89, 25)

GUISetState(@SW_SHOW)

$InstanceCounter = 1 ; Counter to count the amount of renderinstances needed to be opened

; GUI while loop
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $NewLineButton

; Push NewLineButton down
$NewLinePos = $NewLinePos + $PlusTop
GUICtrlSetPos($NewLineButton, 80, $NewLinePos)


$InstanceCounter = $InstanceCounter + 1 ; Count InstanceCounter up
$BeginPos = $BeginPos + $PlusTop ; Update the top positions of the controls


$i = $i + 1 ; Count index variable up
$k = $k + 1 ; Count array size up

; Resize the array / get more room in each array
ReDim $ModelPathsInputs[$k]
ReDim $OpenButtons[$k]
ReDim $MaterialComboBoxes[$k]
ReDim $FilenameInputs[$k]
ReDim $AngleComboBoxes[$k]
ReDim $StorageLocationInputs[$k]
ReDim $ChooseButtons[$k]

; Create a new line of controls and store them in the specific array
$ModelPathsInputs[$i] = GUICtrlCreateInput("", 80, $BeginPos, 297, 21)
$OpenButtons[$i] = GUICtrlCreateButton("open", 384, $BeginPos, 41, 17)
$MaterialComboBoxes[$i] = GUICtrlCreateCombo("", 440, $BeginPos, 161, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Foscari 1922 LEDER|Foscari 1922 STOFF|Frederik Jonsen LEDER|Frederik Jonsen STOFF|v.W. Contemporary LEDER|v.W. Contemporary STOFF|von Wilmowsky LEDER|von Wilmowsky STOFF|zzz_SAMTSTOFFE")
$FilenameInputs[$i] = GUICtrlCreateInput("", 616, $BeginPos, 329, 21)
$AngleComboBoxes[$i] = GUICtrlCreateCombo("", 960, $BeginPos, 57, 25)
GUICtrlSetData(-1, "000|035|045")
$StorageLocationInputs[$i] = GUICtrlCreateInput("", 1032, $BeginPos, 273, 21)
$ChooseButtons[$i] = GUICtrlCreateButton("choose", 1320, $BeginPos, 73, 17)

Case $StartRendering
For $h = 0 To UBound($ChooseButtons) - 1 Step 1
ConsoleWrite("$ChooseButtons[" & $h & "]:=" & $ChooseButtons[$h] & @LF)
Next

; ===THIS IS WHERE I NEED YOUR HELP===
; Assume that several new lines of controls were created by the user (by clicking on the NewLineButton)
; What my program should do is showing a file open dialogue every time i click on one of the open buttons
; and a folder chooser when I click on the choose button (let's just work with different MsgBoxes for testing reasons here)
; The Problem is that I get a message when I click on any of the choose buttons, but
; I only get a message when I click the first of the open buttons

; When I cut and paste the case $OpenButtons at the beginning it is the other way round: the open buttons will work but the choose buttons won't
Case $ChooseButtons[0] To $ChooseButtons[UBound($ChooseButtons) - 1]
For $a = 0 To UBound($ChooseButtons) - 1 Step 1
If $nMsg = $ChooseButtons[$a] Then MsgBox(0, "", "You clicked one of the choose buttons")
Next

Case $OpenButtons[0] To $OpenButtons[UBound($OpenButtons) - 1]
For $j = 0 To UBound($OpenButtons) - 1 Step 1
If $nMsg = $OpenButtons[$j] Then MsgBox(0, "", "You clicked one of the open buttons")
Next



EndSwitch
WEnd

You will have figured out by the time that english isn't my first tongue. I translated the comments, control descriptions and variable names of my code to english so that it hopefully will help you to help me. I also cropped the code a little bit.

If you could have a look that would be really great!

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!

Case $OpenButtons[0] To $OpenButtons[UBound($OpenButtons) - 1]
You can't do it this way because the ControlIds of open and choose buttons get mixed.

I get the IDs 14, 24,31 for $ChooseButtons and 9, 19, 26 for $OpenButtons.

You need something like this:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <Process.au3>
;#include <_ControlGetHandleByPos.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SliderConstants.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>

$i = 0 ; Array Index
$k = 1 ; Array Size

; Create arrays for each group of the controls
Global $ModelPathsInputs[$k]
Global $OpenButtons[$k]
Global $MaterialComboBoxes[$k]
Global $FilenameInputs[$k]
Global $AngleComboBoxes[$k]
Global $StorageLocationInputs[$k]
Global $ChooseButtons[$k]

$BeginPos = 32 ; Startposition of the controls
$NewLinePos = 64
$PlusTop = 50 ; Moving distance for the controls

$Form1_1 = GUICreate("Render Machine", 1412, 600, 231, 165)

; Some Labels for the GUI
$Label3 = GUICtrlCreateLabel("Model", 80, 8, 37, 17)
$Label5 = GUICtrlCreateLabel("Material", 440, 8, 47, 17)
$Label1 = GUICtrlCreateLabel("Filename", 616, 8, 55, 17)
$Label2 = GUICtrlCreateLabel("Angle", 960, 8, 46, 17)
$Label4 = GUICtrlCreateLabel("Storage Location", 1032, 8, 102, 17)

; Create the 1st line of controls and write every single control in each array at index 0
$ModelPathsInputs[$i] = GUICtrlCreateInput("", 80, $BeginPos, 297, 21)
$OpenButtons[$i] = GUICtrlCreateButton("open", 384, $BeginPos, 41, 17)
$MaterialComboBoxes[$i] = GUICtrlCreateCombo("", 440, $BeginPos, 161, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Fos 1922 LEDER|Fos 1922 STOFF|FJ LEDER|FJ STOFF|v.W.C LEDER|v.W.C STOFF|vonW LEDER|vonW STOFF|zzz_SAMTSTOFFE")
$FilenameInputs[$i] = GUICtrlCreateInput("", 616, $BeginPos, 329, 21)
$AngleComboBoxes[$i] = GUICtrlCreateCombo("", 960, $BeginPos, 57, 25)
GUICtrlSetData(-1, "000|035|045")
$StorageLocationInputs[$i] = GUICtrlCreateInput("", 1032, $BeginPos, 273, 21)
$ChooseButtons[$i] = GUICtrlCreateButton("choose", 1320, $BeginPos, 73, 17)

; Some other controls
$StartRendering = GUICtrlCreateButton("Start rendering", 1264, 545, 137, 41)
$Duplicate = GUICtrlCreateButton("duplicate", 16, 32, 57, 25)
$NewLineButton = GUICtrlCreateButton("New Line", 80, $NewLinePos, 89, 25)

GUISetState(@SW_SHOW)

$InstanceCounter = 1 ; Counter to count the amount of renderinstances needed to be opened

; GUI while loop
While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = -3
            Exit
        Case $nMsg = $NewLineButton
            ; Push NewLineButton down
            $NewLinePos = $NewLinePos + $PlusTop
            GUICtrlSetPos($NewLineButton, 80, $NewLinePos)
            $InstanceCounter = $InstanceCounter + 1 ; Count InstanceCounter up
            $BeginPos = $BeginPos + $PlusTop ; Update the top positions of the controls
            $i = $i + 1 ; Count index variable up
            $k = $k + 1 ; Count array size up
            ; Resize the array / get more room in each array
            ReDim $ModelPathsInputs[$k]
            ReDim $OpenButtons[$k]
            ReDim $MaterialComboBoxes[$k]
            ReDim $FilenameInputs[$k]
            ReDim $AngleComboBoxes[$k]
            ReDim $StorageLocationInputs[$k]
            ReDim $ChooseButtons[$k]
            ; Create a new line of controls and store them in the specific array
            $ModelPathsInputs[$i] = GUICtrlCreateInput("", 80, $BeginPos, 297, 21)
            $OpenButtons[$i] = GUICtrlCreateButton("open", 384, $BeginPos, 41, 17)
            $MaterialComboBoxes[$i] = GUICtrlCreateCombo("", 440, $BeginPos, 161, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
            GUICtrlSetData(-1, "Foscari 1922 LEDER|Foscari 1922 STOFF|Frederik Jonsen LEDER|Frederik Jonsen STOFF|v.W. Contemporary LEDER|v.W. Contemporary STOFF|von Wilmowsky LEDER|von Wilmowsky STOFF|zzz_SAMTSTOFFE")
            $FilenameInputs[$i] = GUICtrlCreateInput("", 616, $BeginPos, 329, 21)
            $AngleComboBoxes[$i] = GUICtrlCreateCombo("", 960, $BeginPos, 57, 25)
            GUICtrlSetData(-1, "000|035|045")
            $StorageLocationInputs[$i] = GUICtrlCreateInput("", 1032, $BeginPos, 273, 21)
            $ChooseButtons[$i] = GUICtrlCreateButton("choose", 1320, $BeginPos, 73, 17)
        Case $nMsg = $StartRendering
            For $h = 0 To UBound($ChooseButtons) - 1 Step 1
                ConsoleWrite("$ChooseButtons[" & $h & "]:=" & $ChooseButtons[$h] & @LF)
            Next
            ; ===THIS IS WHERE I NEED YOUR HELP===
            ; Assume that several new lines of controls were created by the user (by clicking on the NewLineButton)
            ; What my program should do is showing a file open dialogue every time i click on one of the open buttons
            ; and a folder chooser when I click on the choose button (let's just work with different MsgBoxes for testing reasons here)
            ; The Problem is that I get a message when I click on any of the choose buttons, but
            ; I only get a message when I click the first of the open buttons

            ; When I cut and paste the case $OpenButtons at the beginning it is the other way round: the open buttons will work but the choose buttons won't
        Case $nMsg > 0
            For $iIndex = 0 To UBound($ChooseButtons) - 1
                If $ChooseButtons[$iIndex] = $nMsg Then
                    MsgBox(0, "", "You clicked one of the choose buttons")
                    ContinueLoop
                Endif
            Next
            For $iIndex = 0 To UBound($OpenButtons) - 1
                If $OpenButtons[$iIndex] = $nMsg Then
                    MsgBox(0, "", "You clicked one of the open buttons")
                    ContinueLoop
                Endif
            Next
    EndSelect
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Wow thank you very much! I'm not sure how this works... I hope I can figure it out by myself. Thanks again you helped me a lot!

Share this post


Link to post
Share on other sites

It's quite easy.

When you click a button GuiGetMsg returns the ControlID of the button. I then loop through both tables with the ControlIDs to check if it's an open or choose button.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

fine ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water,

Had the same issue and that code worked for me! Thanks.;)

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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