473,748 Members | 4,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event handler for context menu; help needed

Hello,
I am having a little trouble creating an event handler for a context menu
toolstripmenuit em.

I've seen various tutorials and so on, but I keep getting a bit stuck!
So far I have a second class defining the eventargs I want to use:

public class ApptEventArgs : EventArgs{
public int ApptUID;
public String ApptOp;
public int RowID;
public int ColID;
}

In my Forms class, I have declared an event:

public delegate void ApptEventHandle r(object sender, ApptEventArgs e);
Then I have created a function to put the code in for when the event fires:

public void NewApptEvent(ob ject sender, ApptEventArgs e)
{
MessageBox.Show ("NEW Event Fired");
}
Finally, I am constructing the menu and trying the following to set the
arguments and configure my event to be used:

ApptEventArgs NewApptArgs = new ApptEventArgs() ;
NewApptArgs.App tOp = "NEW";
NewApptArgs.App tUID = 0;
NewApptArgs.Col ID = ColIdx;
NewApptArgs.Row ID = RowIdx;
//Create Menu Items
ToolStripMenuIt em NewItem = new ToolStripMenuIt em("New
Appointment",gl obal::Itinerary .Properties.Res ources.NewAppt, new
ApptEventHandle r(NewApptEvent( this,NewApptArg s)));
.......

When I compile, I get several errors, all on the last line where I am
configuring the new toolstrip item.
I get:

"Method Name expected"
"The best overloaded method for ...ToolStripMen uItem(string,
System.Drawing. Image, System.EventHan dler) has some invalid arguments"
"Argument 3: Cannot convert from <project>.ApptE ventHandler' to
'System.EventHa ndler'"
Any ideas?
I have tried various different ways of doing this but I always end up with
some or all of the above errors. I'm obviously not getting it!

At the end of the day, my Context Menu has about 5 main menus that pop out
submenus dynamically created depending on where you clicked, and I need to
fire off an event with those 4 parameters passed to it.

James.
May 5 '06 #1
5 3863
"james" <james@com> wrote in message
news:44******** *************@n ews.zen.co.uk.. .
Hello,
I am having a little trouble creating an event handler for a context menu
toolstripmenuit em.

I've seen various tutorials and so on, but I keep getting a bit stuck!
So far I have a second class defining the eventargs I want to use:

public class ApptEventArgs : EventArgs{
public int ApptUID;
public String ApptOp;
public int RowID;
public int ColID;
}

In my Forms class, I have declared an event:

public delegate void ApptEventHandle r(object sender, ApptEventArgs e);
Then I have created a function to put the code in for when the event
fires:

public void NewApptEvent(ob ject sender, ApptEventArgs e)
{
MessageBox.Show ("NEW Event Fired");
}
Finally, I am constructing the menu and trying the following to set the
arguments and configure my event to be used:

ApptEventArgs NewApptArgs = new ApptEventArgs() ;
NewApptArgs.App tOp = "NEW";
NewApptArgs.App tUID = 0;
NewApptArgs.Col ID = ColIdx;
NewApptArgs.Row ID = RowIdx;
//Create Menu Items
ToolStripMenuIt em NewItem = new ToolStripMenuIt em("New
Appointment",gl obal::Itinerary .Properties.Res ources.NewAppt, new
ApptEventHandle r(NewApptEvent( this,NewApptArg s)));
......

When I compile, I get several errors, all on the last line where I am
configuring the new toolstrip item.
I get:

"Method Name expected"
"The best overloaded method for ...ToolStripMen uItem(string,
System.Drawing. Image, System.EventHan dler) has some invalid arguments"
"Argument 3: Cannot convert from <project>.ApptE ventHandler' to
'System.EventHa ndler'"
Any ideas?
I have tried various different ways of doing this but I always end up with
some or all of the above errors. I'm obviously not getting it!

At the end of the day, my Context Menu has about 5 main menus that pop out
submenus dynamically created depending on where you clicked, and I need to
fire off an event with those 4 parameters passed to it.


I think the problem is that you cannot control the type of event handler
that the ToolStripMenuIt em will use when it's clicked - it is defined as
using a System.EventHan dler and so the method you provide as the third
argument to the constructor must be one. Thus when you create the item the
code is:
ToolStripMenuIt em NewItem = new ToolStripMenuIt em("New Appointment",
global::Itinera ry.Properties.R esources.NewApp t,
new EventHandler(My EventHandler));

