Jump to content

Dynamically creating controls


nbaser
 Share

Recommended Posts

Hi,

I am creating a tool in which many of my controls will be created dynamically.

such as: 

   1. In parent window- based on number of machines, tab items will be created.

                           2. For each such machine tab item, based on input of a combobox: group of items will be created and each such group will have different name.

I want to know:

 Is there any way i can get information that  the event is called from which tab item's combobox. As each such combobox have got same control id.

Or please give me link to any such script

Please reply asap.

Thanks in advance

Link to comment
Share on other sites

  • Moderators

nbaser,

 

As each such combobox have got same control id

If you are creating these combos in your GUI then they most certainly will NOT have the same ControlID. ;)

Why not post the code that you have and we can look to see how we can help you. When you post code please use Code tags - see here how to do it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The problem I was facing earlier got solved now. But I am facing another problem now.

I am trying to create an array containing ControlID of comboboxes. How should I handle event now? 

My code: 

I am creating a TabItem depending upon the number selected on first tab window. Function is (Please look at the controlID being stored in $a_workerscombo)

Note that combobox and it's parent both are created dynamically.

 
Func NewMachine($machineName, $cores)
    
    $machinecount+=1   ;just a count to track any new tabItem created
    $a_TabSheet[$machinecount+1] = GUICtrlCreateTabItem("Machine:" & $machineName & $cores)
    MsgBox($MB_SYSTEMMODAL, "machine", (_GUICtrlTab_GetItemText($PageControl1, $machinecount+1 )))
    $a_machineInfo[$machinecount-2][0]= $machineName
    $a_machineInfo[$machinecount-2][1]= $cores
    $Label6 = GUICtrlCreateLabel("No of workers", 80, 72, 118, 24)
    GUICtrlSetFont(-1, 10, 400, 0, "Arial Rounded MT Bold")
     $a_workerscombo[$machinecount-2] = GUICtrlCreateCombo("Workers", 264, 72, 161, 25)
    
        GUICtrlSetData(-1, "0|1|2|3|4|5|6|7|8|9|10")
    $Checkbox5 = GUICtrlCreateCheckbox("MLRCS", 464, 56, 129, 25)
    $Checkbox6 = GUICtrlCreateCheckbox("LRCS", 464, 88, 113, 33)
    $Input7 = GUICtrlCreateInput("CoresAvailable are:", 616, 72, 201, 28)
        $Button6 = GUICtrlCreateButton("Next Worker", 776, 264, 161, 57, $WS_GROUP)
        $Button7 = GUICtrlCreateButton("Previous Worker", 784, 352, 153, 57, $WS_GROUP)
    $Button9 = GUICtrlCreateButton("Apply same settings to all workers", 752, 456, 281, 73, $WS_GROUP)
        GUICtrlCreateTabItem("")
    GUISetState(@SW_SHOW)



EndFun

And my event handle loop is: 

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


EndSwi

My requirement is: 

1. I want to create some more controls when value is selected for combox only on respective Tab.

Please let me know if there is any better way to create and manage controls varying dynamically.

Thanks in advance.

Link to comment
Share on other sites

  • Moderators

nbaser,

This is how I would do it: ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Create array to hold combo ControlIDs
Global $a_workerscombo[1] = [0]

$hGUI = GUICreate("Test", 500, 500)

$cTab = GUICtrlCreateTab(10, 10, 480, 400)
$cButton = GUICtrlCreateButton("New Tab", 10, 460, 80, 30)

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            NewMachine(" Name ", "cores")
        Case Else
            ; Loop through the array of combo ControlIDs
            For $i = 1 To $a_workerscombo[0]
                If $a_workerscombo[$i] = $iMsg Then
                    ; When we find the correct one
                    MsgBox($MB_SYSTEMMODAL, "Combo selected", GUICtrlRead($iMsg))
                    ; No point in looking any further
                    ExitLoop
                EndIf
            Next
    EndSwitch

WEnd

