How to change Event properties & create new events in C# .NET
I'm using NotifyIcon (System tray icon) in my WinForms application. I also have a ContextMenuStrip assigned to it. When user right clicks on NotifyIcon this ContextMenuStrip pops up.
These are the only events that NotifyIcon has.
Click
DoubleClick
MouseClick
MouseDoubleClick
MouseDown
MouseMove
MouseUp
This contextMenuStrip items (ToolStripMenuItem) are dynamically generated. I mean there are few default items like 'About','Exit','Help' etc.. but other items are dynamically generated and inserted into this menu when user right clicks on it. I'm generating items and inserting into contextMenuStrip in Click
event handler
Now, I've two problems:
Problem is for an instant its showing the default menustrip and then my
Click
event handler executes and new update menu pops up. How can I avoid this? I don't want to see the default menu at all. In other words I need to override the default behavior.Other problem is since I'm handling the
Click
event (because I didn't find RightClick event) the left button click also is handled by the same handler. I want to do different things (like show application windows) on left click an开发者_JAVA百科d show dynamically generated contextMenuStrip on right click. How to acheive this?Why are there two different events like
Click
&MouseClick
? What else would we click with? Aren't these two interdependent. I mean when ever there is a MouseClick there is also a Click.
If you can point me to some examples. That would be great!
Item #1) The ContextMenuStrip
has events that allows you to handle any dynamic creation of menu items before the menu is displayed. See the Opening
and Opened
events.
Item #2) Use the MouseEventArgs
parameter to inspect the mouse-state when event was raised.
Item #3) Depending on the control, Click
and MouseClick
can be different. Take buttons for instance. When a button has focus, the "Click" event is raised when the user presses the Space or Enter key. In most cases, a MouseClick
generates a Click
event.
I'm the OP. I guess I've achieved it.
notifyIcon.MouseDown += new MouseEventHandler(notifyIcon_MouseDown);
and
static void notifyIcon_MouseDown(object sender, MouseEventArgs e)
{
NotifyIcon notifyIcon = sender as NotifyIcon;
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left Button Clicked"); // & do what ever you want
}
else
{
updateMenuItems(notifyIcon.ContextMenuStrip);
}
}
精彩评论