When the program starts, the button is not enabled as expected.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

When the program starts, the button is not enabled as expected.

18 Nov 2019, 09:28

Hello,
When Checkbox is selected, Edit is disabled and the button is enabled. Conversely, if Edit is empty, the button is disabled, otherwise the button is enabled.
But when the program starts, the button is not enabled as expected. Why?

Code: Select all

Gui, Add, Checkbox, gtest vtest
Gui, Add, Edit,gtest2 vtest2, TEST
Gui, Add, Button, vok, OK
Gui, Show, h200 w300
f()
return

test:
	GuiControlGet, test
	if ( test = 1 )
	{
		GuiControl, Disable, test2
		GuiControl, Enable, ok
	}
	else
	{
		GuiControl, Enable, test2
		GuiControlGet, test2
		if ( test2 = "" )
			GuiControl, Disable, ok
	}
return

test2:

	GuiControlGet, test2
	if ( test2 = "" )
		GuiControl, Disable, ok
	else
		GuiControl, Enable, ok
return

f()
{
	test := 1
	GuiControl,, test, 1
	GuiControl,, test2           ; Possible conflict

	if ( test = 1 )
		GuiControl, Enable, ok              ; Not enabled as expected
	return
}

I think that GuiControl,, test2 will cause the test2 subroutine to be executed, but after execution, it will continue to execute the following if statement, but the button is still not enabled.
Last edited by afe on 18 Nov 2019, 12:01, edited 1 time in total.
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: When the program starts, the button is not enabled as expected.

18 Nov 2019, 11:23

Try:

Code: Select all

Gui, Add, Checkbox, Checked gtest vtest
Gui, Add, Edit, Disabled gtest2 vtest2, TEST
Gui, Add, Button, Default vok gok, OK
Gui, Show, h200 w300
return

test:
    ControlGet, Cb, Checked
    if Cb
	{
		GuiControl, Disable, test2
		GuiControl, Enable, ok
		GuiControl, Focus, ok
	}
	else
	{
		GuiControl, Enable, test2
		GuiControl, Focus, test2
        GuiControl, Disable, ok
	}
return

ok:
    MsgBox You pressed OK Button!
Return

test2:

	;GuiControlGet, test2
	;if ( test2 = "" )
		;GuiControl, Disable, ok
	;else
		;GuiControl, Enable, ok
return

f()
{
	test := 1
	GuiControl,, test, 1
	GuiControl,, test2

	if ( test = 1 )
		GuiControl, Enable, ok
	return
}

GuiClose:
Esc::ExitApp
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: When the program starts, the button is not enabled as expected.

18 Nov 2019, 11:30

Thanks, but I want the button to be enabled when Edit is not empty.
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: When the program starts, the button is not enabled as expected.

18 Nov 2019, 11:39

For example, when the checkbox is not checked, if the Edit is not empty, the button is enabled, otherwise, if Edit is empty, the button is disabled.

My code basically implements it, but when the program starts to f(), GuiControl, Enable, ok fails to work as expected, which seems to conflict with GuiControl, test2, which is test2: .

Although it is possible to add a Sleep, 1000 before if ( test = 1 ) to make it work, I don't know why.
I think GuiControl, Enable, ok is the last command that should be executed before the program is initialized, but it seems that test2: is still executing.

I read the description of the thread in the manual, but I think that after the test2 subroutine is executed, the if ( test = 1 ) statement will continue to be executed, and the button will be enabled after the startup is completed, but the result is not.

https://www.autohotkey.com/docs/misc/Threads.htm


Or change the test2 subroutine to

Code: Select all

test2:
	GuiControlGet, test
	if ( test = 0 )
	{
		GuiControlGet, test2
		if ( test2 = "" )
			GuiControl, Disable, ok
		else
			GuiControl, Enable, ok
	}
return
, although the program will execute as expected, I still want to know why the if ( test = 1 ) statement does not make the button enabled.
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: When the program starts, the button is not enabled as expected.

18 Nov 2019, 12:13

Not sure if I've done what you want:

Code: Select all

Gui, Add, Checkbox, gtest vtest
Gui, Add, Edit, Disabled gtest2 vtest2, TEST
Gui, Add, Button, Default vok gok, OK
;Gui, Add, Button, vok gok, OK
Gui, Show, h200 w300
return

test:
 ;ControlGet, Cb, Checked
 GuiControlGet cb,,  test ;use this instead of above (otherwise should be no difference)
 GuiControlGet, txt,, test2
    if !Cb
	{
		GuiControl, Disable, test2
        if Txt {
            GuiControl, Enable, ok
            GuiControl, +Default, ok
            GuiControl, Focus, ok
        }
	}
	else
	{
		GuiControl, Enable, test2
		GuiControl, Focus, test2
        GuiControl, Disable, ok
	}
return

ok:
    MsgBox You pressed OK Button!
Return

test2:
 ControlGet, Cb, Checked
 GuiControlGet, txt,, test2
    if !Cb && txt
	{
		;GuiControl, Disable, test2
		GuiControl, Enable, ok
        GuiControl, +Default, ok
		GuiControl, Focus, ok
	}
	else
	{
		;GuiControl, Enable, test2
		;GuiControl, Focus, test2
        GuiControl, Disable, ok
	}
	;GuiControlGet, test2
	;if ( test2 = "" )
		;GuiControl, Disable, ok
	;else
		;GuiControl, Enable, ok
;return

f()
{
	test := 1
	GuiControl,, test, 1
	GuiControl,, test2

	if ( test = 1 )
		GuiControl, Enable, ok
	return
}

GuiClose:
Esc::ExitApp
Edit: using GuiControlGet in place of ControlGet

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: joefiesta and 301 guests