where MyEventhandler is defined as:
private void MyEventHandler( Object sender, EventArgs e)
{
...
}

If, as a result of the click, you need to do something using 4 parameters
then you have at least two options:
(1) Assign a different event handler to each menu item, and hard-code the
appropriate 4 values in it (and you can of course raise another event from
within your event handler if that's what you really want to do).
(2) Use the same event handler for all the menu items, and use the Sender
parameter to identify which menu item was actually clicked and hence what
values should be used for the 4 parameters.

Hope this helps.
Chris Jobson
May 6 '06 #2

"Chris Jobson" <ch**********@b tinternet.com> wrote in message
news:ub******** ******@TK2MSFTN GP05.phx.gbl...
I think the problem is that you cannot control the type of event handler
that the ToolStripMenuIt em will use when it's clicked - it is defined as
using a System.EventHan dler and so the method you provide as the third
argument to the constructor must be one. Thus when you create the item the
code is:
ToolStripMenuIt em NewItem = new ToolStripMenuIt em("New Appointment",
global::Itinera ry.Properties.R esources.NewApp t,
new EventHandler(My EventHandler));

where MyEventhandler is defined as:
private void MyEventHandler( Object sender, EventArgs e)
{
...
}

If, as a result of the click, you need to do something using 4 parameters
then you have at least two options:
(1) Assign a different event handler to each menu item, and hard-code the
appropriate 4 values in it (and you can of course raise another event from
within your event handler if that's what you really want to do).
(2) Use the same event handler for all the menu items, and use the Sender
parameter to identify which menu item was actually clicked and hence what
values should be used for the 4 parameters.

Hope this helps.
Chris Jobson


Hi Chris,
It sounds helpful - I'll know when I get into work tomorrow!
Thanks,
James
May 7 '06 #3

"Chris Jobson" <ch**********@b tinternet.com> wrote in message
news:ub******** ******@TK2MSFTN GP05.phx.gbl...
If, as a result of the click, you need to do something using 4 parameters
then you have at least two options:
(1) Assign a different event handler to each menu item, and hard-code the
appropriate 4 values in it (and you can of course raise another event from
within your event handler if that's what you really want to do).
(2) Use the same event handler for all the menu items, and use the Sender
parameter to identify which menu item was actually clicked and hence what
values should be used for the 4 parameters.


Hi,
OK, I managed to make a "normal" event get fired fine (object sender,
EventArgs e) but I couldn't see how to get my parameters into the sender
object... Hard coding didn't seem right as the parameters would vary
depending on what was right-clicked... in any case, I actually solved it
another way, but I would like to know how or why it works as I don't
entirely understand.

I added a second event handler that would use my ApptEventArgs class:

void NewApptEventHan dler(object sender, ApptEventArgs e)
{
//do stuff here with e.<param>
}

And when I create my menu, I do it with the following bit of code:

ToolStripMenuIt em MDSubItem = new ToolStripMenuIt em("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHa ndler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDo wnItems.Add(MDS ubItem); //Add the item to the menu

It's the delegate line I don't get.
In my original post I had the delegate line outside my forms class as
examples showed and so on, but the error was basically that it couldn't
convert "my" event handler to a system.eventhan dler, yet with the above, it
works!
My theory is that what I am doing here is adding a "second" event handler to
the click event of the menu item and this can be any sort of event handler?
And the "default" click event (which doesn't technically exist, but
whatever) is still the original?

As I said, it works a treat so I'm not too fussed, but I would like to
actually know why it works!

James.
May 11 '06 #4
"james" <james@com> wrote in message
news:44******** **************@ news.zen.co.uk. ..
OK, I managed to make a "normal" event get fired fine (object sender,
EventArgs e) but I couldn't see how to get my parameters into the sender
object... Hard coding didn't seem right as the parameters would vary
depending on what was right-clicked... in any case, I actually solved it
another way, but I would like to know how or why it works as I don't
entirely understand.

I added a second event handler that would use my ApptEventArgs class:

void NewApptEventHan dler(object sender, ApptEventArgs e)
{
//do stuff here with e.<param>
}

And when I create my menu, I do it with the following bit of code:

ToolStripMenuIt em MDSubItem = new ToolStripMenuIt em("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHa ndler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDo wnItems.Add(MDS ubItem); //Add the item to the menu

It's the delegate line I don't get.


I like your solution! I hadn't thought of it, but it seems to be a very good
way of getting the results you want. I find understanding what goes on
behind the scenes with events and delegates rather confusing myself, but
what I think is happening here (and I'm waiting for someone who knows better
to correct me!) is that the line:
MDSubItem.Click += delegate {NewApptEventHa ndler(this, MDApptArgs);};
effectively creates a hidden System.EventHan dler method (i.e. one with the
signature "object sender, EventArgs e") that is called through the
MDSubItem's Click event, and this hidden method then calls your
NewApptEventHan dler with the right parameters.

Chris Jobson
May 11 '06 #5
"james" <james@com> wrote in message
news:44******** **************@ news.zen.co.uk. ..
OK, I managed to make a "normal" event get fired fine (object sender,
EventArgs e) but I couldn't see how to get my parameters into the sender
object... Hard coding didn't seem right as the parameters would vary
depending on what was right-clicked... in any case, I actually solved it
another way, but I would like to know how or why it works as I don't
entirely understand.

I added a second event handler that would use my ApptEventArgs class:

void NewApptEventHan dler(object sender, ApptEventArgs e)
{
//do stuff here with e.<param>
}

And when I create my menu, I do it with the following bit of code:

ToolStripMenuIt em MDSubItem = new ToolStripMenuIt em("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHa ndler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDo wnItems.Add(MDS ubItem); //Add the item to the menu

It's the delegate line I don't get.


I like your solution! I hadn't thought of it, but it seems to be a very good
way of getting the results you want. I find understanding what goes on
behind the scenes with events and delegates rather confusing myself, but
what I think is happening here (and I'm waiting for someone who knows better
to correct me!) is that the line:
MDSubItem.Click += delegate {NewApptEventHa ndler(this, MDApptArgs);};
effectively creates a hidden System.EventHan dler method (i.e. one with the
signature "object sender, EventArgs e") that is called through the
MDSubItem's Click event, and this hidden method then calls your
NewApptEventHan dler with the right parameters.

Chris Jobson
May 11 '06 #6

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

Similar topics

3
1825
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 proceed in the myClass event handler. Actally default event handle has two parameters: Sender and e Actually the context menu is shown in my main application
18
2887
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 that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
4
3722
by: Don Peters | last post by:
I can't seem to find an answer to this problem in spite of many searches. I have a VB.NET program that has a context menu popup event. In it I associate some menu items with their events, as follows: ContextMenu1.MenuItems.Add("+1", New SystemEventHandler(AddressOf Me.Plus1)) ContextMenu1.MenuItems.Add("+2", New SystemEventHandler(AddressOf Me.Plus2))
3
1837
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the dynamically added textbox controls is saved to a database in the custom event handler. The custom event handler fires when the page posts back. I have a menu control which causes a postback, and the custom event handler then fires if a textbox...
6
2271
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 the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When adding toolbar button: tbButton.Click += new...
3
4793
by: RahimAsif | last post by:
I am writing an application that requires the a portion of the main menu to be dynamic. The menu has file, panels, view files and help across the top. The view files sub menu needs to be dynamically generated, and the dynamic generation needs to occur right when the user selects this menu item (that is on the Popup event handler). However, everytime I put following code on the Popup event handler (of the View Files menuitem) to dynamically...
4
2508
by: Me | last post by:
My form has a dynamically populated context menu, and each has the same event handler for the click event. The event need to write the text of the clicked menu item into a database, so I need to either pass that to the event handler, which I cannot figure out how to do, or access the properties of the sender, which I also cannot figure out how to do. The context menu items are a list extracted from a database, using the code below in...
12
4029
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, no items are selected. When the first item is selected by clicking either the left or right button, the SelectedIndexChanged event fires but not the MouseUp event. After the first item is selected and another item clicked on, both the...
1
3159
by: Lila Godel | last post by:
My VB.NET 2008 application is setup with a Sub Main and no forms. At run time a NotifyIcon is created with one context menu choice (Close which terminates app). I have no trouble running the application and when I select Close from the icon's right click menu the system tray icon goes away and the application is removed from the listing on the processes tab of task manager. My problem is that the FileSystemWatcher event does not work....
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.