Jump to content

OnEvent for dynamically created controls...


misioooo
 Share

Recommended Posts

Hello! I have a GUI oriented script with a lot of combo boxes etc.

From my GUI i can add new groups (tabs) and i can also add new subgroups into each tab. That works perfectly (thanks Melba one more time!)

But lets get to the point. I am using OnEvent mode. How i can check if/what value has some control from inside any of all tabs? I store IDs of those controls in arrays being part of 2d array... Getting those ID's is not simple, but iof needed i can walk through all the arrays being inside main array and grab them into separate array or something.

Is there a way to create "GUICtrlSetOnEvent" for control that was added from within the GUI? Or how to create some event with group of control ID's? Maybe i can use array instead of single ctrlID?

Switching to GuiGetMsg will require too much time (for me...) so i prefere to use OnEvent mode.

Link to comment
Share on other sites

  • Moderators

How about you post your script (or a reproducer) to show how you're storing your control IDs, so we can see what you're doing? It will be much easier to assist you.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

"Is there a way to create "GUICtrlSetOnEvent" for control that was added from within the GUI?"

This may be, or not what you ask.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("Form1", 615, 103, 192, 124)
$button1 = GUICtrlCreateButton("Button1", 32, 24, 193, 49)
GUICtrlSetOnEvent($button1, "nuButton")
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func nuButton()
    $button1andOneHalf = GUICtrlCreateButton("Button1 1/2", 230, 24, 100, 49)
    GUICtrlSetOnEvent($button1andOneHalf, "xIt")
EndFunc

Func xIt()
    Exit
EndFunc

Posted script would help for knowing your intent.

Edit: what he said while I was typing.

Edited by lorenkinzel
Link to comment
Share on other sites

lorenkinzel thank you, but thats not what i need...

My script is a bit messy and quiet big so i will write general idea:

Button1 = BUTTON_CREATE1

Button2 = BUTTON_CREATE2

OnPressButton1 = Function_that_creates_new_TAB

OnPressButton2 = Function_that_creates_new_Controls_on_current_TAB

(i also have buttons for removing last created controls on curently selected tab and for removing last added tab)

Data is stored like this:

