Cant change caption of a button

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Cant change caption of a button

Post by braunbaer » 28 Jan 2022, 17:47

I think it must be a trivial problem, but I don't find the reason this does not work:

In a GUI, I have the following Button:

Code: Select all

...
    gui, Add, Button, x51 yp w79 Center gChooseDest vDestUser, Idefix2
...

ChooseDest(){
    DestUser:=AskForUser()
    GuiControl,,DestUser,%Destuser%
    msgbox DestUser: %DestUser%
    }
When I click on the button, the function AskforUser opens a dialog with a listbox, lets me choose a user name and returns the chosen name.
The msgbox displays that name correctly (so I know that AskForUser works correctly), but the GuiControl command does not change the caption of the button. The caption is still idefix2, while the chosen username is Verleihnix.

DestUser is superglobal, so I don't need to declare it as global in ChooseDest. When I display the variables in the AHK main window, it shows the global variable DestUser with the correct value.

Image

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Cant change caption of a button

Post by amateur+ » 28 Jan 2022, 18:29

Run this as a separate script:

Code: Select all

gui, Add, Button, x51 yp w79 Center gChooseDest vDestUser, Idefix2
gui, Show
return

ChooseDest(){
    DestUser:= AskForUser()
    GuiControl,,DestUser,%Destuser%
    msgbox DestUser: %DestUser%
    }

AskForUser() {
	return "Verleihnix"
}
So the problem might be in those parts of your script you haven't posted.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Cant change caption of a button

Post by braunbaer » 01 Feb 2022, 08:01

It's a big script that does a lot of other things, and even some parts are somewhat confidential.

But here, the guicontrol that does not work is immediately followed by a msgbox that shows the correct data, so I don't see how other parts of the script could possibly interfere. It's a single statement that does not work as expected

Code: Select all

    GuiControl,,DestUser,%Destuser%   , does not change the caption (see screenshot)
    msgbox DestUser: %DestUser%       , shows that Destuser contains the correct data (see screenshot)
Edit:
Thank you for your answer, I understand that something HAD to interfere, and I by analizing the difference between yourscript and my original script, I found the problem: The function AskForUser creates another GUI that allows to select the username from a listbox, and thus the default GUI of the thread changes, so GuiControl did not find the control.
Last edited by braunbaer on 01 Feb 2022, 08:27, edited 1 time in total.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Cant change caption of a button

Post by gregster » 01 Feb 2022, 08:14

Perhaps this (especially if you have more than one GUI):
https://www.autohotkey.com/docs/commands/GuiControl.htm#Remarks wrote:To operate upon a window other than the default, include its name or number (or [in v1.1.03+] its HWND) followed by a colon in front of the sub-command as in these examples:

Code: Select all

GuiControl, MyGui:Show, MyButton
GuiControl, MyGui:, MyListBox, Item1|Item2
This is required even if ControlID is a control's associated variable, since any one variable can be used on multiple GUI windows. In [v1.1.20+], the GUI name can be omitted if ControlID is a control's HWND.
(Your msgbox just shows what AskForUser() returned, not the actual result of your GuiControl line.)

You could also check Errorlevel for GuiControl and see what it says.

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Cant change caption of a button

Post by braunbaer » 01 Feb 2022, 08:28

Thank you for your answer, I just found out myself :)

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Cant change caption of a button

Post by amateur+ » 01 Feb 2022, 09:12

Glad that you have solved your task!
@gregster, sniper shot, good guess!
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

Post Reply

Return to “Ask for Help (v1)”