How do I programatically show the DropDown of ContextMenuStrip? - c#

I need a button to show the ContextMenuStrip associated with it on mouse left click instead of right click. How can I achieve this?

Try the code below
Point location = button1.PointToScreen(Point.Empty);
contextMenuStrip1.Show(location);
PointToScreen gets the actual location of your button and shows the context menu strip where the button located.

If cmenEdit is your ContextMenu and btnEdit is your button, in your BtnEdit_Click event you will have this:
cmenEdit.Show(btnEdit, new Point(0, btnEdit.Height));
ContextMenu will show under the button, and will look like this (as an example)

Related

Copy value to Clipboard via right click with menu when mouse right click

Please this event:
private void pieLegend_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var hoveredItem = this.pieLegend.Items.FirstOrDefault(x => x.IsHovered);
Clipboard.SetText(hoveredItem.Title);
}
This event fired after PicChart Legend title right click.
When mouse is over and right click the Legend title is copy but this is not obvious because no menu is opened so is i possible to add the option Copy to the right click before the value is copy to Clipboard ?
You have to add a ContextMenuStrip with a "Copy" ToolStripMenuItem to your form and set it as the ContextMenuStrip property of your PicChart control. This automatically works as a context menu, so you do not need to check any right-click event.
You just need to put your code in the Click event of the ToolStripMenuItem inside your menu strip then.

Adding Click Handler On UserControl Breaks Click On Children

I have a user control that I'm trying to make draggable. The whole control should be draggable except when you click on buttons or text boxes. I'm handling the mousedown, mouseup and mousemove events on the usercontrol itself and I can drag by clicking anywhere. The only issue is now I can't click any buttons on the user control. Any clue what's going on?
Code is something like this:
<UserControl PreviewMouseLeftButtonDown="Popup_PreviewMouseLeftButtonDown" ....STUFF...>
<!-- CAN'T CLICK THIS -->
<Button />
<UserControl>
Code Behind:
public void Popup_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldMousePosition = this.PointToScreen(e.GetPosition(this));
this.Popup.Child.CaptureMouse();
}
The issue arises when you use CaptureMouse() - this permanently captures all your mouse input on the window, and makes it so that you're unable to click on anything within the Window. I don't know if it's different for you, or if you checked, but it's not (just) that the Button is unclickable - it's that literally everything on the Window is unclickable.
You have to actually do something with the mouse after you've captured it, and then once you finish that, you have to return normal control by calling Mouse.Capture(null). For you, this would probably be best to do in your MouseUp() method.
However, this still leaves the child problem. I can't really think of any way you're going to be able to both capture all mouse click events on a parent control and allow them to get to the child control. I suppose you could check the mouse position against the button position, both relative to the UserControl, then route the click event to the Button every time, but this seems a little overelaborate. Is there a reason you can't just add a full-sized Grid to the UserControl with a lower ZIndex than the Button, and just use that to detect if a click was made inside the UserControl but not on the Button?

get the splitcontainer context in user control click event

Sir,
I have a split container in which in right panel i have a usercontrol.In the user control i have few buttons like view,new,edit etc.Bottom of that user control a form will open based on what link is clicked on the left side navigation pane. now when i click the user control's view button, i should open a new form below it. how to get the context of splitcontainer in the click event?also if i want to retrieve the form values to save in database when i click the save button in user control, how to do it?
A Click event has a sender parameter, which is the clicked button. You could use the name of the button to resolve which form should be opened.
To get your button in the click event:
Button clickedButton = (Button)sender;
To get the parent of your button (if it was SplitContainer, you'll have to use Parent property 3 times, because the first one will get you your UserControl, second - left panel of the SplitPanel, which doesn't have a Name property, third - your SplitPanel, and 4th, if you want, your form name)
string splitPanemName = clickedButton.Parent.Parent.Parent.Name;
...or you can just get whole SplitPanel object:
SplitPanel currentSplitPanel = (SplitPanel)clickedButton.Parent.Parent.Parent;
...or Form object:
Form currentSplitForm = (Form)clickedButton.Parent.Parent.Parent.Parent;
To do this, you have to be sure of the composition of your Form, so you can get the right controls at the right place.

Adding a right click / context menu to listbox items in C#

