Jump to content

(indirect var) = GUICtrlCreateCheckbox("c1", 10, 10, 120, 20)?


Mic
 Share

Go to solution Solved by kylomas,

Recommended Posts

Trying all sorts of indirection and Eval() to turn:

$check1 = GUICtrlCreateCheckbox("CHECKBOX 1", 10, 10, 120, 20)

into something like:

local $i = 1

local $indrctVar1 = "$check" & $i

Eval($indrctVar1) = GUICtrlCreateCheckbox("CHECKBOX 1", 10, 10, 120, 20) ; incorrect

tia.

Link to comment
Share on other sites

Use an array, and it will be a lot easier.

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

It's headache and really not very good practice ...

#include <GUIConstantsEx.au3>

GuiCreate("test")
local $i = 1
local $indrctVar1 = "check" & $i
Assign("indrctVar1", GUICtrlCreateCheckbox("CHECKBOX 1", 10, 10, 120, 20) )
$btn = GUICtrlCreateButton("Check", 20, 40, 50, 25)
GuiSetState()

While 1
   Switch GUIGetMsg()
       Case $GUI_EVENT_CLOSE
           ExitLoop
       Case $btn
        ;  GUICtrlSetState($check1, $GUI_CHECKED)
          GUICtrlSetState(Eval("indrctVar1"), $GUI_CHECKED)
   EndSwitch
Wend

It's much better to store the ids of the checkboxes in an array - and create them in a loop eventually

Edited by mikell
Link to comment
Share on other sites

  • Solution

Mic,

A simple example of checkbox ctlid's stored on an array...

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

#AutoIt3Wrapper_Add_Constants=n

local $ckb_cntrls[16], $msg

local $gui010 = guicreate('Checkbox Controls Using an Array for the Control ID')

for $1 = 1 to 15
    $ckb_cntrls[$1] = GUICtrlCreateCheckbox('CheckBox #' & stringformat('%02i',$1),95, $1 * 25,140,15)
Next

guisetstate()

while 1

    $msg = guigetmsg()

    switch $msg
        case $gui_event_close
            Exit
        case $ckb_cntrls[1] to $ckb_cntrls[ubound($ckb_cntrls) - 1]
            local $h = _winapi_getfocus()
            ConsoleWrite('Last action on ' & wingettitle($h) & @LF)
            for $1 = 1 to ubound($ckb_cntrls) - 1
                if guictrlread($ckb_cntrls[$1]) = $gui_checked then
                    ConsoleWrite('Checkbox #' & stringformat('%02i',$1) & ' checked' & @LF)
                else
                    ConsoleWrite('Checkbox #' & stringformat('%02i',$1) & ' unchecked' & @LF)
                endif
            next
    EndSwitch

wend

A little more complex demo of storing various control id's in an array and how to use them...

#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <date.au3>
#include <array.au3>
#include <GuiListView.au3>
#include <ComboConstants.au3>
#include <GuiComboBox.au3>

local $array_with_data[20][7]       ;   data array to demo filling various controls
local $array_for_controls[20][2]    ;   array of control ids / column 0 = buttons / column 1 = listview items

; fill data array

for $1 = 0 to ubound($array_with_data) - 1
    for $2 = 0 to ubound($array_with_data,2) - 1
        $array_with_data[$1][$2] = stringformat('%02i-%02i', $1+1, $2+1)
    Next
Next

; create listview header

local $hdr
for $1 = 0 to ubound($array_with_data,2) - 1
    $hdr &= 'Col ' & stringformat('%02i',$1+1) & '|'
Next
$hdr = StringTrimRight($hdr,1)

; create gui

local $gui010   =   guicreate('Examples of Array Usage with Controls and Data',900,700,-1,-1)
                    guictrlcreatelabel('A LISTVIEW Control is Similar to a Spread Sheet',10,10,370,25, _
                        bitor($SS_CENTER, $SS_CENTERIMAGE))
                    guictrlsetfont(-1,10,800)
                    guictrlsetcolor(-1,0xaa0000)
