Jump to content

Spacing between checkboxes.

Recommended Posts

Hello All, 

I'm pretty sure there has been another post on this, but I cannot find it. I'm unsure of the terminology that I would use, so I guess that is why I cannot find it. 

I'm searching for a way to space checkboxes apart from each other using the previous ones width. This would require me to fetch the previous checkbox width and use it to space the next one. I've tried using "ControlGetPos", but it only detects the width of the checkbox itself, not the checkbox plus the text. Here is what I'm seeing;

$checkbox1 = GUICtrlCreateCheckbox("/E", 20, 110)
$pos = ControlGetPos($hGUI, "", $checkbox1)
GUICtrlCreateCheckbox("/COPYALL", $pos[2], 110)


Any help would be appreciated. 


Edit: This is what I am looking to accomplish for reference, although without manually setting the width. 


Edited by ReconX
Link to post
Share on other sites

Hi ReconX :)
If you look at the example found in help file, topic GUICtrlCreateCheckbox, you'll notice this line :

Local $idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25)

But 185 is a too large width, compared to the checkbox size + its text.
So what could we do to automatically set a width corresponding exactly to the checkbox size + its "Standard Checkbox" text ?

The answer is : when creating the control, indicate a width = -1 and a size = -1, as shown in the following pic :


Now the text fits perfectly and you can get the unknow width & height like this :

Local $aPos = ControlGetPos($hGUI, "", $idCheckbox)
MsgBox(0, "Checkbox", "Position: " & $aPos[0] & ", " & $aPos[1] & @CRLF & "Size: " & $aPos[2] & ", " & $aPos[3])


After that, it's a piece of cake to place the other checkboxes exactly where you like.

Though "-1, -1" isn't clearly indicated in the help file, I guess they're the values that correspond to the 2 explanative lines found in the help file : "default text autofit in width" and "default text autofit in height"

Good luck

Link to post
Share on other sites

Thanks for the help. 

Oooof, I see my mistake though. It was picking up the correct width, but I forgot to add the 20px distance from the left. I've gotten it to work like this; 

$CheckBox1 = GUICtrlCreateCheckbox("/E", 20, 110, -1, -1)
        $pos_CheckBox1 = ControlGetPos($hGUI, "", $CheckBox1)
        $CheckBox2 = GUICtrlCreateCheckbox("/COPYALL", $pos_CheckBox1[0] + $pos_CheckBox1[2] + 10, 110, -1, -1)
        $pos_CheckBox2 = ControlGetPos($hGUI, "", $CheckBox2)
        $CheckBox3 = GUICtrlCreateCheckbox("/XJ", $pos_CheckBox2[0] + $pos_CheckBox2[2] + 10, 110, -1, -1)

I don't suppose there's any way to make an argument like this to inherit the location and then add the string to it; 

-1 + $pos_CheckBox[2]

Is there? I've tried + and &.

Link to post
Share on other sites

Melba23 StringSize UDF could also be used as well, find it useful for calculating font size

#include <GUIConstantsEx.au3>
;~ StringSize
;~ Author   : Melba23
;~ Download : https://www.autoitscript.com/forum/topic/114034-stringsize-m23-new-version-16-aug-11/
#include <StringSize.au3>

Global $g_aCheckbox[4][2] = [["","/X"],["","/COPYALL"],["","/FP"],["","/BYTES"]]
Global $g_iFontSize = 12
Global $g_iFontWeight = 400


Func Example()
    Local $hGUI = GUICreate("Example", 400, 200)
    GUISetFont($g_iFontSize, $g_iFontWeight)
    Local $iX = 10, $aStringSize
    For $i = 0 To UBound($g_aCheckbox) - 1
        $aStringSize = _StringSize($g_aCheckbox[$i][1], $g_iFontSize, $g_iFontWeight)
        $g_aCheckbox[$i][0] = GUICtrlCreateCheckbox($aStringSize[0], $iX, 10, $aStringSize[2] + 35, 25)
        GUICtrlSetBkColor(-1, 0x10FF00)
        $iX += $aStringSize[2] + 40
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close


Link to post
Share on other sites

Subz indicated a good way to do it using Melba23's StringSize UDF (I thought of it at 1st place but was too lazy to script it. Bravo Subz, so it's not really a "lazy Sunday" after all :D

Also my "-1 -1 discovery" isn't a discovery at all as it works fine if you don't pass any of these 2 arguments (as ReconX did in his 1st post). Nevertheless, it would be great to indicate in the help file that the default is -1, -1, in case someone needs to indicate those 2 arguments (automatic width & height) before accessing the Style argument (for example).

1 hour ago, ReconX said:

I don't suppose there's any way to make an argument like this to inherit the location and then add the string to it; 

-1 + $pos_CheckBox[2]

Is there? I've tried + and &.

I tried to play with :

; Parameters
; left : The left side of the control. If -1 is used then left will be computed according to GUICoordMode."

Opt("GUICoordMode", 1) ;1=absolute (default), 0=relative, 2=cell

But I couldn't make it in your example. Maybe somebody will ?

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...