473,386 Members | 1,827 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.

Event handler for context menu; help needed

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

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 ApptEventHandler(object sender, ApptEventArgs e);
Then I have created a function to put the code in for when the event fires:

public void NewApptEvent(object 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.ApptOp = "NEW";
NewApptArgs.ApptUID = 0;
NewApptArgs.ColID = ColIdx;
NewApptArgs.RowID = RowIdx;
//Create Menu Items
ToolStripMenuItem NewItem = new ToolStripMenuItem("New
Appointment",global::Itinerary.Properties.Resource s.NewAppt, new
ApptEventHandler(NewApptEvent(this,NewApptArgs)));
.......

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 ...ToolStripMenuItem(string,
System.Drawing.Image, System.EventHandler) has some invalid arguments"
"Argument 3: Cannot convert from <project>.ApptEventHandler' to
'System.EventHandler'"
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 3810
"james" <james@com> wrote in message
news:44*********************@news.zen.co.uk...
Hello,
I am having a little trouble creating an event handler for a context menu
toolstripmenuitem.

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 ApptEventHandler(object sender, ApptEventArgs e);
Then I have created a function to put the code in for when the event
fires:

public void NewApptEvent(object 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.ApptOp = "NEW";
NewApptArgs.ApptUID = 0;
NewApptArgs.ColID = ColIdx;
NewApptArgs.RowID = RowIdx;
//Create Menu Items
ToolStripMenuItem NewItem = new ToolStripMenuItem("New
Appointment",global::Itinerary.Properties.Resource s.NewAppt, new
ApptEventHandler(NewApptEvent(this,NewApptArgs)));
......

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 ...ToolStripMenuItem(string,
System.Drawing.Image, System.EventHandler) has some invalid arguments"
"Argument 3: Cannot convert from <project>.ApptEventHandler' to
'System.EventHandler'"
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 ToolStripMenuItem will use when it's clicked - it is defined as
using a System.EventHandler 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:
ToolStripMenuItem NewItem = new ToolStripMenuItem("New Appointment",
global::Itinerary.Properties.Resources.NewAppt,
new EventHandler(MyEventHandler));

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**********@btinternet.com> wrote in message
news:ub**************@TK2MSFTNGP05.phx.gbl...
I think the problem is that you cannot control the type of event handler
that the ToolStripMenuItem will use when it's clicked - it is defined as
using a System.EventHandler 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:
ToolStripMenuItem NewItem = new ToolStripMenuItem("New Appointment",
global::Itinerary.Properties.Resources.NewAppt,
new EventHandler(MyEventHandler));

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**********@btinternet.com> wrote in message
news:ub**************@TK2MSFTNGP05.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 NewApptEventHandler(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:

ToolStripMenuItem MDSubItem = new ToolStripMenuItem("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHandler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDownItems.Add(MDSubItem); //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.eventhandler, 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 NewApptEventHandler(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:

ToolStripMenuItem MDSubItem = new ToolStripMenuItem("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHandler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDownItems.Add(MDSubItem); //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 {NewApptEventHandler(this, MDApptArgs);};
effectively creates a hidden System.EventHandler 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
NewApptEventHandler 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 NewApptEventHandler(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:

ToolStripMenuItem MDSubItem = new ToolStripMenuItem("blah", null );
MDSubItem.Text = ApptDetail; //Item from datareader
MDSubItem.Tag = mnuApptUID; //Item from datareader
MDSubItem.Click += delegate {NewApptEventHandler(this, MDApptArgs);};
//MDApptArgs is an instance of my EventArgs class with the parameters for
this record
MoveDate.DropDownItems.Add(MDSubItem); //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 {NewApptEventHandler(this, MDApptArgs);};
effectively creates a hidden System.EventHandler 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
NewApptEventHandler 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
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...
4
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...
3
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...
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...
3
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...
4
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...
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,...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...
0
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...

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.