Page 1 of 1

Cant change caption of a button

Posted: 28 Jan 2022, 17:47
by braunbaer
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

Re: Cant change caption of a button

Posted: 28 Jan 2022, 18:29
by amateur+
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.

Re: Cant change caption of a button

Posted: 01 Feb 2022, 08:01
by braunbaer
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.

Re: Cant change caption of a button

Posted: 01 Feb 2022, 08:14
by gregster
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.

Re: Cant change caption of a button

Posted: 01 Feb 2022, 08:28
by braunbaer
Thank you for your answer, I just found out myself :)

Re: Cant change caption of a button

Posted: 01 Feb 2022, 09:12
by amateur+
Glad that you have solved your task!
@gregster, sniper shot, good guess!