Checked groupbox

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Checked groupbox

07 Mar 2018, 05:02

Hi

Is it possible to achieve a checked groupbox like this

Image

The article where this image is from mentions the following technique:
1. check whether the CheckBox is inside the GroupBox and, if it is, move it into the GroupBox‘s parent
2. move the CheckBox to the front of the stacking order so it doesn’t end up behind the GroupBox.
Is it possible to perform these steps in ahk?

Thanks
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Checked groupbox

07 Mar 2018, 05:54

I think to do this: move it into the GroupBox‘s parent, you have to have a parent-child relationship between Gui elements.
In VBA, I remember having seen those. In AHK not so much built-in but possible to add yourself, I think.
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Checked groupbox

07 Mar 2018, 09:37

You're going to have to use a normal checkbox without any text and do some gLabels in order to do what you want to do.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Checked groupbox

07 Mar 2018, 10:33

As Delta has already said, simply positioning a "nameless" checkbox relatively close to a section label and you should be done.
This one will eliminate a "keeping an already selected option even if its checkbox has been unchecked meanwhile" (something you see in your breakfast section):
Spoiler
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Checked groupbox

07 Mar 2018, 10:33

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force

    Gui, Add, GroupBox, w260 h100 Section
    Gui, Add, Checkbox, xs+5 ys gonCB_Click Checked hwndhCB1, Breakfast
    Gui, Add, Radio, xs+20 ys+20 w100 hwndhRB11, Toast
    Gui, Add, Radio, y+12 w100 hwndhRB12, Pancakes
    Gui, Add, Radio, y+12 w100 hwndhRB13, Pickles

    Gui, Add, GroupBox, xm w260 h100 Section
    Gui, Add, Checkbox, xs+5 ys gonCB_Click Checked hwndhCB2, Lunch
    Gui, Add, Radio, xs+20 ys+20 w100 hwndhRB21, Sandwich
    Gui, Add, Radio, y+12 w100 hwndhRB22, Soup
    Gui, Add, Radio, y+12 w100 hwndhRB23, Rack of Lamb
    Gui, Show

    Children := {}
    Children.Push({hwnd: hCB1})
    Children.Push({hwnd: hCB2})

    Children[1].Push(hRB11)
    Children[1].Push(hRB12)
    Children[1].Push(hRB13)

    Children[2].Push(hRB21)
    Children[2].Push(hRB22)
    Children[2].Push(hRB23)

Return

GuiClose:
ExitApp



;-------------------------------------------------------------------------------
onCB_Click(hwnd, GuiEvent, EventInfo, ErrLevel := "") {
;-------------------------------------------------------------------------------
    global Children
    SetFormat, Integer, h
    GuiControlGet, state,, % hwnd += 0
    Loop, 2
        If Children[A_Index].hwnd = hwnd {
            Index := A_Index
            Break
        }
    For each, Child in Children[Index]
        If Not (Child = hwnd)
            GuiControl, % (state ? "Enable":"Disable"), %Child%
}
Maybe that is all you need?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Checked groupbox

07 Mar 2018, 10:40

wolf_II wrote:Try this: ... Maybe that is all you need?
That's cool! Would you mind to sort out how to reset the RB section once you uncheck an already selected section? I've already created my own code (see above). But your's looks much more sophisticated. I'm always trying to get something new from those samples. Thx :)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Checked groupbox

07 Mar 2018, 10:54

Hi BoBo! Sure thing. I was about to mention that I prefer to have a named CheckBox, it is not intuitive to click on some text next to a checkbox and nothing happens.

Have you got an example how you would like that to happen?
example: uncheck => all radios will be cleared.

Code: Select all



;-------------------------------------------------------------------------------
onCB_Click(hwnd, GuiEvent, EventInfo, ErrLevel := "") {
;-------------------------------------------------------------------------------
    global Children
    SetFormat, Integer, h
    GuiControlGet, state,, % hwnd += 0
    Loop, 2
        If Children[A_Index].hwnd = hwnd {
            Index := A_Index
            Break
        }
    For each, Child in Children[Index]
        If Not (Child = hwnd) {
            GuiControl,, %Child%, 0 ; <<<< clear 
            GuiControl, % (state ? "Enable":"Disable"), %Child%
        }
}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Checked groupbox

07 Mar 2018, 11:03

@ wolf_II
"Have you got a example how you would like that to happen?". If you look at my spoilered code above you'll get the idea
"example: uncheck => all radios will be emptied." Yep, that's how I've done it. Thought that would be kinda common approach to clear a section eg. to prevent that no previously selected item will be "ordered" by mistake?!
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: Checked groupbox

07 Mar 2018, 17:19

Thanks for the solutions.

My main concern wasn't actually with the management of child controls (I was already doing that manually in an unsophisticated way) but rather to ensure the checkbox gets drawn on top of the groupbox. It seems the basic rule is that as long as the checkbox is added after the groupbox, it should appear on top of the groupbox. But I'm worried that this is a bit of a hack and will flicker or appear underneath on certain versions of windows , or on certain themes, or just different brand GPU's.

Is "move the CheckBox to the front of the stacking order so it doesn’t end up behind the GroupBox" equivalent to just adding the checkbox after the groupbox, or is there some Dll function that can explicitly move a control to the top of the stacking order?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 292 guests