Add a function to contextMenu item at notifyIcon - c#

I use an contextMenu1 and an notifyIcon1 for the app. When the app is in Tray Icon and I will press Right Click, a menu will appear.
The code is this (I add only 2 items for test):
contextMenu1.MenuItems.Add("View");
contextMenu1.MenuItems.Add("Exit");
notifyIcon1.ContextMenu = contextMenu1;
In this moment, in the menu I see only the items that don't do enything.
How I can add a function, like private void exit() to the contextMenu1.MenuItems.Add("Exit"). When I will pres the Exit item, to close my app (example).

There is a second parameter to Add that lets you assign an eventhandler:
contextMenu1.MenuItems.Add("Exit", ExitApplication);
// or using an anonymous method:
contextMenu1.MenuItems.Add("Exit", (s,e) => Application.Exit());
In the first example, ExitApplication is your event handler:
private void ExitApplication(object sender, EventArgs e)
{
// exit..
}
You can also construct a MenuItem first and assign the eventhandler in the constructor, if you prefer.

I am assuming that you have a Windows Form and a Button (name : btnShowMessage). When you dobule click on the button you will get a event handler "btnShowMessage_Click". Also you have a notificationIcon with ContextMenuStrip attached with it. You have even an menu option in the context menu strip (name : btnContextOpenMsg). With The following steps you can use to achieve your requirement.:
Below image is for your clear understanding :
Go to Context Menu --> select btnContextOpenMsg
Press F4 to open the property sheet
Click on "Events" button on top of the property sheet
Click on "Click" and expend the drop down beside the click event by clicking "..."
Select the btnShowMessage_Click from the drop down.
Compile you code after saving it.
You should see your notification (system tray) menu when you minimized your app.
Click on the "Show Message" option so that it will execute the same function as for the button.

Related

How to have a split button show context menu only when the split part is clicked?

I have found many question concerning split buttons, but I need clarification.
I am using a Wisej split button. The following code will show a context menu to the bottom left of the button part(left-side of the split button which has button text) of a split button.
private void splitButton1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(-20, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenu1.Show(ptLowerLeft);
}
I want to know is how to have a context menu show only when the split part (i.e. the small section on the right-side of the split button with an arrow icon) of the split button is clicked and not on the button side.
You don't need to use a context menu object simply add the menu items to the SplitButton.MenuItems collection. You'll get a Click event when clicking on the button and an ItemClicked event when clicking on a menu item. The menu items will show only when clicking on the split part of the button.
When adding MenuItems to a Button (instead of a SplitButton) the menu will always show when clicking on the button.
The latest release added the Orientation property to SplitButton to show the split part below the button when set to Vertical.
HTH. Sorry for the late reply.

cannot set cancel button to forms?

I'm learning on programming with C# windows forms. Then I discover cancelButton property. I try to set this property to my form so that when I hit ESC it would close my form. But when I double click cancelButton in my form's property, there nothing happen except VS marks my Form1.cs as unsaved. No method created after the double click. I tried to create private void cancelButon(object sender, EventArgs e){} but the dropdown box where I select method for cancelButton refuses to show my method. The same thing happens to AcceptButton. I have tried to create a brand new project, but it would not help.
Is that VS's bug, or am I missing something?
You've misunderstood what the form's CancelButton and AcceptButton properties are for...
To make the form's CancelButton property work you first have to add a button to the form. Then you select that button from the drop-down list next to the form's CancelButton property.
What this does is to automatically click the Cancel button when the user presses the Escape key.
Similar logic applies to the form's AcceptButton property, except that it will cause the associated button to be clicked when the user presses the Enter key.
Having done that, you STILL HAVE TO ADD A HANDLER FOR THE BUTTON CLICKS.
To do that, double-click on the button in the form (displayed in the designer) - that is what will automatically add the handler for you.
To summarise:
Form.CancelButton -> Determines which button will be clicked when user presses Escape.
Form.AcceptButton -> Determines which button will be clicked when user
presses Enter.
To add a handler for a button, double click the button in the designer.

