Page 1 of 1

Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 05:01
by Albireo
How to make a GUI button disabled?

With this instruction the button disappears completely .: Gui Add, Button, hidden vButtOK, OK!
A small test program

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 05:45
by Odlanir

Code: Select all

Gui 1: Add, Button, x150 y200 h40 w100 +Center Disabled vButtOK, OK!

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 06:13
by Albireo
Yes! (Thanks)
Activating the button is no problem. GuiControl 1: Enable, ButtOK
But to focus on the button I got stuck. GuiControl 1: Focus, ButtOK (nothing happens)
a new test script
(123 is the valid value - everything else is incorrect values)

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 08:05
by boiler
Albireo wrote:
04 Apr 2020, 06:13
Activating the button is no problem. GuiControl 1: Enable, ButtOK
But to focus on the button I got stuck. GuiControl 1: Focus, ButtOK (nothing happens)
It actually does get focus. Notice that the caret no longer appears in the edit box. It just doesn’t visually indicate that it has focus until you activate another window and then activate the GUI again for some reason.

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 08:32
by Albireo
boiler wrote:
04 Apr 2020, 08:05
...It actually does get focus. Notice that the caret no longer appears in the edit box...
Yes, focus is moved from the edit field (but not to the button)
If I hit Enter (and the button was active) I wished it was pressed.
It works if I press TAB and activate the button - Now the button is pressed when Enter is pressed.

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 08:39
by Hellbent

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 09:17
by Albireo
Thanks!

Yes! GuiControl, 1:+Default, ButtOK works in one way.
But, now I got another function of the program.

My desire is, when the input is correct (123 in this case) and Enter is pressed.
A result is created and can be reviewed. (the message Correct number! in this example)
The next time Enter is pressed - the button is pressed.
(With +default the button is directly pressed)

Re: Make the GUI button disabled / grayed

Posted: 04 Apr 2020, 14:38
by Hellbent
Albireo wrote:
04 Apr 2020, 09:17
Thanks!

Yes! GuiControl, 1:+Default, ButtOK works in one way.
But, now I got another function of the program.

My desire is, when the input is correct (123 in this case) and Enter is pressed.
A result is created and can be reviewed. (the message Correct number! in this example)
The next time Enter is pressed - the button is pressed.
(With +default the button is directly pressed)
One way you could do it is by using context-sensitive hotkeys and a variable that switches states

For example.

Code: Select all

#If (!Var)

Enter::
	if(something something blah blah){
		msgbox, yada yada
		var := 1
	}
	return
	
#If (var)

Enter::
	GuiControl, 1:+Default, ButtOK
	var := 0
	return
	
#If	

Re: Make the GUI button disabled / grayed

Posted: 05 Apr 2020, 05:09
by Albireo
Thank you @Hellbent !
I don't understand what's happens in more "complicated" cases (like mine) the AHK-doc of #If
Something like that .:

Code: Select all

#If WinActive( "ahk_id " GuiHwnd ) 
~NumpadEnter::
~Enter::
	GuiControlGet FocusOn, FocusV
...

#If (!Flag)
NumpadEnter::
Enter::
...

#If (Flag)
NumpadEnter::
Enter::
...
#If
1) What is the difference between ~Enter:: and Enter::?

2) The first #if WinActive..., ~Enter is executed if Enter is pressed and the specific ahk_id GuiHwnd window is active.
But must the first criterion for #if WinActive .. be met in order for the next #if (!Flag) to apply?
(Is it the same as #If ( WinActive( "ahk_id " GuiHwnd ) and !Flag ))

3) When is #if (!Flag) executed /checked? (When the variable Flag is changing the value?)

Re: Make the GUI button disabled / grayed

Posted: 05 Apr 2020, 08:04
by boiler
Albireo wrote: 1) What is the difference between ~Enter:: and Enter::?
Per the documentation on ~ in hotkey modifier symbols, "When the hotkey fires, its key's native function will not be blocked (hidden from the system)." So pressing Enter will have its usual effect in addition to what the hotkey routine does, where without it, it only performs what the hotkey routine does.
Albireo wrote: 2) The first #if WinActive..., ~Enter is executed if Enter is pressed and the specific ahk_id GuiHwnd window is active.
But must the first criterion for #if WinActive .. be met in order for the next #if (!Flag) to apply?
(Is it the same as #If ( WinActive( "ahk_id " GuiHwnd ) and !Flag ))
No, they are evaluated independently. If they happen to both evaluate as true, per the #IfWinActive documentation on variant hotkeys, which also applies to #If: "If more than one variant is eligible to fire, only the one closest to the top of the script will fire."
Albireo wrote: 3) When is #if (!Flag) executed /checked? (When the variable Flag is changing the value?)
It is evaluated at the time when the hotkey is pressed (although there are some exceptions where it might also be evaluated).