I have a ListBox, and its populated with items, id like to know how to:
when you rightclick in the listbox, that the rightclicked item will be selected,
a rightclick menu will be displayed with several items..
when you click on any of the items, a corresponding void will be triggered..
thanks in advance for any help, and code examples please!
This feels to me like a "homework" question, so I am going to answer by (I hope) giving you just a few pointers to solving this for yourself.
Phase One
create a sample project with a ListBox
define event handlers for MouseClick, MouseDown, and Click events.
put a Console.WriteLine("some appropriate text"); statement in each of those handlers so you can look in the Output Window in Visual Studio and see which event handler was called.
...
Phase Two
run the test program and observe the difference between what events are reported for a left mouse down and a right-mouse down (assuming your environment has the context click set to the right mouse down ... which may not be true for everyone).
focus on the one event you can intercept with a context-click.
add a context menu to the test project and set that context menu to be the context menu of the ListBox.
verify that you can now right click on an item in the ListBox and that the context menu will appear, BUT THE EVENT IS STILL HANDLED BY THE HANDLER YOU "DISCOVERED" IN STEP 2.
now go through all the Event handlers for ListBox, and figure out which one could be used to detect, given a certain location in the ListBox, which List Item was "hit."
once you can determine which List Item was right-clicked on, and you know your context menu is working, you have only the problem of making sure that the right-clicked List Item is selected : and that's pretty easy.
Figuring this out yourself will teach you several very useful things you'll be able to use later in programming to other controls.
best of luck, Bill
First, you need to subscribe to ListBox.MouseClick event. You will be able do determine what button was pressed and cursor coordinates. Then, use ListBox.IndexFromPoint method to find clicked item. You can select it using ListBox.SelectedIndex property. To display context menu use ContextMenu or ContextMenuStrip classes. Additional documentation on context menu is available in MSDN
1.when you right click in the listbox, that the right clicked item will be selected
2.a right click menu will be displayed with several items..
private void listBoxNode_MouseUp(object sender, MouseEventArgs e)
{
int location = listBoxNode.IndexFromPoint(e.Location);
if (e.Button == MouseButtons.Right)
{
listBoxNode.SelectedIndex = location; //Index selected
contextMenuStrip1.Show(PointToScreen(e.Location)); //Show Menu
}
}
3.when you click on any of the items, a corresponding void will be triggered..
private void showDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
//put your code here after clicking
//on items in context menu
}

How to get system tray functionality WITHOUT using NotifyIcon.ContextMenu?

I'm trying to get my application to display a popup context menu when a user right-clicks on my notify icon in the system tray... but there's a twist.
I'm aware that the NotifyIcon class I'm using to get the icon in the system tray has a ContextMenu property. I don't want to use that to get a right-click popup menu, because it ALWAYS displays a right-click popup menu, and never does anything else. When my main form is displaying a modal dialog, I want right-click to activate the main form, NOT display a popup menu.
So, I'm guessing I need to use the NotifyIcon.MouseClick event, and manually pop up the menu in that event? Here's where I've got to so far:
private NotifyIcon trayIcon;
private ContextMenu iconMenu;
private void frmMain_Load(object sender, EventArgs e) {
// [...]
this.trayIcon.MouseClick += new MouseEventHandler(trayIcon_MouseClick);
iconMenu = new ContextMenu();
// [...]
}
private void trayIcon_MouseClick(object sender, MouseEventArgs ea) {
this.iconMenu.Show(Program.instanceFrmMain, new Point(System.Windows.Forms.Cursor.Position.X - Program.instanceFrmMain.Left, System.Windows.Forms.Cursor.Position.Y - Program.instanceFrmMain.Top));
}
Notice how in iconMenu.Show, because it takes popup co-ordinates relative to the parent control (my main form here), I'm annoyingly having to subtract the parent control's co-ordinates from popup co-ordinates, something I already don't want to have to do.
Apart from that, here are the problems I'm having:
Although the menu does popup on right-click, it doesn't close if I click somewhere else on the screen outside the menu - and it should.
The menu doesn't quite popup in the right location; for other system tray apps, it pops up so its bottom-right or bottom-left corner are at the tip of the mouse cursor. For mine, the popup menu is at the base of the screen, to the side of the mouse cursor.
Any ideas how I can get this to work better? I know it's possible, plenty of other apps manually handle the displaying of a popup menu manually instead of using some NotifyIcon.ContextMenu property.
Use the ContextMenuStrip property rather than ContextMenu. The ContextMenuStrip class has an Opening event, which you can cancel by setting e.Cancel = true. That way you don't have to worry about the location of the menu, since it is automatically handled
OK, well I didn't manage to get the functionality I wanted as I described in the original question, but I have managed to find a way to achieve the desired effect using a different method.
I DO attach a ContextMenu to the trayIcon.ContextMenu property, but I attach event handler code to the Popup property of the context menu itself. If, in that handler, I .Clear the ContextMenu, it actually doesn't appear at all, allowing my code to elect to effectively stop the trayicon's popup menu from showing if it wants to. This was the effect I was looking to achieve. If I populate the ContextMenu in the Popup event handler code instead, the menu pops up as usual containing what I populated it with.
Sooo, I managed to solve the problem a different way. :-)

Categories