Sign in to follow this  
Followers 0
musahi0128

How to create GUI Control by looping the code

11 posts in this topic

#1 ·  Posted (edited)

Hello forum, here come some question.

By using loop below i succesfully create all control that i need, but in the end when i try to run the function to check each checkbox, the only valid checkbox handle is $checkbox, not something like $checkbox1, $checkbox2, ... etc. Can someone explain what's wrong and how to make this work?

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;#include "D:\Apps\Windows\Project\hmholv\main.variables.au3"
;#include "D:\Apps\Windows\Project\hmholv\main.functions.au3"
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1", -1, -1, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$DS_MODALFRAME))
For $i = 0 to 7
    $Label = "$Label"&$i+1
    $Label = GUICtrlCreateLabel("Slot #", 10, 14+(25*$i), 32, 17)
    $Input = "$Input"&$i+1
    $Input = GUICtrlCreateInput($i+1, 42, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Updown = "$Updown"&$i+1
    $Updown = GUICtrlCreateUpdown($Input)
    $Combo = "$Combo"&$i+1
    $Combo = GUICtrlCreateCombo("", 91, 10+(25*$i), 128, 25, BitOR($GUI_SS_DEFAULT_COMBO,$CBS_SIMPLE,$CBS_DISABLENOSCROLL))
    ;_DboxVal($Tools)
    $Label = "$Label"&$i+2
    $Label = GUICtrlCreateLabel("Qty", 223, 14+(25*$i), 20, 17)
    $Input = "$Input"&$i+2
    $Input = GUICtrlCreateInput("1", 243, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Updown = "$Updown"&$i+2
    $Updown = GUICtrlCreateUpdown($Input)
    $Checkbox = "$Checkbox"&$i
    $Checkbox = GUICtrlCreateCheckbox("", 291, 9+(25*$i), 23, 23)
    GUICtrlGetHandle(-1)
Next
$Button1 = GUICtrlCreateButton("Evaluate", 243-1, 209)
GUICtrlSetOnEvent(-1, "_Evaluate")

Func _Evaluate()
;   If GUICtrlRead($Checkbox1)=1 Then; << This is not working, i want this
        If GUICtrlRead($Checkbox)=1 Then; << This is working, i don't need this
        MsgBox(0, "", "Haha")
    EndIf   
EndFunc

GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
Func Close()
    Exit
EndFunc

GUISetState(@SW_SHOW)

While 1
WEnd

Sorry for my bad English. Regards :)

Edited by musahi0128

Share this post


Link to post
Share on other sites



First off, I notice that you are defining your $Checkbox variable as a String, and then turning right around and defining the same variable again as a checkbox control. Try something like this:

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;#include "D:\Apps\Windows\Project\hmholv\main.variables.au3"
;#include "D:\Apps\Windows\Project\hmholv\main.functions.au3"
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1", -1, -1, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$DS_MODALFRAME))
Local $aBoxes[8]

For $i = 0 to 7
    $Label = "$Label"&$i+1
    $Label = GUICtrlCreateLabel("Slot #", 10, 14+(25*$i), 32, 17)
    $Input = "$Input"&$i+1
    $Input = GUICtrlCreateInput($i+1, 42, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Updown = "$Updown"&$i+1
    $Updown = GUICtrlCreateUpdown($Input)
    $Combo = "$Combo"&$i+1
    $Combo = GUICtrlCreateCombo("", 91, 10+(25*$i), 128, 25, BitOR($GUI_SS_DEFAULT_COMBO,$CBS_SIMPLE,$CBS_DISABLENOSCROLL))
    ;_DboxVal($Tools)
    $Label = "$Label"&$i+2
    $Label = GUICtrlCreateLabel("Qty", 223, 14+(25*$i), 20, 17)
    $Input = "$Input"&$i+2
    $Input = GUICtrlCreateInput("1", 243, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Updown = "$Updown"&$i+2
    $Updown = GUICtrlCreateUpdown($Input)
    ;$Checkbox = "$Checkbox"&$i Get rid of this
    $aBoxes[$i] = GUICtrlCreateCheckbox("", 291, 9+(25*$i), 23, 23)
    GUICtrlGetHandle(-1)
Next
$Button1 = GUICtrlCreateButton("Evaluate", 243-1, 209)
GUICtrlSetOnEvent(-1, "_Evaluate")

Func _Evaluate()
    For $i = 0 To 7
        If GUICtrlRead($aBoxes[$i]) = 1 Then MsgBox(0, "", "CheckBox " & $i & " checked!")
    Next
EndFunc

GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
Func Close()
    Exit
EndFunc

GUISetState(@SW_SHOW)

While 1
WEnd

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

To do the for loop with dynamic controls, do it like so:

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;#include "D:\Apps\Windows\Project\hmholv\main.variables.au3"
;#include "D:\Apps\Windows\Project\hmholv\main.functions.au3"
Opt("GUIOnEventMode", 1)

Local $Ctrl[8][16]

$Form1 = GUICreate("Form1", -1, -1, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$DS_MODALFRAME))
For $i = 0 to 7
    $Ctrl[$i][0] = "$Label"&$i+1
    $Ctrl[$i][1] = GUICtrlCreateLabel("Slot #", 10, 14+(25*$i), 32, 17)
    $Ctrl[$i][2] = "$Input"&$i+1
    $Ctrl[$i][3] = GUICtrlCreateInput($i+1, 42, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Ctrl[$i][4] = "$Updown"&$i+1
    $Ctrl[$i][5] = GUICtrlCreateUpdown($Ctrl[$i][3])
    $Ctrl[$i][6] = "$Combo"&$i+1
    $Ctrl[$i][7] = GUICtrlCreateCombo("", 91, 10+(25*$i), 128, 25, BitOR($GUI_SS_DEFAULT_COMBO,$CBS_SIMPLE,$CBS_DISABLENOSCROLL))
    ;_DboxVal($Tools)
    $Ctrl[$i][8] = "$Label"&$i+2
    $Ctrl[$i][9] = GUICtrlCreateLabel("Qty", 223, 14+(25*$i), 20, 17)
    $Ctrl[$i][10] = "$Input"&$i+2
    $Ctrl[$i][11] = GUICtrlCreateInput("1", 243, 10+(25*$i), 45, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
    $Ctrl[$i][12] = "$Updown"&$i+2
    $Ctrl[$i][13] = GUICtrlCreateUpdown($Ctrl[$i][11])
    $Ctrl[$i][14] = "$Checkbox"&$i
    $Ctrl[$i][15] = GUICtrlCreateCheckbox("", 291, 9+(25*$i), 23, 23)
    GUICtrlGetHandle(-1)
Next
$Button1 = GUICtrlCreateButton("Evaluate", 243-1, 209)
GUICtrlSetOnEvent(-1, "_Evaluate")

Func _Evaluate()
  If GUICtrlRead($Ctrl[2][15])=1 Then; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Try this now
         MsgBox(0, "", $Ctrl[2][14] & " is checked")
     Else
         MsgBox(0, "", $Ctrl[2][14] & " is not checked")
     EndIf
 EndFunc
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
Func Close()
    MsgBox(0, "", $Ctrl[3][8])
    Exit
EndFunc

GUISetState(@SW_SHOW)

While 1
WEnd
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

 

First off, I notice that you are defining your $Checkbox variable as a String, and then turning right around and defining the same variable again as a checkbox control. Try something like this:

 

I think he is trying to add a dynamic label to it, and is mistakenly adding it into the same variable holding the combobox etc. The 2D array shown in my post above is one way of doing it. ;)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

But he also wants to check all checkboxes for which one is checked. The code you posted only checks for one. Also, as you're not going to be doing any validation on a label (or any control but checkboxes, according to the OP) why go through the trouble of creating a multi-dimensional array and assigning every control a variable when it will never be used?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

But he also wants to check all checkboxes for which one is checked. The code you posted only checks for one. Also, as you're not going to be doing any validation on a label (or any control but checkboxes, according to the OP) why go through the trouble of creating a multi-dimensional array and assigning every control a variable when it will never be used?

 

True, I only check for one (it was for example to show it works). Because, we cannot predict what the OP wants for the future, and it could be using these controls (we don't know). It was really just for example purposes to show OP how to create a 2D array with dynamic content. ;)

EDIT: JLogan, on your example it is saving each Ctrl into a variable, but then looping over and overwriting that CtrlID. Why do you still have this if we are creating more than 2 comboboxes? We will never be able to read but from the last combobox, checkbox, and input. I understand OP just wants the checkbox values and your $aBoxes[$i] represents creating them dynamically. But, they will never be able to read a value from any of these comboboxes unless they use a 1D or 2D array. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

If you are meaning the Labels, Updowns, etc. it is a straight copy of the OP's snippet. Again, that "teach a man to fish" tact, as I pointed out in my response he was overwriting variables.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If you are meaning the Labels, Updowns, etc. it is a straight copy of the OP's snippet. Again, that "teach a man to fish" tact, as I pointed out in my response he was overwriting variables.

 

True.. but one small change is not teaching one to fish, its giving them better bait (that is my opinion though). ;)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

And in my opinion your opinion has derailed the OP's thread enough. My apologies to the OP, there is not usually this snotty back and forth on the forum.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

And in my opinion your opinion has derailed the OP's thread enough. My apologies to the OP, there is not usually this snotty back and forth on the forum.

 

Harsh.. My apologies.


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

In the end I just gave up on using variable as control handle, using the exact id of the control in exchange. And because i move on and use tab, i create the tab with variable as handle, get the id of it and start counting by for statement.

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
Global $InputStyle = BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER)
$Form1 = GUICreate("Fill the fridge", 328, 274, -1, -1, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX, $WS_EX_TOPMOST), -1, WinGetHandle("Program Manager"))
$Menu1 = GUICtrlCreateMenu("File")
$MenuItem1 = GUICtrlCreateMenuItem("See what in fridge", $Menu1)
$MenuItem2 = GUICtrlCreateMenuItem("", $Menu1)
$MenuItem3 = GUICtrlCreateMenuItem("Close the fridge", $Menu1)
$Tab = GUICtrlCreateTab(0, 0, 330, 312)
$TabSheet1 = GUICtrlCreateTabItem("Main fridge")

; --- here is our discussion [start] ---
For $i = 0 to 9
    GUICtrlCreateCheckbox("Slot #", 10, 28+(22*$i))
    GUICtrlCreateInput($i+1, 59, 28+(22*$i), 45, 21, $InputStyle)
    GUICtrlSetLimit(-1, 2, 1)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 30, 1)
    GUICtrlCreateCombo("Apple", 108, 28+(22*$i), 141, 25, BitOR($GUI_SS_DEFAULT_COMBO,$CBS_SIMPLE,$CBS_DISABLENOSCROLL))
    GUICtrlSetData(-1, "Banana|Mango|Melon|Orange")
    GUICtrlCreateLabel("Qty", 253, 31+(22*$i), 20, 17)
    GUICtrlCreateInput("1", 273, 27+(22*$i), 45, 21, $InputStyle)
    GUICtrlSetLimit(-1, 2, 1)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 99, 1)
