AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GUI: Dynamically generated buttons' gosubs

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Hülyesamu



Joined: 24 Sep 2004
Posts: 22

PostPosted: Tue Jan 01, 2008 3:28 pm    Post subject: GUI: Dynamically generated buttons' gosubs Reply with quote

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.

Question
Back to top
View user's profile Send private message
Mustang



Joined: 17 May 2007
Posts: 421
Location: England

PostPosted: Tue Jan 01, 2008 4:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Wed Jan 02, 2008 5:36 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Hülyesamu



Joined: 24 Sep 2004
Posts: 22

PostPosted: Wed Jan 02, 2008 9:26 am    Post subject: Reply with quote

Thanks, I'll try it.
Back to top
View user's profile Send private message
Link68759



Joined: 19 Aug 2008
Posts: 6

PostPosted: Sun Jun 07, 2009 11:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Sun Jun 07, 2009 11:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Link68759



Joined: 19 Aug 2008
Posts: 6

PostPosted: Mon Jun 08, 2009 12:11 am    Post subject: Reply with quote

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
View user's profile Send private message
diesalher



Joined: 14 Apr 2009
Posts: 28

PostPosted: Thu Jun 18, 2009 2:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Thu Jun 18, 2009 3:41 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group