RadioToggler: Radio buttons you can Uncheck

Post your working scripts, libraries and tools for AHK v1.1 and older
Bones
Posts: 28
Joined: 30 Oct 2013, 15:30

RadioToggler: Radio buttons you can Uncheck

09 Jan 2014, 16:17

RadioToggler: allows for Radio buttons that can be clicked off, like a Checkbox, but they still switch off other Radio buttons in the same group, like a Radio button.

Code: Select all

#SingleInstance force
#NoEnv
; RadioToggler: allows for Radio buttons that can be clicked off, like a Checkbox,
; but they still switch off other Radio buttons in the same group, like a Radio button.

ButtonCount := 0    ; You need some way to keep track of the classNN Button number for the Radio buttons you want to uncheck
CurrentButtonStatus := 0


Gui,Add, Radio,  vMyRadioGroup1 group gRadioToggler, good
++ButtonCount
Gui,Add, Radio, ys vMyRadioGroup2 gRadioToggler,  better
++ButtonCount
Gui,Add, Radio, ys vMyRadioGroup3  gRadioToggler, best
++ButtonCount
Gui,Add, Checkbox, ys , box one      ;  no need to put gRadioToggler for other Checkboxes or Radio buttons you don't wish to ever uncheck
++ButtonCount
Gui,Add, Checkbox, ys gRadioToggler, box two  ; but it doesn't hurt anything if you do put it in except is "uses up" your g-label
++ButtonCount
Gui, Show

loop, %ButtonCount%
{
	TogglerButton%A_Index% := 0
}

return

RadioToggler:    
{
; The variable CurrentButtonStatus will be zero or one representing the state of the button just BEFORE you clicked it 

	MouseGetPos,,,, TempNN           ;   TempNN is (ClassNN) like Button1, Button2, etc
	CurrentButtonStatus := Toggler%TempNN% ;  e.g. Toggler%TempNN% could be TogglerButton1

	If (CurrentButtonStatus = 0)
	{
		Toggler%TempNN% := 1
		GuiControl, ,%TempNN%,1
	}

	If (CurrentButtonStatus = 1)
	{
		Toggler%TempNN% := 0
		GuiControl, ,%TempNN%,0
	}

	loop, %ButtonCount%   ;  see note below
	{
		ControlGet, TogglerButton%A_Index%, Checked , , Button%A_Index%
	}
	;One of the Radio Buttons may have been ON prior to this Radio Button click.
	;If so, it switched off when this button was clicked.
	;If I were more clever, I would just switch that one button's TogglerButton variable.
	;But I'm not, so I'll just update the TogglerButton variable for all buttons
	;You can omit this ControlGet step but then you have to click twice to turn on a button
Return
}

Escape:
Guiclose:
GuiEscape:
Exitapp
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: RadioToggler: Radio buttons you can Uncheck

10 Jan 2014, 04:36

could you give an example how to use this script?
Bones
Posts: 28
Joined: 30 Oct 2013, 15:30

Re: RadioToggler: Radio buttons you can Uncheck

10 Jan 2014, 07:34

Guest10 wrote:could you give an example how to use this script?
To actually use this, you would:
1. delete all of my Gui, Add statements;
2. put the initializations at the beginning of your code;
3. add a ++ButtonCount every time you make a Checkbox or Radio button
4. put the g-label gRadioToggler into the options section of any Radio button which you want to have the ability to "turn off";
5. Put the RadioToggler subroutine at the end of your code.

I see you're signed in as Guest so I'm not sure of you're familiarity with AHK and programming in general, so no offense if this explanation is beneath your level.
Just click "select all" and paste to a new Notepad file, save with extension .ahk and run it.
There is an included example (which just makes 3 Radio buttons and 2 Checkboxes that don't do anything).

With Radio buttons, in general, once you have selected any Radio button (from a given group) you can never go back to having zero Radio buttons selected (from that Radio group). You can switch to another Radio button but there will always be one. But what if I change my mind and wish I had a "time machine" to go back in time to before I clicked the first Radio button. Regular Radio buttons don't let you do this.

All this program does is allow for Radio buttons to be turned off. Just click the same Radio button a few times in a row.
For the code below, I just made one change. I added a Radio button without the g-label. This 4th Radio is a typical Radio button.

Code: Select all

#SingleInstance force
#NoEnv
; RadioToggler: allows for Radio buttons that can be clicked off, like a Checkbox,
; but they still switch off other Radio buttons in the same group, like a Radio button.

ButtonCount := 0    ; You need some way to keep track of the classNN Button number for the Radio buttons you want to uncheck
CurrentButtonStatus := 0


Gui,Add, Radio,  vMyRadioGroup1 group gRadioToggler, good
++ButtonCount
Gui,Add, Radio, ys vMyRadioGroup2 gRadioToggler,  better
++ButtonCount
Gui,Add, Radio, ys vMyRadioGroup3  gRadioToggler, best
++ButtonCount
Gui,Add, Radio, ys vMyRadioGroup4  , this one will not turn off
++ButtonCount
Gui,Add, Checkbox, ys , box one      ;  no need to put gRadioToggler for other Checkboxes or Radio buttons you don't wish to ever uncheck
++ButtonCount
Gui,Add, Checkbox, ys gRadioToggler, box two  ; but it doesn't hurt anything if you do put it in except is "uses up" your g-label
++ButtonCount
Gui, Show

loop, %ButtonCount%
{
    TogglerButton%A_Index% := 0
}

return

RadioToggler:   
{
; The variable CurrentButtonStatus will be zero or one representing the state of the button just BEFORE you clicked it 

    MouseGetPos,,,, TempNN           ;   TempNN is (ClassNN) like Button1, Button2, etc
    CurrentButtonStatus := Toggler%TempNN% ;  e.g. Toggler%TempNN% could be TogglerButton1

    If (CurrentButtonStatus = 0)
    {
        Toggler%TempNN% := 1
        GuiControl, ,%TempNN%,1
    }

    If (CurrentButtonStatus = 1)
    {
        Toggler%TempNN% := 0
        GuiControl, ,%TempNN%,0
    }

    loop, %ButtonCount%   ;  see note below
    {
        ControlGet, TogglerButton%A_Index%, Checked , , Button%A_Index%
    }
    ;One of the Radio Buttons may have been ON prior to this Radio Button click.
    ;If so, it switched off when this button was clicked.
    ;If I were more clever, I would just switch that one button's TogglerButton variable.
    ;But I'm not, so I'll just update the TogglerButton variable for all buttons
    ;You can omit this ControlGet step but then you have to click twice to turn on a button
Return
}

Escape:
Guiclose:
GuiEscape:
Exitapp
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: RadioToggler: Radio buttons you can Uncheck

10 Jan 2014, 15:30

thanks! i'll follow your steps. :geek:

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: fiendhunter and 232 guests