Next

Func PeekInside()
    ; Get the first Checkbox id using $TabSheet as reference.
    $Checkbox = $TabSheet1+1
    Local $Msg
    For $i = 0 to 9
        ; Including the first Checkbox, there is 7 control between first and the second : Checkbox(0), Input(1), UpDown(2), Combo(3), Lable(4), Input(5), UpDown(6)
        If BitAND(GUICtrlRead($Checkbox+($i*7)),$GUI_CHECKED) Then
            $a = GUICtrlRead($Checkbox+1+($i*7)); $Checkbox+1 for read Input(1)
            $b = GUICtrlRead($Checkbox+3+($i*7)); $Checkbox+3 for read Combo(3)
            $c = GUICtrlRead($Checkbox+5+($i*7)); $Checkbox+5 for read Input(5)
            $Msg &= "> " & $c & " " & $b & " in fridge slot " & $a & ";" & @CRLF
        EndIf
    Next
    If $Msg = "" Then
        MsgBox(0, "", "Nothing")
    Else
        MsgBox(0, "", "Ther is : " & @CRLF & $Msg & "in the fridge")
    EndIf
EndFunc
; --- here is our discussion [finish] ---

Func Close()
    Exit
EndFunc

GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
GUICtrlSetOnEvent($MenuItem1, "PeekInside")
GUICtrlSetOnEvent($MenuItem3, "Close")
GUISetState(@SW_SHOW)

While 1
WEnd

Am i violate any of forum rules by using different snippet for answering? please someone advise so i can be sure to mark this thread as solved.

It's great to teach someone to fish, and give him a bait for one try. But when he does'nt get it in the end, what's the point?

Thanks, and be cool boys :v

Edited by musahi0128

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
Sign in to follow this  
Followers 0