Jump to content
youtuber

How to get Checkbox Captions names?

Recommended Posts

I want to get extensions from checkbox names

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 195, 125)
$Button1 = GUICtrlCreateButton("ok", 86, 64, 75, 34)
Dim $Checkbox[3]
$Checkbox[0] = GUICtrlCreateCheckbox(".JPG", 14, 32, 49, 17)
$Checkbox[1] = GUICtrlCreateCheckbox(".PNG", 68, 32, 49, 17)
$Checkbox[2] = GUICtrlCreateCheckbox(".GIF", 127, 32, 49, 17)

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            For $i = 0 To UBound($Checkbox) - 1
                If GUICtrlRead($Checkbox[$i]) = $GUI_CHECKED Then ConsoleWrite($Checkbox[$i] & @CRLF)
            Next
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites
Posted (edited)

@youtuber

Create a two-dimension array; in the first column, you store the ControlID of the checkbox, and in the second column you store its label, so when you use GUICtrlRead() in a For...Next loop, you just need to use

For $i = 0 To UBound($arrCheckBoxes) - 1 Step 1
    If GUICtrlRead($arrCheckBoxes[$i][0]) = $GUI_CHECKED Then ConsoleWrite($arrCheckBoxes[$i][1] & @CRLF)
    Next

to retrieve the information about the label of the checked checkbox(es) :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Posted (edited)

@mikell Thanks very good :)

@FrancescoDiMuro Did you mean a two dimension array like this?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 195, 125)
$Button1 = GUICtrlCreateButton("ok", 86, 64, 75, 34)
Dim $Checkbox[3][2]
$Checkbox[0][".jpg"] = GUICtrlCreateCheckbox(".JPG", 14, 32, 49, 17)
$Checkbox[1][".png"] = GUICtrlCreateCheckbox(".PNG", 68, 32, 49, 17)
$Checkbox[2][".gif"] = GUICtrlCreateCheckbox(".GIF", 127, 32, 49, 17)

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            For $i = 0 To UBound($Checkbox) - 1 Step 1
                If GUICtrlRead($Checkbox[$i][0]) = $GUI_CHECKED Then ConsoleWrite($Checkbox[$i][1] & @CRLF)
            Next
    EndSwitch
WEnd

 

Edited by youtuber

Share this post


Link to post
Share on other sites
Posted (edited)

@youtuber

Something like this:

$arrCheckBoxes[0][0] = GUICtrlCreateCheckbox(...)
$arrCheckboxes[0][1] = ".JPG"

; And so on for as many checkboxes you want to set

By the way, @mikell gives you the easiest solution, as long as the text you display is the one you want to retrieve; else, you need to do something like above :)

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Posted (edited)

@youtuber

Take the array as a table, in which the first dimension is the row index, and the second dimension is the column index.

In the first column, you store the ControlID of the checkbox, so, you do something like this:

; Global array with three rows and two columns
Global $arrCheckBoxes[3][2]

; Store the ControlID of the CheckBox in the first row and first column of the array
$arrCheckBoxes[0][0] = GUICtrlCreateCheckBox(...)

; Store the additional information in the first row and the second column of the array
$arrCheckBoxes[0][1] = ".JPG"

; Go on as many rows you have (three in this case)

If you want to know more about arrays, you can find a Wiki here and a more in-depth thread here.

