GroupBox Based on dynamic content

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tomharto
Posts: 6
Joined: 27 Jan 2021, 20:43

GroupBox Based on dynamic content

15 Feb 2021, 21:01

I'm trying to get a list of controls, to fill a GroupBox, but the number of controls is dynamic,

I'm using this GroupBox functions to wrap the controls, but I'm having trouble positioning the controls

Code: Select all

;************************** GroupBox *******************************
;
;	Adds and wraps a GroupBox around a group of controls in
;	the default Gui. Use the Gui Default command if needed.
;	For instance:
;
;		Gui, 2:Default
;
;	sets the default Gui to Gui 2.
;
;	Add the controls you want in the GroupBox to the Gui using
;	the "v" option to assign a variable name to each control. *
;	Then immediately after the last control for the group
;	is added call this function. It will add a GroupBox and
;	wrap it around the controls.
;
;	Example:
;
;	Gui, Add, Text, vControl1, This is Control 1
;	Gui, Add, Text, vControl2 x+30, This is Control 2
;	GroupBox("GB1", "Testing", 20, 10, "Control1|Control2")
;	Gui, Add, Text, Section xMargin, This is Control 3
;	GroupBox("GB2", "Another Test", 20, 10, "This is Control 3")
;	Gui, Add, Text, yS, This is Control 4
;	GroupBox("GB3", "Third Test", 20, 10, "Static4")
;	Gui, Show, , GroupBox Test
;
;	* The "v" option to assign Control ID is not mandatory. You
;	may also use the ClassNN name or text of the control.
;
;	Author: dmatch @ AHK forum
;	Date: Sept. 5, 2011
;
;********************************************************************

GroupBox(GBvName			;Name for GroupBox control variable
		,Title				;Title for GroupBox
		,TitleHeight		;Height in pixels to allow for the Title
		,Margin				;Margin in pixels around the controls
		,Piped_CtrlvNames	;Pipe (|) delimited list of Controls
		,FixedWidth=""		;Optional fixed width
		,FixedHeight="")	;Optional fixed height
{
	Local maxX, maxY, minX, minY, xPos, yPos ;all else assumed Global
	minX:=99999
	minY:=99999
	maxX:=0
	maxY:=0
	Loop, Parse, Piped_CtrlvNames, |, %A_Space%
	{
		;Get position and size of each control in list.
		GuiControlGet, GB, Pos, %A_LoopField%
		;creates GBX, GBY, GBW, GBH
		if(GBX<minX) ;check for minimum X
			minX:=GBX
		if(GBY<minY) ;Check for minimum Y
			minY:=GBY
		if(GBX+GBW>maxX) ;Check for maximum X
			maxX:=GBX+GBW
		if(GBY+GBH>maxY) ;Check for maximum Y
			maxY:=GBY+GBH

		;Move the control to make room for the GroupBox
		xPos:=GBX+Margin
		yPos:=GBY+TitleHeight+Margin ;fixed margin
		GuiControl, Move, %A_LoopField%, x%xPos% y%yPos%
	}
	;re-purpose the GBW and GBH variables
	if(FixedWidth)
		GBW:=FixedWidth
	else
		GBW:=maxX-minX+2*Margin ;calculate width for GroupBox
	if(FixedHeight)
		GBH:=FixedHeight
	else
		GBH:=maxY-MinY+TitleHeight+2*Margin ;calculate height for GroupBox ;fixed 2*margin

	;Add the GroupBox
	Gui, Add, GroupBox, v%GBvName% x%minX% y%minY% w%GBW% h%GBH%, %Title%
	return
}
Inside the GroupBox I'd like to see, but I can't figure out the positioning, can anyone help?

Code: Select all

#1: name        [Delete]
#2: othername   [Delete]

Current code

Code: Select all

; Load all the account files
AccControls := []
Loop %DataDirectory%\*.*
{
	 IniRead, Account, %A_LoopFileFullPath%, Account, Username
	 SVal := 5*A_Index

	 Gui, Add, Text, Section vAccLabel%A_Index% y+%SVal% , #%A_Index%: %Account%
	 Gui, Add, Button, Section vAccButton%A_Index% ys+%SVal% , Delete
	
	 AccControls.Push("AccLabel"A_Index)
	 AccControls.Push("AccButton"A_Index)
}

GroupBox("AccountsBox", "Accounts", 20, 10, Join("|", AccControls*))		;Create GB around "variables" Control1 and Control2 listed above

....

Join(sep, params*) {
    for index,param in params
        str .= sep . param
    return SubStr(str, StrLen(sep)+1)
}
dmatch
Posts: 49
Joined: 02 Oct 2013, 09:56

Re: GroupBox Based on dynamic content

16 Feb 2021, 14:09

Try this for x positioning of the buttons:

Code: Select all

Gui, Add, Button, Section vAccButton%A_Index% x+10, Delete
You might also want to change font sizes to help lineup button with the text left of it like this:

Code: Select all

Gui, font, s12
Gui, Add, Text, Section vAccLabel%A_Index% xMargin y+%SVal% , #%A_Index%: %Account%
Gui, Font, s8
Gui, Add, Button, Section vAccButton%A_Index% x+10, Delete
....
Gui, Font, s12
GroupBox("AccountsBox", "Accounts", 20, 10, Join("|", AccControls*))
Hope this helps

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], KilliK, mikeyww and 122 guests