473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom event handling for click of menu item.

Hi all,

I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.

And then while setting eventhandler i am passing this custom event
args. But this is not working.

Can anyone tell me what i am doing wrong. Can't i create my own
handler and attach it to client event.

Please help me asap.

Any sample code will be truely appreciated.

Feb 21 '07 #1
2 1848
In article <11**********************@l53g2000cwa.googlegroups .com>,
tr**************@yahoo.com says...
I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.
The MenuItem class already defines a specific signature for each of its
events. You can't change that.

If you want to store "extra" information with your MenuItem's you could
either subclass it, or, if its something really simple, use the Tag
property.

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 22 '07 #2
On Feb 21, 8:07 am, trialproduct2...@yahoo.com wrote:
Hi all,

I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.

And then while setting eventhandler i am passing this custom event
args. But this is not working.

Can anyone tell me what i am doing wrong. Can't i create my own
handler and attach it to client event.

Please help me asap.

Any sample code will be truely appreciated.
You would have to derive your own menu item and add your custom data
to it. Something like this (check syntax):

Public Class MyMenuItemClass
Inherits ToolStripMenuItem

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
Dim myEvent As New MyMenuEventArgs
myEvent.MyCustomProperty = "Custom Data"

MyBase.OnClick(myEvent)
End Sub

End Class

Public Class MyMenuEventArgs
Inherits System.EventArgs

Private _myCustomProperty As String
Public Property MyCustomProperty() As String
Get
Return _myCustomProperty
End Get
Set(ByVal value As String)
_myCustomProperty = value
End Set
End Property
End Class

Then when you add your menu item dynamically and handle the click
event you can get the custom property data:

'In the Form class:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim customMenuItem As New MyMenuItemClass()
AddHandler customMenuItem.Click, AddressOf MenuItem_Click

RootMenu.DropDownItems.Add(customMenuItem)
End Sub
Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
If TypeOf sender Is MyMenuItemClass Then
MsgBox("Custom data for menu: " & DirectCast(e,
MyMenuEventArgs).MyCustomProperty)
Else
MsgBox("Normal menu item")
End If
End Sub

Notice in the MenuClick handler I first check to make sure the sender
is actually an instance of my custom menu class. If that is true,
then I can safely cast the EventArgs parameter (e) to my custom event
args.

Hope this helps

Feb 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: serge calderara | last post by:
Dear all, I have a class named for Instance "MyClass" which populate trough an interface a context menu object to the main application. The click event on any of the context menu itemm will be...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
11
by: Marcelo | last post by:
Hello! I am developping a Visual C++ .NET 2003 multiple forms application. My problem is: When running my application, I click a button and a new main menu item is created. Ok. Now I want to...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
6
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook...
2
by: AmericanGotham | last post by:
The following method adds items to a toolstripmenu named colums public void AddColumnMenu() { foreach (ColumnHeader column in messagesListView.Columns) { ToolStripMenuItem item =...
12
by: Tom Bean | last post by:
I am trying to display a ContextMenuStrip when a user right-clicks on an item in a ListView and have encountered a something that seems strange to me. When the ListView is initially populated,...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.