 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Hülyesamu
Joined: 24 Sep 2004 Posts: 22
|
Posted: Tue Jan 01, 2008 3:28 pm Post subject: GUI: Dynamically generated buttons' gosubs |
|
|
Hi, I want to create a popup menu made of array of buttons that reads its content from a text file. (Ie. The first button has a text of THIS, and does THAT, both definded in the text file).
Certeanly, the buttons do the same, but with a different variable (more specifically, they send the keys that is written onto them: A "Kitchen" key says the keys k--i-t-c-h-e-n, etc)
Is it possible to do this in the AutoHotkey? Since the buttons need a "g label" gosub for everyone of them, but at a dynamically generated content it doesn't work.
 |
|
| Back to top |
|
 |
Mustang
Joined: 17 May 2007 Posts: 421 Location: England
|
Posted: Tue Jan 01, 2008 4:04 pm Post subject: |
|
|
Create a text file called "File.txt" with the following in it:
| Code: | Name1,Text1
Name2,Text2
Name3,Text3 |
Then in the same folder a file with the following AHK script:
| Code: | Loop, Read, File.txt
{
Count := A_Index
Loop, Parse, A_LoopReadLine, `,
{
If ( A_Index = 1 )
{
MenuName%Count% := A_LoopField
}
Else
{
MenuText%Count% := A_LoopField
}
}
}
Loop, %Count%
{
Menu, MyMenu, Add, % MenuName%A_Index%, MenuHandler
}
Return
MenuHandler:
{
MsgBox, % MenuText%A_ThisMenuItemPos%
Return
}
^!a::
{
Menu, MyMenu, Show
Return
} |
Use Ctrl+Alt+a to bring up the menu
Edit to your pleasing |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Wed Jan 02, 2008 5:36 am Post subject: |
|
|
you can also do it with buttons. give them all the same glabel, and then make the label send/use %A_GuiControl%, which will contain up to the first 63 letters of the button's text. _________________
(Common Answers) |
|
| Back to top |
|
 |
Hülyesamu
Joined: 24 Sep 2004 Posts: 22
|
Posted: Wed Jan 02, 2008 9:26 am Post subject: |
|
|
| Thanks, I'll try it. |
|
| Back to top |
|
 |
Link68759
Joined: 19 Aug 2008 Posts: 6
|
Posted: Sun Jun 07, 2009 11:03 pm Post subject: |
|
|
I don't mean to raise a dead topic, but how does one assign the same glabel to the buttons?
I understand a little; report the button's name from the gosub using A_GUIControl, but how do you set that up in the first place? |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Sun Jun 07, 2009 11:27 pm Post subject: |
|
|
The example above assigns each menu item to the label MenuHandler and determines which item was selected by checking A_ThisMenuItemPos.
If you're talking about using actual buttons, it's even easier: just assign each button to the same gLabel. | Code: | string = Mary had a little lamb
Loop, Parse, string, %A_Space%
Gui, Add, Button, gLabel, %A_LoopField%
Gui, Show
return
GuiClose:
ExitApp
Label:
MsgBox, %A_GuiControl%
return |
|
|
| Back to top |
|
 |
Link68759
Joined: 19 Aug 2008 Posts: 6
|
Posted: Mon Jun 08, 2009 12:11 am Post subject: |
|
|
thanks, I actually just figured it out.
What I didn't understand was that the glabel was activated every time I pressed the button; the manual just said that it ran the label immediately. In my mind, if the label was only run when the button was created, even if I made the glabel, I would've ended right back where I started. Then I just did it to see what would happen and voila.
Thanks for the help. |
|
| Back to top |
|
 |
diesalher
Joined: 14 Apr 2009 Posts: 28
|
Posted: Thu Jun 18, 2009 2:53 pm Post subject: |
|
|
Im trying the same with one difference, i need the buttons to have the same text (i have a list of to-do things and i want a delete button at one side) How can i determine the button pressed and the label associated?
Example
List.
[Delete]Buy milk
[Delete]Buy Coffee
[Delete]Write a Book
I want to press the button near the label and identify in the subroutine the label near the button, is there a way? |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Thu Jun 18, 2009 3:41 pm Post subject: |
|
|
Here's a sample using buttons... | Code: | string = Mary had a little lamb
Loop, Parse, string, %A_Space%
{
Gui, Add, Button, xm v%A_Index%, Delete
Gui, Add, Edit, x+10 w100, %A_LoopField%
}
Gui, Show
return
GuiClose:
ExitApp
ButtonDelete:
GuiControl,,Edit%A_GuiControl%
return |
...but it would be much nicer in a ListView. | Code: | list = Buy milk|Buy Coffee|Write a Book
Gui, Add, ListView, r10 -ReadOnly Checked, ToDo
Loop, Parse, list, |
LV_Add("",A_LoopField)
Gui, Show
return
GuiClose:
ExitApp |
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|