local $lv010    =   guictrlcreatelistview($hdr,10,35,370,300,-1,bitor($LVS_EX_FULLROWSELECT,$LVS_EX_GRIDLINES))
local $LVsize   =   controlgetpos($gui010,'',$lv010)
                    guictrlsetfont($lv010,8,800,-1,'courier new')
                    guictrlcreatelabel('ListBox Control',20,400,110,20,bitor($SS_CENTER, $SS_CENTERIMAGE))
                    guictrlsetfont(-1,10,800)
                    guictrlsetcolor(-1,0xaa0000)
local $lb010    =   guictrlcreatelist('',35,450,60,220)
                    guictrlcreatelabel('*-Col 2',45,425,200,20)
                    guictrlsetfont(-1,7,800,-1,'courier new')
                    guictrlcreatelabel('Combo Control',140,400,110,20,bitor($SS_CENTER, $SS_CENTERIMAGE))
                    guictrlsetfont(-1,10,800,-1)
                    guictrlsetcolor(-1,0xaa0000)
local $cb010    =   GUICtrlCreateCombo('',155,450,80,20)
                    _GUICtrlComboBox_SetCueBanner($cb010, "Selections...")
                    guictrlcreatelabel('*-Col 5',175,425,200,20)
                    guictrlsetfont(-1,7,800,-1,'courier new')
local $grp010   =   guictrlcreategroup('',450,65,50,500)
local $lbl010   =   guictrlcreatelabel('Press a Button to See the Corresponding Listview Row',400,15,150,50,$SS_CENTER)
                    guictrlsetfont(-1,9,800,-1,'Comic Sans MS')
                    guictrlcreatelabel('"Click on any entry in the Listview, List or Combo controls to see the controls value in the edit box"', _
                        125,520,200,200)
                    guictrlsetfont(-1,10,800,-1,'Comic Sans MS')

; create 1 button for each row of the array and put the buttons in the group control
for $1 = 0 to ubound($array_with_data) - 1
    $array_for_controls[$1][0] = GUICtrlCreateButton($1+1,460,85+$1*23,30,20)
    GUICtrlSetBkColor(-1, '0x' & hex(random(0,255,1),2) & hex(random(0,255,1),2) & hex(random(0,255,1),2))  ; just dick'in around setting random colors
    guictrlsetfont(-1,10,800)
next
                    GUICtrlCreateGroup('',99,99,1,1)