Re: Make the GUI button disabled / grayed

Posted: 05 Apr 2020, 10:34
by Hellbent
@Albireo

If you find using #If too problematic or difficult to implement, you can just use the same concept of using the variable as a extra if statement in your routine.

Code: Select all

#If WinActive( "ahk_id " GuiHwnd ) 
~NumpadEnter::
~Enter::
	GuiControl, 1:-Default, ButtOK
	GuiControlGet FocusOn, FocusV
	if ( FocusOn = "Inp" ){	
		MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, The Focus is on ( %Inp% )
		If ( Inp = 123 && !Var){	
			GuiControl 1: Show, ButtOK
			MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Number %Inp% is correct!
			Var := 1
		}else if(Var){
			GuiControl, 1:+Default, ButtOK
			Var := 0
		}else{	
			MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Wrong input! ( %InputVal% )
			GuiControl 1:, Inp
		}
		Return
	}
#If

Re: Make the GUI button disabled / grayed

Posted: 05 Apr 2020, 12:18
by Albireo
Thanks for the explanation! (and example)

I have tried this with a #if solution like this .:
Program with #If WinActive(...
I never got this attempt to work fully. But it becomes an easier code to maintain with #if

Made another attempt with many If-statements.
Program many if-statements
This solution seems to fulfill my wish. (now is it only to implement this in my head program)

Code: Select all

#If WinActive( "ahk_id " GuiHwnd )
~NumpadEnter::
~Enter::
	GuiControl, 1:-Default, ButtOK
	GuiControlGet FocusOn, FocusV
	if (( FocusOn = "Inp" ) && StrLen(Inp) = "3" )
	{	If ( Inp = 123 )
		{	If !Flag
			{	Flag := True
				GuiControl 1: Enable, ButtOK
				
				Gui 1: Font, cGreen s14 Normal, Verdana
				GuiControl 1: Font, Result1
				GuiControl 1:, Result1, % "Correct number! (" StrLen(Inp) ")"
				; MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Number %Inp% is correct!
			}
			else
			{	Flag := False
				GuiControl 1:+Default, ButtOK
				GuiControl 1:, Result1	; Clear the message
			}
		}
		else
		{	Flag := False
			GuiControl 1: Disable, ButtOK
			
			Gui 1: Font, cRed s12 normal, Verdana
			GuiControl 1: Font, Result1
			GuiControl 1:, Result1, % "Wrong number! ( " Inp " )"
			; MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Wrong input! ( %Inp% )
		}
	}
	else
	{	Flag := False
		GuiControl 1: Disable, ButtOK
		
		Gui 1: Font, cRed s12 normal, Verdana
		GuiControl 1: Font, Result1
		GuiControl 1:, Result1, % "Wrong number! ( " Inp " )"
		; MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Wrong input! ( %Inp% )
	}
	Return
#If

Re: Make the GUI button disabled / grayed  Topic is solved

Posted: 05 Apr 2020, 12:33
by boiler
You can’t do the following from your first example because only the last directive before the hotkey would take effect:

Code: Select all

#If WinActive( "ahk_id " GuiHwnd )
#If ( !Flag )
But you could do this:

Code: Select all

 #If WinActive( "ahk_id " GuiHwnd ) && ( !Flag )

Re: Make the GUI button disabled / grayed

Posted: 05 Apr 2020, 15:42
by Albireo
Thanks!
Now I even got #If WinActive( .. to work in same way :clap:
Program .: #If WinActive( " ...

Code: Select all

#If WinActive( "ahk_id " GuiHwnd ) && ( !Flag )
~NumpadEnter::
~Enter::
	; Gui 1: Submit, NoHide
	GuiControl, 1: -Default, ButtOK
	GuiControlGet FocusOn, FocusV

	; If focus on the field "inp" and it's the first "Return" (Flag is "False")
	if ( FocusOn = "Inp" )
	{	If ( Inp = 123 )
		{	Flag := True
			GuiControl 1: Enable, ButtOK
			
			Gui 1: Font, cGreen s14 Normal, Verdana
			GuiControl 1: Font, Result1
			GuiControl 1:, Result1, % "Correct number!"
		}
		else
		{	Flag := False
			GuiControl 1: Disable, ButtOK
			
			Gui 1: Font, cRed s12 normal, Verdana
			GuiControl 1: Font, Result1
			GuiControl 1:, Result1, % "Wrong number! ( " Inp " )"
		}
	}
Return

#If ( Flag )
~NumpadEnter::
~Enter::
	If( StrLen(Inp) = "3" )
	{	Flag := False	
		GuiControl 1: Enable, ButtOK
		
		GuiControl, 1: +Default, ButtOK
		; MsgBox ,, Row.: %A_LineNumber% -> %A_ScriptName%, Enter is pressed and Flag is %Flag% (1)
	}
Return
#If