By the way, feel free to ask if you need any help :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@FrancescoDiMuro I still can't figure it out anyway I don't really understand :(

not happening

Global $Checkbox[3][2]

; Store the ControlID of the CheckBox in the first row and first column of the array
$Checkbox[0][0] = GUICtrlCreateCheckBox(".JPG", 14, 32, 49, 17)
$Checkbox[0][0] = GUICtrlCreateCheckBox(".PNG", 68, 32, 49, 17)
$Checkbox[0][0] = GUICtrlCreateCheckBox(".GIF", 127, 32, 49, 17)

; Store the additional information in the first row and the second column of the array
$Checkbox[0][1] = ".JPG"
$Checkbox[0][2] = ".PNG"
$Checkbox[0][3] = ".GIF"

or

Global $Checkbox[3][2]

; Store the ControlID of the CheckBox in the first row and first column of the array
$Checkbox[0][0] = GUICtrlCreateCheckBox(".JPG", 14, 32, 49, 17)
$Checkbox[0][0] = GUICtrlCreateCheckBox(".PNG", 68, 32, 49, 17)
$Checkbox[0][0] = GUICtrlCreateCheckBox(".GIF", 127, 32, 49, 17)

; Store the additional information in the first row and the second column of the array
$Checkbox[0][1] = [".JPG", ".PNG", ".GIF"]

or

Global $Checkbox[3][2]

; Store the ControlID of the CheckBox in the first row and first column of the array
$Checkbox[0][0] = GUICtrlCreateCheckBox(".JPG", 14, 32, 49, 17) & GUICtrlCreateCheckBox(".PNG", 68, 32, 49, 17) & GUICtrlCreateCheckBox(".GIF", 127, 32, 49, 17)

; Store the additional information in the first row and the second column of the array
$CheckBox[0][1] = ["JPG", "PNG", "GIF"]

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

Like a so

Global $Checkbox[3][2]

; Store the ControlID of the CheckBox in the first row and first column of the array
$Checkbox[0][0] = GUICtrlCreateCheckBox(".JPG", 14, 32, 49, 17)
$Checkbox[1][0] = GUICtrlCreateCheckBox(".PNG", 68, 32, 49, 17)
$Checkbox[2][0] = GUICtrlCreateCheckBox(".GIF", 127, 32, 49, 17)

; Store the additional information in the first row and the second column of the array
$Checkbox[0][1] = ".JPG"
$Checkbox[1][1] = ".PNG"
$Checkbox[2][1] = ".GIF"

 

Maybe this will be helpful visualize aid to understand how the array indexes are used.  

Const $iCheckboxCount = 3, $iCheckboxArrayParams = 2   ;Define How Many Checkboxes and How Many "Parameters" about the Checkbox to store

Enum $iCheckboxParamCtrlID, $iCheckboxParamText ;Define the Array Indexes for the Checkbox "Parameters" (Checkbox Ctrl IDs and Checkbox Text values)
Enum $iCheckbox1, $iCheckbox2, $iCheckbox3  ;Define The Checkbox Instances

Global $Checkbox[$iCheckboxCount][$iCheckboxArrayParams]    ;Build Array for storing Checkboxes and "Parameters"

$Checkbox[$iCheckbox1][$iCheckboxParamCtrlID] = GUICtrlCreateCheckBox(".JPG", 14, 32, 49, 17)
$Checkbox[$iCheckbox2][$iCheckboxParamCtrlID] = GUICtrlCreateCheckBox(".PNG", 68, 32, 49, 17)
$Checkbox[$iCheckbox3][$iCheckboxParamCtrlID] = GUICtrlCreateCheckBox(".GIF", 127, 32, 49, 17)

$Checkbox[$iCheckbox1][$iCheckboxParamText] = ".JPG"
$Checkbox[$iCheckbox2][$iCheckboxParamText] = ".PNG"
$Checkbox[$iCheckbox3][$iCheckboxParamText] = ".GIF"

I'm not saying this is how it should be done; it's just to help illustrate. 
Nothing wrong with doing it this way though (using enums to make meaningful/readable names for array index values).  I will say that declaring each checkbox index (i.e. $iCheckbox1, $iCheckbox2, etc.) is overkill, especially if the Array name is already meaningful to denote what is contained in the array.

 

Having said all of that, I still don't particularly see the value or need to store the text when it can easily be retrieved on demand--with negligible performance impact--using the GUICtrlRead function @mikell laid out above.

Edited by spudw2k

Share this post


Link to post
Share on other sites

Or use the extended GuiCtrlRead

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 195, 125)
$Button1 = GUICtrlCreateButton("ok", 86, 64, 75, 34)
Dim $Checkbox[3]
$Checkbox[0] = GUICtrlCreateCheckbox(".JPG", 14, 32, 49, 17)
$Checkbox[1] = GUICtrlCreateCheckbox(".PNG", 68, 32, 49, 17)
$Checkbox[2] = GUICtrlCreateCheckbox(".GIF", 127, 32, 49, 17)

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            For $i = 0 To UBound($Checkbox) - 1
                If GUICtrlRead($Checkbox[$i]) = $GUI_CHECKED Then ConsoleWrite(GuiCtrlRead($Checkbox[$i], $GUI_READ_EXTENDED) & @CRLF)
            Next
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites
Posted (edited)

@youtuber

You've been suggested to read a tutorial about arrays, because, even with a sample, you misundertood the difference between rows and columns in your first script, then treat a two-dimension array like a one-dimension array when assigning text in the first column in your second script, and finally treat the two-dimension like a one-dimension array, both for the checkboxes creation and texts assignment.

I suggest you once again to read carefully the array tutorials, and to try with simpler examples to get into them.

@spudw2k @Rex

The whole speech was about letting the user experiment another (possibly) useful way to do what he was trying to do.

He was already pointed out to a solution (the @mikell one), and so if he was keep trying the other solution, or he didn't tried the @mikell script, or he it isn't what he was needing.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Posted (edited)
1 hour ago, FrancescoDiMuro said:

The whole speech was about letting the user experiment another (possibly) useful way to do what he was trying to do.

Understood.  It looked like he was trying, but wasn't quite there.  After seeing some effort on his part I didn't think it was unreasonable to show where he was incorrect and perhaps shed light in a different way to help him understand

1 hour ago, FrancescoDiMuro said:

He was already pointed out to a solution (the @mikell one)

Agreed, but sometimes I have even been guilty of missing help someone gave me in the past and it took another member to point that out to me.

 

I want to commend @Rex though for not using a magic number in his example. ;)

Edited by spudw2k

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

×
×
  • Create New...