Func NewMachine($machineName, $cores)

    ; Increase count
    $a_workerscombo[0] += 1
    ; Resize array
    ReDim $a_workerscombo[$a_workerscombo[0] + 1]

    GUICtrlCreateTabItem("Machine:" & $machineName & $cores)
    ; Store combo ControlID in array
    $a_workerscombo[$a_workerscombo[0]] = GUICtrlCreateCombo("", 264, 72, 161, 25)
    ; Add different item to each combo so we can distinguish them later
    GUICtrlSetData(-1, "Combo " & $a_workerscombo[0] & "|0|1|2|3|4|5|6|7|8|9|10", "Combo " & $a_workerscombo[0])
    GUICtrlCreateTabItem("")

EndFunc   ;==>NewMachine
Please ask if anything is unclear. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

nbaser,

Expect more such questions as I am working with AutoIT for first time

Reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page). :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks. I have already gone through the help file/wiki and references. Facing problems in creation of run time controls.

How can I create controls only on selected tab. In continuation of above mentioned case, when I am trying to create some more controls based on the value selected in combobox, the controls are drawn up on all tabs.

I have tried using methods 

 _GUICtrlTab_GetCurSel or _GUICtrlTab_ActivateTab but couldn't succeed :/

Link to comment
Share on other sites

  • Moderators

nbaser,

I do not have the time to reply in detail just at the moment - try reading the Tabs tutorial in the Wiki and see if that helps. It sounds like GUISwitch is the answer - and that is explained in the tutorial. Ask again in a few hours if you are still stuck. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba23

I have tried using GUISwitch but coudn't achieve :(

Please see attached code.

Func AddWorkers($Noofworker, $index )

 
GUISwitch($PageControl1, $a_TabSheet[$index+1])

For $i=1 To $Noofworker Step +1

    $Group2 = GUICtrlCreateGroup("Worker" & $i & $a_machineInfo[$index-1][0], 56, 152, 673, 537)
    $Checkbox2 = GUICtrlCreateCheckbox("Cacheobjfirstoccur", 72, 200, 209, 25)
    $Label7 = GUICtrlCreateLabel("maxrastercache", 280, 184, 137, 24)
    $Input10 = GUICtrlCreateInput("maxrastercache", 280, 208, 145, 28)
    $Label20 = GUICtrlCreateLabel("maxrasterdiskcache", 456, 184, 148, 24)
    GUICtrlSetFont(-1, 10, 400, 0, "Cambria")
    $Radio5 = GUICtrlCreateRadio("-1", 456, 208, 33, 17)
    $Radio4 = GUICtrlCreateRadio("1", 568, 208, 33, 17)
    $Radio3 = GUICtrlCreateRadio("0", 512, 208, 41, 17)
    $Input11 = GUICtrlCreateInput("Input11", 128, 272, 121, 28)
    $Label21 = GUICtrlCreateLabel(" jplog", 72, 272, 50, 24)
    $Input12 = GUICtrlCreateInput("D:/DRIP_Automation/Tempdir/", 256, 272, 273, 28)
    $Button4 = GUICtrlCreateButton("Change", 536, 272, 73, 33, $WS_GROUP)
    $Button10 = GUICtrlCreateButton("Reset", 616, 272, 97, 33, $WS_GROUP)
     GUICtrlCreateGroup("", -99, -99, 1, 1)
Next
     GUISwitch($PageControl1)
     GUICtrlCreateTabItem ("")
EndFunc
Link to comment
Share on other sites

  • Moderators

nbaser,