$ALL_DATA [$tab_number_stores_rows_number][$row/control_set_number] (so $ALL_DATA[2][x] says that on TAB2 we have (VALUE_OF_DATA_ALL[2][0]) rows/added controls.

Controls are stored in $controlIDs[14] (every press of a button 2 adds 15 controls) and is stored in proper $DATA_ALL[$tab_selected][$DATA_ALL[$tab_selected][0]+1]

In case u want to look at the code:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt ("GUIOnEventMode",1)
Opt ("GUICoordMode",1)

#Region CONSTANTS
Const $GUIWidth         = 1500
Const $GUIHeight        = 800
Const $GROUP_TAB        = 0
Const $MANUFACTURER     = 1
Const $MODEL            = 2
Const $LAN              = 3
Const $DISTANCE_LABEL   = 11
Const $DISTANCE         = 4
Const $EXTENDER_LABEL   = 12
Const $EXTENDER         = 5
Const $MANUFACTURER_LABEL = 6
Const $MODEL_LABEL      = 7
Const $NET              = 8
Const $GSM              = 9
Const $NET_NONE         = 10
#EndRegion CONSTANTS

#Region VARIABLES
Global $max_groups[20], $max_monitors = 0, $samsung_models_list
Global $data_all[1][15]                         ;1st dim is group, 2nd dim is subgroup (that holds array of data for each subgroup)
Global $ids [13]                                ;holds IDs and some data for each subgroup
Global $groups_subgroups[2] = [0,0]
Global $x_off = 170
Global $y_off = 20
Global $extenders_list  = "|Kramer PT101Hxl|Kramer PT101HDCP|Kramer 602R/T|Kramer C-FODM/FODM|Kramer C-FOHM/FOHM|Kramer TP573/TP574|Inny"
Global $server_list     = "|Dell R210 II i3, 4GB, 2x1TB|OPTIMUS IRON R11041 E3-1220v3/8GB/2x500GB|Fujitsu PRIMERGY RX100S7 E3-1220 4GB 2x500GB|HP DL320e Gen8 E3-1220v|Inny|Brak (własny)"
Global $player_list     = "|OPS|MiniPC|IPC"
#EndRegion VARIABLES

#Region GUI
$max_groups[0]=0
$gui_main = GUICreate("Konfigurator DS", $GUIWidth, $GUIHeight, -1, -1)

;Main menu on top
$system_label   = GUICtrlCreateLabel("Wybierz platformę",20,20,150)
$system         = GUICtrlCreateCombo("",20,20+$y_off)
GUICtrlSetData(-1,"|(SaaS) Media4Display|Media4Display|(SaaS) Strong Signage|Strong Signage|MagicInfo Lite|MagicInfo Premium S|MagicInfo Premium I")
$player_label   = GUICtrlCreateLabel("Wybierz odtwarzacze",20+$x_off,20,150)
$player         = GUICtrlCreateCombo("",20+$x_off,20+$y_off)
GUICtrlSetState(-1,$GUI_DISABLE)
$server_label   = GUICtrlCreateLabel("Wybierz serwer",20+2*$x_off,20,150)
$server         = GUICtrlCreateCombo("",20+2*$x_off,20+$y_off)
GUICtrlSetState(-1,$GUI_DISABLE)
GUICtrlSetData(-1,$server_list)
$add_group      = GUICtrlCreateButton("Dodaj grupę monitorów",1310,20,150)
$add_monitor    = GUICtrlCreateButton("Dodaj monitor",1310,40+$y_off,150)
$rem_monitor    = GUICtrlCreateButton("Usuń monitor",1150,40+$y_off,150)
$rem_group      = GUICtrlCreateButton("Usuń grupę monitorów",1150,20,150)
$tabGroups      = GUICtrlCreateTab(20,100,$GUIWidth-40,$GUIHeight-140)
GUICtrlCreateTabItem("")
;EVENTS
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUIClose")
GUICtrlSetOnEvent($add_group,"_AddGroup")
GUICtrlSetOnEvent($add_monitor,"_AddMonitor")
GUICtrlSetOnEvent($rem_monitor,"_RemMonitor")
GUICtrlSetOnEvent($rem_group,"_RemGroup")
GUICtrlSetOnEvent($system,"_ChangeSystem")
;GUICtrlSetOnEvent($ids[$DISTANCE],"_ChangeDistance")
;END EVENTS
GUISetState(@SW_SHOW)
#EndRegion GUI

;$data_all[0][0] = $groups_subgroups ;Holds number of GROUPS and SUBGROUPS (always in element [0,0])

$data_all[0][0] = 0 ;group count
#Region EVENTS
Func _GUIClose()
    Exit
EndFunc

Func _ChangeDistance() ;Polling function
    ;ConsoleWrite("!> DEBUG - Inside _ChangeDistance function..."&@CRLF)
    Sleep(250)
    For $i=1 To $max_groups[0] Step 1
        For $j=1 to $max_groups[$i] Step 1
            $tmp = $data_all[$i][$j]
            ;ConsoleWrite("!>DEBUG - $data_all["&$i&"]["&$j&"] DISTANCE SELECTED: "&$tmp[$DISTANCE]&@CRLF)
            If GUICtrlRead($tmp[$DISTANCE]) = ">20" Then

                GUICtrlSetState($tmp[$EXTENDER],$GUI_ENABLE)
            ElseIf GUICtrlRead($tmp[$DISTANCE]) = "<20" Then
                GUICtrlSetState($tmp[$EXTENDER],$GUI_DISABLE)
            EndIf
        Next
    Next

    ;MsgBox($MB_SYSTEMMODAL,"","Control ID that triggered event: "&@GUI_CtrlId)
EndFunc

Func _ChangeSystem()
    Local $tmp
    Local $system_value = GUICtrlRead($system)
    If $system_value = "MagicInfo Lite" OR $system_value = "MagicInfo Premium S" Then
        GUICtrlSetData($player,"|MagicInfo")
        GUICtrlSetState($player,$GUI_DISABLE)
        For $i=1 To $max_groups[0] Step 1
            For $j=1 to $max_groups[$i] Step 1
                $tmp = $data_all[$i][$j]
                GUICtrlSetState($tmp[$MANUFACTURER],$GUI_DISABLE)
            Next
        Next
    Else
        For $i=1 To $max_groups[0] Step 1
            For $j=1 to $max_groups[$i] Step 1
                $tmp = $data_all[$i][$j]
                GUICtrlSetState($tmp[$MANUFACTURER],$GUI_ENABLE)
            Next
        Next
        MsgBox($MB_SYSTEMMODAL,"","$player state: "&GUICtrlGetState($player))
        If GUICtrlGetState($player) = 144 Then
            GUICtrlSetData($player,$player_list)
            GUICtrlSetState($player,$GUI_ENABLE)
        EndIf
    EndIf
    If $system_value = "Media4Display" OR $system_value = "Strong Signage" OR $system_value = "MagicInfo Premium I" Then
        GUICtrlSetData($server,$server_list)
        GUICtrlSetState($server,$GUI_ENABLE)
    Else
        GUICtrlSetData($server,"")
        GUICtrlSetState($server,$GUI_DISABLE)
    EndIf
EndFunc
Func _AddGroup()

    $max_groups[0] += 1
    $max_groups[$max_groups[0]] = 1
    ReDim $data_all[UBound($data_all)+1][UBound($data_all,2)]
    $ids[$GROUP_TAB]            = GUICtrlCreateTabItem("Grupa "&$max_groups[0])
    $ids[$MANUFACTURER_LABEL]   = GUICtrlCreateLabel("Producent",40,130,100)
    $ids[$MANUFACTURER]         = GUICtrlCreateCombo("Samsung",40,150,100)
    If (GUICtrlRead($system)) = "MagicInfo Lite" OR (GUICtrlRead($system)) = "MagicInfo Premium S" Then
        GUICtrlSetState($ids[$MANUFACTURER],$GUI_DISABLE)
    EndIf
    $ids[$MODEL_LABEL]          = GUICtrlCreateLabel("Model",150,130,150)
    $ids[$MODEL]                = GUICtrlCreateCombo("",150,150,150)
    GUICtrlSetData(-1,$samsung_models_list)
    $ids[$NET]                  = GUICtrlCreateGroup("Podłączenie do sieci",260+50,130,150,40)
    $ids[$LAN]                  = GUICtrlCreateRadio("LAN",265+50,145)
    GUICtrlSetState(-1,$GUI_CHECKED)
    $ids[$GSM]                  = GUICtrlCreateRadio("GSM",315+50,145)
    $ids[$NET_NONE]             = GUICtrlCreateRadio("Brak",365+50,145)
    GUICtrlCreateGroup("",-99,-99,1,1) ;end of radio buttons group
    $ids[$DISTANCE_LABEL]       = GUICtrlCreateLabel("Odległość do poprzedniego monitora (m)",470,130)
    ;$ids[$DISTANCE]            = GUICtrlCreateCombo("<20",540,150,50)
    ;GUICtrlSetData(-1,">20")
    $ids[$EXTENDER_LABEL]       = GUICtrlCreateLabel("Wybierz extender",670,130)
    ;GUICtrlSetData(-1,$extenders_list)
    ;$ids[$EXTENDER]            = GUICtrlCreateCombo("",670,150,150)
    ;_ArrayDisplay($ids)
    $ids[$DISTANCE]             = $ids[$MANUFACTURER]
    $ids[$EXTENDER]             = $ids[$MANUFACTURER]
    ;GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlCreateTabItem("")
    $data_all[$max_groups[0]][1] = $ids

EndFunc
Func _RemGroup()
    If $max_groups[0] < 2 Then
        MsgBox($MB_SYSTEMMODAL,"Konfigurator DS","Nie mozesz usunąć jedynej grupy!")
        Return 1
    EndIf

    Local $tmp = $data_all[UBound($data_all)-1][1]
    ;_ArrayDisplay($data_all[UBound($data_all)-1][1])
    GUICtrlDelete($tmp[$GROUP_TAB])

    ReDim $data_all[UBound($data_all)-1][UBound($data_all,2)]
    $max_groups[0] -= 1
    $max_groups[$max_groups[0]] = 0
EndFunc
Func _AddMonitor()
    ;$ids=""
    Local $group_no = GUICtrlRead($tabGroups)+1
    Local $y_move = 45*$max_groups[$group_no]
    $max_groups[$group_no] += 1
    GUISwitch($gui_main,GUICtrlRead($tabGroups,1))

    $ids[$MANUFACTURER]         = GUICtrlCreateCombo("Samsung",40,150+$y_move,100)
    If (GUICtrlRead($system)) = "MagicInfo Lite" OR (GUICtrlRead($system)) = "MagicInfo Premium S" Then
        GUICtrlSetState($ids[$MANUFACTURER],$GUI_DISABLE)
    EndIf
    $ids[$MODEL]                = GUICtrlCreateCombo("",150,150+$y_move,150)
    $ids[$NET]                  = GUICtrlCreateGroup("Podłączenie do sieci",260+50,130+$y_move,150,40)
    $ids[$LAN]                  = GUICtrlCreateRadio("LAN",265+50,145+$y_move)
    GUICtrlSetState(-1,$GUI_CHECKED)
    $ids[$GSM]                  = GUICtrlCreateRadio("GSM",315+50,145+$y_move)
    $ids[$NET_NONE]             = GUICtrlCreateRadio("Brak",365+50,145+$y_move)
    GUICtrlCreateGroup("",-99,-99,1,1) ;end of radio buttons group
    $ids[$DISTANCE]             = GUICtrlCreateCombo("<20",540,150+$y_move,50)
    GUICtrlSetData(-1,">20")
    $ids[$EXTENDER]             = GUICtrlCreateCombo("",670,150+$y_move,150)
    GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlSetData(-1,$extenders_list)
    $ids[$GROUP_TAB]            = $ids[$MANUFACTURER]
    $ids[$MANUFACTURER_LABEL]   = $ids[$MANUFACTURER]
    $ids[$MODEL_LABEL]          = $ids[$MANUFACTURER]
    $ids[$DISTANCE_LABEL]       = $ids[$MANUFACTURER]
    $ids[$EXTENDER_LABEL]       = $ids[$MANUFACTURER]

    ;MsgBox($MB_SYSTEMMODAL,"","Group no: "&$group_no&@CRLF&"Monitor to add: "&$max_groups[$group_no]&@CRLF&"UBounds data_all: "&UBound($data_all)&" x "&UBound($data_all,2))
    $data_all[$group_no][$max_groups[$group_no]] = $ids
    $data_all[$group_no][0] += 1
    ;_ArrayDisplay($ids)
    GUICtrlCreateTabItem("")
    ;*****************************************************************
EndFunc
Func _RemMonitor()
    Local $group_no = GUICtrlRead($tabGroups)+1
    Local $mon_no = $max_groups[$group_no]
    If $mon_no < 2 Then
        MsgBox($MB_SYSTEMMODAL,"Konfigurator DS","Nie mozesz usunąć jedynego monitora z grupy!")
        Return 1
    EndIf
    Local $tmp = $data_all[$group_no][$mon_no]
    ;_ArrayDisplay($tmp)
    For $i=0 To 12 Step 1
        GUICtrlDelete($tmp[$i])
    Next

    $max_groups[$group_no] -= 1

EndFunc
#EndRegion EVENTS

Func _InitOnce()
    Local $mon1[13]
    $max_groups[0] += 1
    $max_groups[$max_groups[0]] += 1
    $mon1[$GROUP_TAB]           = GUICtrlCreateTabItem("Grupa 1")
    $mon1[$MANUFACTURER_LABEL]  = GUICtrlCreateLabel("Producent",40,130,100)
    $mon1[$MANUFACTURER]        = GUICtrlCreateCombo("Samsung",40,150,100)
    $mon1[$MODEL_LABEL]             = GUICtrlCreateLabel("Model",150,130,150)
    $mon1[$MODEL]               = GUICtrlCreateCombo("",150,150,150)
    $mon1[$NET]                     = GUICtrlCreateGroup("Podłączenie do sieci",260+50,130,150,40)
    $mon1[$LAN]                 = GUICtrlCreateRadio("LAN",265+50,145)
    GUICtrlSetState(-1,$GUI_CHECKED)
    $mon1[$GSM]                 = GUICtrlCreateRadio("GSM",315+50,145)
    $mon1[$NET_NONE]                = GUICtrlCreateRadio("Brak",365+50,145)
    GUICtrlCreateGroup("",-99,-99,1,1) ;end of radio buttons group
    $mon1[$DISTANCE_LABEL]      = GUICtrlCreateLabel("Odległość do poprzedniego monitora (m)",470,130)
    ;$mon1[$DISTANCE]           = GUICtrlCreateCombo("<20",540,150,50)
    ;GUICtrlSetData(-1,">20")
    $mon1[$EXTENDER_LABEL]      = GUICtrlCreateLabel("Wybierz extender",670,130)
    ;$mon1[$EXTENDER]           = GUICtrlCreateCombo("",670,150,150)
    ;_ArrayDisplay($mon1)
    $ids[$DISTANCE]             = $ids[$MANUFACTURER]
    $ids[$EXTENDER]             = $ids[$MANUFACTURER]
    ReDim $data_all[UBound($data_all)+1][UBound($data_all,2)]
    $data_all[1][1]=$mon1
    ;$tmp = $data_all[1][1]
    ;_ArrayDisplay($tmp)
    ;GUICtrlSetState(-1,$GUI_DISABLE)
    GUICtrlCreateTabItem("")

EndFunc


;****************** CSV to Array (2D)
Func _ParseCSV($f,$Dchar,$error,$skip)

  Local $array[500][500]
  Local $line = ""

  $i = 0
  $file = FileOpen($f,0)
  If $file = -1 Then
    MsgBox(0, "Error", $error)
    Return False
   EndIf

  ;skip 1st line
  If $skip Then $line = FileReadLine($file)

  While 1
       $i = $i + 1
       Local $line = FileReadLine($file)
       If @error = -1 Then ExitLoop
       $row_array = StringSplit($line,$Dchar)
        If $i == 1 Then $row_size = UBound($row_array)
        If $row_size <> UBound($row_array) Then  MsgBox(0, "Error", "Row: " & $i & " has different size ")
        $row_size = UBound($row_array)
        $array = _arrayAdd_2d($array,$i,$row_array,$row_size)

   WEnd
  FileClose($file)
  $array[0][0] = $i-1 ;stores number of lines
   $array[0][1] = $row_size -1  ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
   Return $array

EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
    For $i=1 To $row_size -1 Step 1
        $array[$inwhich][$i] = $row_array[$i]
  Next
  Return $array
EndFunc
;*****************************************************
_InitOnce()
Global $samsung_models = _ParseCSV("SAMSUNG.csv",";","Cant read from SAMSUNG.csv file!",True) ;get all SAMSUNG data into 2D array (0,0 and 0,1 holds number of columns/rows)
;_ArrayDisplay($samsung_models)
Global $samsung_models_names[$samsung_models[0][0]+1]
$samsung_models_names[0] = $samsung_models[0][0]
For $i = $samsung_models[0][0] To 1 Step -1
    ;For $j = $samsung_models[0][1] To 1 Step -1
        $samsung_models_names[$i] = $samsung_models[$i][4]
    ;Next
Next
Global $samsung_models_list = "|"&_ArrayToString($samsung_models_names,"|",1)
;MsgBox($MB_SYSTEMMODAL,"","Samsung models: "&_ArrayToString($samsung_models_names,"|",1))
;_ArrayDisplay($samsung_models_names)

While 1
    Sleep(500)
    _ChangeDistance()
WEnd
GUIDelete()

You may compile it and see how it works. What i need - is to fill combo boxes created inside tabs dynamically (2nd combo depends what was selected in 1st combo etc...)

Link to comment
Share on other sites

Ok. I managed to recode my script so it uses now GUIGetMsg().

And what i am thinking of...

I can run throuh all array elements and copy needed ctrlID's to separate, 1D array (and i can run this every 1sec or something like that so new array will be up to date).

Since i am using guigetmsg() is it possible to somehow code:

Switch GUIGetMsg()
    Case $something
    ...
    Case ($val1 to $valN where N is size of array)
    ...
EndSwitch

Size of array can change any second (so number of control IDs can change).

Maybe somehow i can use dummy control here?

Link to comment
Share on other sites

misi0000,

One way to do it...

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***

#AutoIt3Wrapper_Add_Constants=n

local $aCTL[10]
local $gui010   =   guicreate('')
for $1 = 0 to ubound($aCTL) - 1
    $aCTL[$1] = guictrlcreatecheckbox('Checkbox # ' & $1, 20, $1*30, 100, 20)
next
                    guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        case $aCTL[0] to $aCTL[ubound($aCTL)-1]     ;   detect that one of the controls was actioned
            ; now find out which one
            for $1 = 0 to ubound($aCTL) - 1
                if guictrlread($aCTL[$1]) = $gui_checked then ConsoleWrite(guictrlread($aCTL[$1],1)  & ' checked' & @CRLF)
            next
    EndSwitch
WEnd

kylomas

edit: formatting

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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