Why does my MenuStrip appear in the incorrect Location on first click?

I'm working on a VB Winforms project (although I'm just as fine with a C# solution) and have the following set up:
I have a ContextMenuStrip on the form, msCreateReports
I have a MenuStrip at the top of the form with one menu item being Create Reports and its DropDown set to msCreateReports
I have a command button on the form cmdCreateReports
Now, for my command button, I have the following code for its click event:
Private Sub cmdCreateReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateReports.Click
msCreateReports.Show(Cursor.Position.X, Cursor.Position.Y)
End Sub
EDIT (Update / Explanation):
Basically, what I'm looking for functionality-wise is to have this contextmenustrip be able to show up in one of 2 different places, depending on where the user clicks...
If they click on the menu option, the context menu appears as a drop-down on the Create Reports menu item OR if the user clicks the command button, the menu will apeear as a context menu on the side of the mouse pointer. I want the same menu to be able to appear on either one of these two locations depending on where the user clicks to make this menu appear.
Now, my problem is that only the first time the command button is clicked, the ContextMenuStrip appears up top by the menu, rather than on top of the command button, as I would like it to.
After the first click, the menu appears in the correct location... What did I do wrong / how can I fix this??
Thanks!!
I have the same problem (I'm using Visual Studio 2010 SP1, and C#). I don't think we did anything wrong, it looks like a Winforms bug to me.
I fixed it like this:
1) I have unset (using the visual designer) the DropDown property of the main strip item.
2) I have defined the Opening event on the contextMenuStrip, and the DropDownOpening event on the main strip item like this:
private void toolStripMyMenuItem_DropDownOpening(object sender, EventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
And I don't have this problem anymore. Hope it will help you too :-)
Cursor.Position.X and Cursor.Position.Y are relative to Form, you need to use the Overloaded method ContextMenuStrip.Show(Control control, Point pos)
Example :
//control = the control you have added context menu
msCreateReports.Show(control, new Point(Cursor.Position.X, Cursor.Position.Y));
I had the same problem. I believe this to be a bug also as this behavior only happens the first time. So I force it to open and then close it before the user has a chance to interact with it. So in effect I use it once, before the user does and then it behaves correctly. Here is the code I added in the form load method. My combo box is used for allowing the user to select from a list of printers.
printerToolStripMenuItem.ShowDropDown();
toolStripComboBoxPrinter.Owner.Hide();
printerToolStripMenuItem.Owner.Hide();

How to find Context menu was shown and not to handle click events

I got a quick question,
I am developing a custom chart control. The chart control let users to draw lines or drop rectangle (could be anything). This start draw/ drop options are set to controls mouse down, left button click and mouse down right click is set to show context menu.
My problem is, if user decide not to select anything and come out from context menu while it is shown they should left click on control as right click will show the menu again. But at the moment context menu is diapering and rest of the left click also executed. How can I verify context menu was shown on left click and user wants to come out from context menu and not to execute start draw/ drop logic.
Additional Info:
I am working on C# project, I tried to capture context menu closed event but it triggers and closes itself before it comes to controls mouse down event and I can not verify was context menu was shown before to avoid going through rest of the left mouse button down event logic.
Any help is greatly appreciated in advance.
Here's a simple solution.
Suppose you have a ListBox control on your form which has a ContextMenu associated with it. Now we want to add a list item to the control each time it is clicked:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
}
Now define a bool variable at your form level called menuClosed like so:
private bool menuClosed = false;
Now capture the context menu's Closed event like so and flip the flag:
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
menuClosed = true;
}
Now update the code that adds an item to the list box control like the following:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (!menuClosed)
listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
}
I'm just setting the bool variable to true when the context menu is closed, then i check for the bool flag to see if an item should be added to the list. you can use this kind of same mechanism to determine a specific command should be executed or not.

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 and 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);
}
}

Categories

Resources