local $edt010   =   guictrlcreateedit('',550,70,320,600,bitor($ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
                    guictrlsetfont(-1,7,800,-1,'Lucida Console')
                    guictrlsetdata($edt010,'Application Started at ' & _now() & @crlf,1)

; populate the listview with array data

local $lv_item
for $1 = 0 to ubound($array_with_data) - 1
    for $2 = 0 to ubound($array_with_data,2) - 1
        $lv_item &= $array_with_data[$1][$2] & '|'
    next
    $array_for_controls[$1][1] = GUICtrlCreateListViewItem($lv_item,$lv010)
    $lv_item = ''
next

; adjust the listview to distribute the columns evenly across the control

for $1 = 0 to _GUICtrlListView_GetColumnCount($lv010)
    _GUICtrlListView_SetColumnWidth($lv010, $1, ($LVSize[2])/_GUICtrlListView_GetColumnCount($lv010)-3)
next

; populate the listbox with column 2 of the data array

local $lb_str
for $1 = 0 to ubound($array_with_data) - 1
    $lb_str &= $array_with_data[$1][1] & '|'
Next
$lb_str = stringtrimright($lb_str,1)
guictrlsetdata($lb010,$lb_str)

; populate the combo control with column 5 of the data array

$lb_str = ''
for $1 = 0 to ubound($array_with_data) - 1
    $lb_str &= $array_with_data[$1][4] & '|'
Next
$lb_str = stringtrimright($lb_str,1)
guictrlsetdata($cb010,$lb_str)

guisetstate()

local $msg
while 1
    $msg = guigetmsg()
    switch $msg
        case $GUI_EVENT_CLOSE
            Exit
        ; test for a listview item control
        case $array_for_controls[0][1] to $array_for_controls[ubound($array_for_controls) - 1][1]
            guictrlsetdata($edt010,@crlf & 'ListView Row from mouse select ------' & @crlf & guictrlread(guictrlread($lv010)) & @crlf,1)
        case $lb010
            guictrlsetdata($edt010,@crlf & 'Entry From List ------' & @crlf & guictrlread($lb010) & @crlf,1)
        case $cb010
            guictrlsetdata($edt010,@crlf & 'Entry From combo ------' & @crlf & guictrlread($cb010) & @crlf,1)
        ; test for a button control
        case $array_for_controls[0][0] to $array_for_controls[ubound($array_for_controls) - 1][0]
            for $1 = 0 to ubound($array_for_controls) - 1
                if $msg = $array_for_controls[$1][0] then
                    guictrlsetdata($edt010,@crlf & 'ListView Row from button push ------' & @crlf & _guictrllistview_getitemtextstring($lv010,$1) & @crlf,1)
                    exitloop
                EndIf
            next

    EndSwitch
WEnd

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

k found Assign and have it working with that. I do  create the checkboxes in a loop:

For $j = 0 To 14 Step 1
    For $i = 0 To 31 Step 1
       Assign($j & "_" & $i, GUICtrlCreateCheckbox(" ",30 + ($i * 16) + $bar,35 + ($j * 25),16,20,-1,-1))
       GUICtrlSetOnEvent(-1,"_cbClick")
       GUICtrlSetState(-1,4)
    Next
Next
 
If I create an array with the ids, how do I get those ids into the checkbox handle? tia.
 
 
..... just refreshed and am looking at Bohica's examples. Thanks! 
Edited by Mic
Link to comment
Share on other sites

I am using: 

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

Is there any way of translating:

$msg = guigetmsg()

    switch $msg

        case $gui_event_close

            Exit

        case $ckb_cntrls[1] to $ckb_cntrls[ubound($ckb_cntrls) - 1] .....

into OnEvent? Or is it better to use the message loop in general? Is message loop more powerful - seems like it "knows a lot more"?

Link to comment
Share on other sites

Mic,

I rarely use on event processing so this is probably not the best way, but...

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

#AutoIt3Wrapper_Add_Constants=n

Opt('GUIOnEventMode', 1)

local $ckb_cntrls[16], $msg

local $gui010 = guicreate('Checkbox Controls Using an Array for the Control ID')
GUISetOnEvent($GUI_EVENT_CLOSE, '_fini')

for $1 = 1 to 15
    $ckb_cntrls[$1] = GUICtrlCreateCheckbox('CheckBox #' & stringformat('%02i',$1),95, $1 * 25,140,15)
    guictrlsetonevent(-1,'_do_checkboxes')
Next

guisetstate()

while 1
    sleep( 86400000 )
wend

func _do_checkboxes()

    switch @gui_ctrlid
        case $ckb_cntrls[1] to $ckb_cntrls[ubound($ckb_cntrls) - 1]
            local $h = _winapi_getfocus()
            ConsoleWrite('Last action on ' & wingettitle($h) & @LF)
            for $1 = 1 to ubound($ckb_cntrls) - 1
                if guictrlread($ckb_cntrls[$1]) = $gui_checked then
                    ConsoleWrite('Checkbox #' & stringformat('%02i',$1) & ' checked' & @LF)
                else
                    ConsoleWrite('Checkbox #' & stringformat('%02i',$1) & ' unchecked' & @LF)
                endif
            next
    EndSwitch

endfunc

func _fini()
    Exit
endfunc

kylomas

edit:spelling

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