Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Mar 23, 2017 1:06 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Menu Handler Question
PostPosted: Sat May 25, 2013 4:08 pm 
Offline

Joined: Tue Apr 12, 2011 6:32 am
Posts: 14
Hi All,

I can see that menubars and menuitems can be created in code. Can a menu handler be created/added via code? If so, how? I was not able to find this.

Thanks!

Gary


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sat May 25, 2013 11:23 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Yes. You can use AddHandler to assign a method to handle the MenuItem.Action event for any menuitem you have a reference to.

Handler methods MUST conform to the same method signature (same number, type, and order of parameters and the same return type, if any) of the event they are handling. In addition to whatever parameters a particular event defines, all handler methods will receive a reference to the object it's assigned to, passed as the first parameter, followed by whatever other parameters are defined.

To handle the MenuItem.Action Event, a method similar to this one is needed:

Function MyMenuHandler(Sender As MenuItem) As Boolean
MsgBox("You clicked it!")
Return True
End Function


Any time you create a new MenuItem that needs to be handled by this method, you would use AddHandler to assign the method to the event:

Dim mnu As New MenuItem("Foo")
AddHandler mnu.Action, AddressOf MyWindow.FooHandler


More about AddHandler and events

_________________
Boredom Software


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 12:52 am 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
TrakSoft wrote:
Hi All,

I can see that menubars and menuitems can be created in code. Can a menu handler be created/added via code? If so, how? I was not able to find this.

Another option to use / consider is just to make a custom subclass of menuitem & put your code in the events in the custom subclass

_________________
Norman Palardy (Real Software)


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 8:03 am 
Offline

Joined: Tue Apr 12, 2011 6:32 am
Posts: 14
charonn0 wrote:
Yes. You can use AddHandler to assign a method to handle the MenuItem.Action event for any menuitem you have a reference to.

Handler methods MUST conform to the same method signature (same number, type, and order of parameters and the same return type, if any) of the event they are handling. In addition to whatever parameters a particular event defines, all handler methods will receive a reference to the object it's assigned to, passed as the first parameter, followed by whatever other parameters are defined.

To handle the MenuItem.Action Event, a method similar to this one is needed:

Function MyMenuHandler(Sender As MenuItem) As Boolean
MsgBox("You clicked it!")
Return True
End Function


Any time you create a new MenuItem that needs to be handled by this method, you would use AddHandler to assign the method to the event:

Dim mnu As New MenuItem("Foo")
AddHandler mnu.Action, AddressOf MyWindow.FooHandler


More about AddHandler and events


I am having problems with your syntax. I'm trying to do some testing and I am getting a syntax error. I checked the Docs site and it appears to be correct, but not sure what the problem is. Here is my code. I get a syntax error on the function declaration line. Any help will be greatly appreciated.

Dim mnuFoo As new MenuItem("Foo")

Function FooHandler(Sender As MenuItem) As Boolean
MsgBox("You clicked it!")
Return True
End Function

AddHandler mnuFoo.Action, AddressOf FooHandler


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 5:01 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
Function MyMenuHandler(Sender As MenuItem) As Boolean
MsgBox("You clicked it!")
Return True
End Function

While you will often see code presented in this format because it is more obvious that it's a function, etc., and while this format is used behind the scenes (and in VCP format), you don't enter code like this in the IDE. In the IDE, use Add Method. The first line (Function ...) is broken into Name, Parameters and Return Type. The code block goes into the body of the method. And the last line (End Function) you just drop.


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 5:53 pm 
Offline

Joined: Tue Apr 12, 2011 6:32 am
Posts: 14
timhare wrote:
Quote:
Function MyMenuHandler(Sender As MenuItem) As Boolean
MsgBox("You clicked it!")
Return True
End Function

While you will often see code presented in this format because it is more obvious that it's a function, etc., and while this format is used behind the scenes (and in VCP format), you don't enter code like this in the IDE. In the IDE, use Add Method. The first line (Function ...) is broken into Name, Parameters and Return Type. The code block goes into the body of the method. And the last line (End Function) you just drop.


Tim,

Thanks for your reply. However, I am aware of being able to add a function/menu handler via the IDE. But going back to my original question (original post in this thread), I am looking to add these dynamically during runtime.

Are you are saying that RB can NOT add a menu handler "dynamically" using code - and you have to do it via the IDE? If that is true, why have the capability to add menuitems via code? Seems worthless to me because you must already have a menu handler in place to handle the menuitem you ad via code.

I am looking for the capability to be able to dynamically create a menu, to include the handlers, during runtime. Is this capability not in RB?

Again, thanks,

Gary


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 7:17 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You were just given 2 ways to do it. I'm just pointing out where your code went wrong.


Top
 Profile  
 
 Post subject: Re: Menu Handler Question
PostPosted: Sun May 26, 2013 10:42 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Methods must be defined in the IDE, but you can decide which method should be assigned to a MenuItem in code. Different menuitems can be handled by the same method, or you can determine which of several methods should be assigned at runtime.

Example project

_________________
Boredom Software


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group