This is getting very complicated: :(

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>

; Create array to hold combo ControlIDs
Global $a_workerscombo[1] = [0]
Global $a_Tabs[1] = [0]
Global $a_Buttons[1][1] = [[0]]

$hGUI = GUICreate("Test", 500, 500)

$cTab = GUICtrlCreateTab(10, 10, 480, 400)
$cButton = GUICtrlCreateButton("New Tab", 10, 460, 80, 30)

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            NewMachine(" Name ", "cores")
        Case Else
            ; Loop through the array of combo ControlIDs
            For $i = 1 To $a_workerscombo[0]
                If $a_workerscombo[$i] = $iMsg Then
                    ; When we find the correct one
                    ; Create controls only on the selected tab
                    $iTab = GUICtrlRead($cTab)
                    ; Switch to that tab and open tab structure
                    GUISwitch($hGUI, $a_Tabs[$iTab + 1])
                    ; Create Button on that tab
                    ; Increase button count for the tab
                    $a_Buttons[0][$iTab] += 1
                    ; We will only allow 5 to be created
                    If $a_Buttons[0][$iTab] <= 5 Then
                        ; Resize array if required
                        If $a_Buttons[0][$iTab] > UBound($a_Buttons, $UBOUND_ROWS) - 1 Then
                            ReDim $a_Buttons[$a_Buttons[0][$iTab] + 1][UBound($a_Buttons, $UBOUND_COLUMNS)]
                        EndIf
                        ; Create the button
                        $a_Buttons[$a_Buttons[0][$iTab]][$iTab] = GUICtrlCreateButton("But " & $a_Buttons[0][$iTab] & " - Tab " & $iTab, 50, ($a_Buttons[0][$iTab] * 50), 80, 30)
                    EndIf
                    ; Close tab structure again
                    GUICtrlCreateTabItem("")
                    GUISwitch($hGUI)
                    ; No point in looking any further
                    ExitLoop
                EndIf
            Next
    EndSwitch

WEnd

Func NewMachine($machineName, $cores)

    ; Increase counts
    $a_workerscombo[0] += 1
    $a_Tabs[0] += 1
    ; Resize arrays
    ReDim $a_workerscombo[$a_workerscombo[0] + 1]
    ReDim $a_Tabs[$a_Tabs[0] + 1]

    $a_Tabs[$a_Tabs[0]] = GUICtrlCreateTabItem("Machine:" & $machineName & $cores)
    ; Store combo ControlID in array
    $a_workerscombo[$a_workerscombo[0]] = GUICtrlCreateCombo("", 264, 72, 161, 25)
    ; Add different item to each combo so we can distinguish them later
    GUICtrlSetData(-1, "Combo " & $a_workerscombo[0] & "|0|1|2|3|4|5|6|7|8|9|10", "Combo " & $a_workerscombo[0])
    GUICtrlCreateTabItem("")

    ; Increase size of the button array
    ReDim $a_Buttons[UBound($a_Buttons, $UBOUND_ROWS)][UBound($a_Buttons, $UBOUND_COLUMNS) + 1]

EndFunc   ;==>NewMachine
Might I suggest creating as many tabs as you need when you create the GUI and adding all the controls at that point. Then you can activate them as required - something like this: ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Global $aTab[5][2]

$hGUI = GUICreate("Test", 500, 500)

$cTab = GUICtrlCreateTab(10, 10, 480, 400)
For $i = 0 To 4
    $aTab[$i][0] = GUICtrlCreateTabItem("Not Used") ; Store ControlID
    $aTab[$i][1] = 0                                ; Store activation code (all not activated)
Next
GUICtrlCreateTabItem("")

$cButton = GUICtrlCreateButton("Open Tab", 10, 460, 80, 30)

GUISetState()

; Tab to use as focus in inactive tab clicked
$cLastTab = $aTab[0][0]

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cTab
            ; Get clicked tab index
            $iTab = GUICtrlRead($cTab)
            ; If inactive
            If $aTab[$iTab][1] <> 1 Then
                ; Reselect last tab
                GUICtrlSetState($cLastTab, $GUI_SHOW)
            Else
                ; Reset current selection
                $cLastTab = $aTab[$iTab][0]
            EndIf
        Case $cButton
            For $i = 0 To 4
                If $aTab[$i][1] <> 1 Then
                    ; Activate tab
                    GUICtrlSetState($aTab[$i][0], $GUI_SHOW)
                    $aTab[$i][1] = 1
                    ; Rename it
                    GUICtrlSetData($aTab[$i][0], "Tab " & $i)
                    ; No point in looking further
                    ExitLoop
                EndIf
            Next
    EndSwitch

WEnd
Neither are going to be that easy to code nor maintain, but in my opinion the second is better. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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