Problems with GUI button names
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Problems with GUI button names
I am quite puzzled - but never mind I am habitual user of AHK, but an occasional codemaker - in dealing with the following problem using v.1.1.34.
I have a code to build a dynamic GUI. It is a bit long to put all here (and with too many msgbox for debugging), so the essential lines are the following:
Gui, 1: Add, Button, w275 h20 x5 y%YPos% gRun, % FileName%A_Index%
1) The buttons are built according to a given numer of files in a folder, that a "loop" detects.
2) Each button displays the name of the file using % FileName%A_Index%
So far so good.
Now, in pressing a button I tried to capture the name of the file that I see on the button.
3) Not having variables I could use A_GetControl, but it has a limit of 63 characters I discovered and some files have longer names.
So I tried the following code to pick up such a name of the pressed button (i.e. the name of the file):
GuiControlGet, Button_Name,, %A_GuiControl%
4) It works also for longer file names, i.e. above 63 characters limit.
5) However, with the following two file names (and displayed name buttons) it goes in confusion: "launcher - copy" and "launcher". When I pressed the second button (with the second name: launcher) Button_Name picks up the first name: launcher - copy.
6) If I rename the first file (and hence if the button is called like that) "launcherz - copy" (just need to add a letter: still not working instead with punctuations and "-") both buttons work ok.
QUESTION:
Why in the first case %Button_Name% fails to collect the correct button? There is a way to do it in a simple way, without changing file names?
I found a solution by adding variables to buttons, but I am wonder why of the above failure, and if a simpler solution without adding variables exists.
Thanks
I have a code to build a dynamic GUI. It is a bit long to put all here (and with too many msgbox for debugging), so the essential lines are the following:
Gui, 1: Add, Button, w275 h20 x5 y%YPos% gRun, % FileName%A_Index%
1) The buttons are built according to a given numer of files in a folder, that a "loop" detects.
2) Each button displays the name of the file using % FileName%A_Index%
So far so good.
Now, in pressing a button I tried to capture the name of the file that I see on the button.
3) Not having variables I could use A_GetControl, but it has a limit of 63 characters I discovered and some files have longer names.
So I tried the following code to pick up such a name of the pressed button (i.e. the name of the file):
GuiControlGet, Button_Name,, %A_GuiControl%
4) It works also for longer file names, i.e. above 63 characters limit.
5) However, with the following two file names (and displayed name buttons) it goes in confusion: "launcher - copy" and "launcher". When I pressed the second button (with the second name: launcher) Button_Name picks up the first name: launcher - copy.
6) If I rename the first file (and hence if the button is called like that) "launcherz - copy" (just need to add a letter: still not working instead with punctuations and "-") both buttons work ok.
QUESTION:
Why in the first case %Button_Name% fails to collect the correct button? There is a way to do it in a simple way, without changing file names?
I found a solution by adding variables to buttons, but I am wonder why of the above failure, and if a simpler solution without adding variables exists.
Thanks
Last edited by science2002 on 25 Mar 2024, 14:13, edited 2 times in total.
Re: Problems with GUI button names
example from @mikeyww ( but defaced , bad modified by me ... )
create buttons , run ahk-files in a_scriptdir
viewtopic.php?p=462052#p462052
create buttons , run ahk-files in a_scriptdir
viewtopic.php?p=462052#p462052
Code: Select all
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=104039
i:=0
dir = %A_ScriptDir%
Gui,2: Font, s12 cBlack,Lucida Console
Loop, Files, %dir%\*.ahk
{
i++
Md:=mod(i,2)
if md=0
colbgr:="red",coltxt:="white"
else
colbgr:="yellow",coltxt:="black"
Gui,2:Add,Progress, w550 Disabled Background%colbgr% ;- TITLE Button-backgroundcolor
Gui,2:Add,Text, xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunscript ,% RegExReplace(A_LoopFileName, "\.ahk$")
}
Gui,2:show
return
;----------
2Guiclose:
Exitapp
;-----------
RunScript:
Run, %dir%\%A_GuiControl%.ahk
return
;=================================
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Re: Problems with GUI button names
Thanks, @garry, for your quick feedback. I'm amazed at how similar features of mine could be achieved with one-tenth of the code: I have 250 lines of code, whereas yours has 25.
Admittedly, I do not fully understand the ratio of many lines of it. What I can say for certain, though, is that it fails with the same initial problem of mine when files have more than 63 characters.
And let me naively add that I am still puzzled by my question.
Admittedly, I do not fully understand the ratio of many lines of it. What I can say for certain, though, is that it fails with the same initial problem of mine when files have more than 63 characters.
And let me naively add that I am still puzzled by my question.
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Re: Problems with GUI button names
And just to clarify. In order to correct the 63 character limit if I substitute your final lines with the following I have the same problem with the two names: "launcher - copy" and "launcher"
GuiControlGet, ControlName,, %A_GuiControl%
Run, %dir%\%ControlName%.ahk
;Run, %dir%\%A_GuiControl%.ahk
GuiControlGet, ControlName,, %A_GuiControl%
Run, %dir%\%ControlName%.ahk
;Run, %dir%\%A_GuiControl%.ahk
Re: Problems with GUI button names
have no answer if problem with more than 63 characters . This works but you get with a_loopfileshortpath the DOS format-name 8.3
this works also when you have space in filename ( but also needs less than 64 characters )
Code: Select all
;- Gui help / mikeyww
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=104039
i:=0
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 cBlack,Lucida Console
Loop, Files, %dir%\*.ahk
{
i++
Md:=mod(i,2)
if md=0
colbgr:="red",coltxt:="white"
else
colbgr:="yellow",coltxt:="black"
x:=a_loopfilefullpath
loop,%x%
SP1:=A_loopFileShortPath
SplitPath,sp1, name, dir, ext, name_no_ext, drive
Gui,2:Add,Progress, w550 Disabled Background%colbgr% ;- TITLE Button-backgroundcolor
Gui,2:Add,Text, xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunscript ,%name%
}
Gui,2:show
return
;----------
2Guiclose:
Exitapp
;-----------
RunScript:
r:=a_guicontrol
Run,%dir%\%r%
return
;=================================
Code: Select all
i:=0
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 cBlack,Lucida Console
Loop, Files, %dir%\*.ahk
{
i++
Md:=mod(i,2)
if md=0
colbgr:="red",coltxt:="white"
else
colbgr:="yellow",coltxt:="black"
x:=a_loopfilefullpath
SplitPath,x, name, dir, ext, name_no_ext, drive
Gui,2:Add,Progress, w550 Disabled Background%colbgr% ;- TITLE Button-backgroundcolor
Gui,2:Add,Text, xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunscript ,%name_no_ext%
}
Gui,2:show
return
;----------
2Guiclose:
Exitapp
;-----------
RunScript:
r:=a_guicontrol
Run,%dir%\%r%.ahk
return
Re: Problems with GUI button names
and this works with long names , greater than 63 characters , but works not when you have space in filename ( then problem in > v%name_no_ext% )
Code: Select all
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 ,Lucida Console
colbgr:="black",coltxt:="yellow"
Loop, Files, %dir%\*.ahk
{
z:=a_loopfilefullpath
SplitPath,z, name, dir, ext, name_no_ext, drive
Gui,2:Add,Progress, w550 Disabled Background%colbgr%
Gui,2:Add,Text, xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 v%name_no_ext% gRunx ,%name_no_ext%
;Gui,2:add,Button,xs y+20 w550 gRunx v%name_no_ext%,%name_no_ext%
}
Gui,2:show,,TEST
return
;----------
2Guiclose:
Exitapp
;-----------
Runx:
Gui,2:submit,nohide
MouseGetPos,,,, classNN
GuiControlGet, varName, Name, %classNN%
run,%dir%\%varname%.ahk
return
;=================================
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Re: Problems with GUI button names
Thanks @garry for the extra code. But as you mentioned the problems posed are still there.
The extraction of the button name with A_GuiControl limited to 63 characters is disappointing, but advertised in the AHK help file. So no surprise.
Yet the other problem I outlined is out of my understanding.
GuiControlGet, ButtonName,, %A_GuiControl% does not have problems with long names. It does for certain files. In my limited testing, as I partially said, it fails with 1. (it picks up 2) yet it does not fail with 3 and 4 that also have the same initial name and a space.
1. launcher
2. launcher - copy
3. AHK_Test Archive1
4. AHK_Test Archive2
Regarding to your last post, by putting variables in the buttons, I resolved the problem with no fear of long names, spaces, or whatever else. Yet my code is very long, as compared to yours. As you can see from my first post I use loops and A_Index.
The extraction of the button name with A_GuiControl limited to 63 characters is disappointing, but advertised in the AHK help file. So no surprise.
Yet the other problem I outlined is out of my understanding.
GuiControlGet, ButtonName,, %A_GuiControl% does not have problems with long names. It does for certain files. In my limited testing, as I partially said, it fails with 1. (it picks up 2) yet it does not fail with 3 and 4 that also have the same initial name and a space.
1. launcher
2. launcher - copy
3. AHK_Test Archive1
4. AHK_Test Archive2
Regarding to your last post, by putting variables in the buttons, I resolved the problem with no fear of long names, spaces, or whatever else. Yet my code is very long, as compared to yours. As you can see from my first post I use loops and A_Index.
Re: Problems with GUI button names
an example because the problem with space in filename , concerns > v%name_no_ext%
replace all spaces with a character which usually not exist , here example > replace space with $ , and later replace $ again with space
( script not uses a_guicontrol ,and shoul'd work with more than 63 characters )
;-
replace all spaces with a character which usually not exist , here example > replace space with $ , and later replace $ again with space
( script not uses a_guicontrol ,and shoul'd work with more than 63 characters )
;-
Code: Select all
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 ,Lucida Console
colbgr:="black",coltxt:="yellow"
Loop, Files, %dir%\*.ahk
{
z:=a_loopfilefullpath
SplitPath,z, name, dir, ext, name_no_ext, drive
stringreplace,name_no_ext,name_no_ext,%a_space%,$,all
Gui,2:Add,Progress, w550 Disabled Background%colbgr%
Gui,2:Add,Text, xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 v%name_no_ext% gRunx ,%name_no_ext%
;Gui,2:add,Button,xs y+20 w550 gRunx v%name_no_ext%,%name_no_ext%
}
Gui,2:show,,TEST
return
;----------
2Guiclose:
Exitapp
;-----------
Runx:
Gui,2:submit,nohide
MouseGetPos,,,, classNN
GuiControlGet, varName, Name, %classNN%
stringreplace,varname,varname,$,%a_space%,all
run,%dir%\%varname%.ahk
return
;=================================
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Re: Problems with GUI button names
Good strategy @garry, for what I understand. Yet in my case it does not work. The replaced spaces seem different from the expected ones. And the code breaks at line 11, i.e. GUI Add Text.
From the warning window I see that the name of the files have been changed but additing two characters and not just one ($).
Here an example (AHK_Test Archive) with just one space substitution.
---------------------------
running_AHK_scripts8_garry.ahk
---------------------------
Error: The following variable name contains an illegal character:
"AHK-TestÂ$Archive"
---> 011: Gui,2:Add,Text,xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunx v%name_no_ext%,%name_no_ext%
.
This gives me - if I may say - a bit of comfort: the shortest code has its problems.
PS: I saved the code in Utf8 - Bom (before just Utf8) and that has removed the extra-added character (Â). Yet, it kept the same error.
PS2: It seems that the GUI variable does not accept within its name "-" in addition to spaces. And problably it does not accept other characters too, like (, ), ".", etc. Moreover all these substitutions with other characters will appear in the shown GUI.
From the warning window I see that the name of the files have been changed but additing two characters and not just one ($).
Here an example (AHK_Test Archive) with just one space substitution.
---------------------------
running_AHK_scripts8_garry.ahk
---------------------------
Error: The following variable name contains an illegal character:
"AHK-TestÂ$Archive"
---> 011: Gui,2:Add,Text,xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunx v%name_no_ext%,%name_no_ext%
.
This gives me - if I may say - a bit of comfort: the shortest code has its problems.
PS: I saved the code in Utf8 - Bom (before just Utf8) and that has removed the extra-added character (Â). Yet, it kept the same error.
PS2: It seems that the GUI variable does not accept within its name "-" in addition to spaces. And problably it does not accept other characters too, like (, ), ".", etc. Moreover all these substitutions with other characters will appear in the shown GUI.
Last edited by science2002 on 26 Mar 2024, 07:15, edited 3 times in total.
Re: Problems with GUI button names
Seems that you have a pseudo-array of filenames displayed in buttons. Also it seems that you are using a loop to create the buttons. If so, you can add unique names to your buttons:
After this change, the value of A_GuiControl in Run will be the button's name which is unique when used in
and will always retrieve the correct filename.
See: Storing and Responding to User Input
Code: Select all
Gui, 1: Add, Button, w275 h20 x5 y%YPos% gRun vBtn%A_Index%, % FileName%A_Index% ; <<<<< added vBtn%A_Index%
Code: Select all
GuiControlGet, Button_Name,, %A_GuiControl%
See: Storing and Responding to User Input
-
- Posts: 31
- Joined: 29 Oct 2016, 05:18
Re: Problems with GUI button names
Hi @just me. Yes, as you can detect from the code line of my first post and as I wrote in my third post:
Final point - tell me if I am wrong. I suspect that your last line of code gives, in Button_Name the name of the GUI variable, not that of % FileName%A_Index%. So you need to extract the latter from the variable.
That's what was my approach since the beginning and that's what I did also with the GUI variables to overcome the puzzle. Thanks for underlining the point. I posted here because I was interested to solve the puzzle of GuiControlGet with A_GuiControl, in the attempt to avoid GUI variables.As you can see from my first post I use loops and A_Index.
Final point - tell me if I am wrong. I suspect that your last line of code gives, in Button_Name the name of the GUI variable, not that of % FileName%A_Index%. So you need to extract the latter from the variable.
Re: Problems with GUI button names
@just me yes this works fine
earlier example worked also , don't know what the problem with '$' was , but was useless to replace the spaces .
Is the variable > vBtn%A_Index% maybe not needed ?
the same with 'COLOR-BUTTONS'
earlier example worked also , don't know what the problem with '$' was , but was useless to replace the spaces .
Is the variable > vBtn%A_Index% maybe not needed ?
Code: Select all
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 ,Lucida Console
Loop, Files, %dir%\*.ahk
{
z:=a_loopfilefullpath
SplitPath,z, name, dir, ext, name_no_ext, drive
Gui,2:add,Button,xs y+20 w550 gRunx,%name_no_ext%
}
Gui,2:show,,TEST
return
;----------
2Guiclose:
Exitapp
;-----------
Runx:
Gui,2:submit,nohide
GuiControlGet,var,, %A_GuiControl%
run,%dir%\%var%.ahk
return
Code: Select all
dir = %A_ScriptDir%
Gui,2:default
Gui,2: Font, s12 ,Lucida Console
colbgr:="black",coltxt:="yellow"
Loop, Files, %dir%\*.ahk
{
z:=a_loopfilefullpath
SplitPath,z, name, dir, ext, name_no_ext, drive
Gui,2:Add,Progress, w550 Disabled Background%colbgr%
Gui,2:Add,Text , xp yp wp hp c%coltxt% BackgroundTrans Center 0x200 gRunx ,%name_no_ext%
}
Gui,2:show,,TEST
return
;----------
2Guiclose:
Exitapp
;-----------
Runx:
Gui,2:submit,nohide
GuiControlGet,var,, %A_GuiControl%
run,%dir%\%var%.ahk
return
Re: Problems with GUI button names
If you try to get a GUI button by its caption used as control ID, AHK (1.1?) searches all controls in the order they were added to the GUI. It chooses the first control whose caption starts with the search string.science2002 wrote:5) However, with the following two file names (and displayed name buttons) it goes in confusion: "launcher - copy" and "launcher". When I pressed the second button (with the second name: launcher) Button_Name picks up the first name: launcher - copy.
Code: Select all
#NoEnv
#Requires AutoHotkey v1.1.33+
Gui, Add, Text, xm gClick, Button - Copy 111
Gui, Add, Button, xm gClick, Button - Copy 11
Gui, Add, Button, xm gClick, Button - Copy 1
Gui, Add, Button, xm gClick, Button
Gui, Show, , Caption Test
Return
Click:
GuiControlGet, Caption, , %A_GuiControl%
MsgBox, Clicked on A_GuiControl`n`t>%A_GuiControl%<`nwith caption`n`t>%Caption%<
Return
GuiCLose:
ExitApp
I took this line from your first post. It will store the complete caption of the button in Button_Name.science2002 wrote: Final point - tell me if I am wrong. I suspect that your last line of code gives, in Button_Name the name of the GUI variable, not that of % FileName%A_Index%. So you need to extract the latter from the variable.
Re: Problems with GUI button names
@just me thank you , interesting what you get
so this I like to use > GuiControlGet, Caption, , %A_GuiControl% , >> no problem with space and more than 63 characters in filename
( I think my last examples are OK )
so this I like to use > GuiControlGet, Caption, , %A_GuiControl% , >> no problem with space and more than 63 characters in filename
( I think my last examples are OK )
Code: Select all
;-------- saved at 星期三 三月 2024-03-27 09:28 UTC --------------
;- Problems with GUI button names
;- https://www.autohotkey.com/boards/viewtopic.php?p=565091#p565091
#NoEnv
#Requires AutoHotkey v1.1.33+
f1:=a_scriptdir . "\test22.txt"
Gui, Add, Text, xm gClick, Button - Copy 111 ;- <<< ( caption ? )
;---
Gui, Add, Button, xm gClick, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz
Gui, Add, Button, xm gClick, Button - Copy 1
Gui, Add, Button, xm gClick, Button
Gui, Show, , Caption Test
Return
;--------
Click:
GuiControlGet, Caption, , %A_GuiControl%
aa=Clicked on A_GuiControl`n`t>%A_GuiControl%<`nwith caption`n`t>%Caption%<`n-----------------------------------------`n
fileappend,%aa%,%f1%,utf-8
try
run,%f1%
Return
;--------
GuiCLose:
ExitApp
;========================================================
/*
Clicked on A_GuiControl
>Button - Copy 111<
with caption
>Button - Copy 111<
-----------------------------------------
;- concerns caption > here I don't get the value from Gui,add,Text > >Button - Copy 111< , instead I get all characters from the Button which I find useful
Clicked on A_GuiControl
>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa< ;-< 63 characters
with caption
>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz<
-----------------------------------------
Clicked on A_GuiControl
>Button - Copy 1<
with caption
>Button - Copy 111<
-----------------------------------------
Clicked on A_GuiControl
>Button<
with caption
>Button - Copy 111<
-----------------------------------------
*/
;========================================================
Re: Problems with GUI button names
Hi @garry,
spaces or more than 63 characters are no problem as long as the resulting "ControlID" is unique for all controls which can use contents as "ID" repectively is not the leading part of the "ID" of another control added before.
spaces or more than 63 characters are no problem as long as the resulting "ControlID" is unique for all controls which can use contents as "ID" repectively is not the leading part of the "ID" of another control added before.