473,408 Members | 2,477 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,408 software developers and data experts.

[C#] Custom event args for the Click event of a menu item

Hi there,

Let me start off by saying that I'm working in Visual Studio 2005, using Visual C#.
I've been trying to pass custom Event arguments to the Click event of a ToolStripMenuItem, but I can't figure it out.

I'm probably just doing it all wrong, hence why I'm coming here for help.

What I've done is the following:

First, I create a custom eventargs class
Expand|Select|Wrap|Line Numbers
  1. public class EventArgs<T> : EventArgs
  2.   {
  3.     public EventArgs(T value)
  4.     {
  5.       argValue = value;
  6.     }
  7.  
  8.     private T argValue;
  9.     public T Value
  10.     {
  11.       get { return argValue; }
  12.     }
  13.   }
  14.  
For testing purposes, my eventhandler looks like this:
Expand|Select|Wrap|Line Numbers
  1. void MenuItem_Click(object sender, EventArgs<string> e)
  2.   {
  3.     MessageBox.Show("YAY!");
  4.   }
  5.  
Then, to call the eventhandler, I do this:
Expand|Select|Wrap|Line Numbers
  1. Item.Click += new EventHandler<EventArgs<string>>(MenuItem_Click)(this, new EventArgs<string>("test"));
All of this results in the following error:
Expand|Select|Wrap|Line Numbers
  1. Cannot implicitly convert type 'void' to 'System.EventHandler'
  2.  
My guess is that I'm doing something wrong at the event assignment, but I've tried so many things now, that I'm asking for help.


What am I doing wrong?


PS: Apologies for any flaws in my English, it's not my native language, so I might've made some weird sentences.
Oct 25 '07 #1
3 5425
Plater
7,872 Expert 4TB
I am pretty sure you have to use the definition for the delegate defined for the event. I don't believe you can just "change" them without inheriting the class and adding your own event with your own delegate.
Especially since you will never be able to pass your own arguments into the handler since you can't "call" the event the way object itself triggers the event. (Unless you can do that inheriting thing)
Oct 25 '07 #2
How exactly would this work with a delegate then?
Sorry for the silly question, but I've never really used delegates.

On a sidenote though, I thought it was possible to alter Eventargs to pass your own data through.
Oct 25 '07 #3
Plater
7,872 Expert 4TB
It's very possible to have a variable declared that can be used both inside and outside of your event handler if you wish.


This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.
msdn
Oct 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jesse.hartwick | last post by:
Hey group! I have a toolbar with a "DEVELOPER" menu. I have it so that it when the menu title is clicked, a pop-up form will appear if the user has not yet verified that he or she has developer...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
5
by: james | last post by:
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...
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...
27
by: Wayne | last post by:
I've been clicking around Access 2007 Beta 2 and can't see the custom menu bar designer. Is it in the beta? Maybe I'm blind. The question that comes to mind is: Will custom menu bars be the same...
10
by: jubei | last post by:
I've been trying to write a custom widget for a project, however when i try to run code that uses it i get the following error > ./nokiagui.py Traceback (most recent call last): File...
2
by: trialproduct2004 | last post by:
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...
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
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
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.