C# Capture mouse right click in ToolStrip Drop Down Item Clicked - c#

I want to capture the right click mouse event within the ToolStripDropDownItemClicked Event
private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
Based on the right click, i display a contextmenu. So, I need to get both the item clicked and mouse click right.

You could use the MouseUp or MouseDown event it knows which button was pressed.
The sender object is the menu item that was clicked. Cast it to the correct type to access it's properties. If you're not sure about the type you could use as and then check if the resulting object is null.
void firstToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
var toolStripMenuItem = (ToolStripMenuItem)sender;
var nullIfOtherType = sender as ToolStripMenuItem;
var right = e.Button == System.Windows.Forms.MouseButtons.Right;
}
How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

Related

Why in every right click, the context menustrip pop up,how to enable it in a specify position by controlling the right click event?

Why in every right click, the contextmenustrip pop up,how to enable it in a specify position by controlling the right click event??
Try to remove the default contextmenustrip and create a new one.
With HitTest on a control you can check if the clicked position is on a element
private void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (dataGrid.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.ColumnHeader)
{
new ContextMenuStrip().Show(dataGrid, e.Location);
}
}
}
See: How do I correctly position a Context Menu when I right click a DataGridView's column header?

How to stop mouse left button function on datagridview c#

Mouse left button should be stop select or click when i edit cell in datagridview. i don't know how to disable mouse left button in windows application.
you can manage the action in MouseClick event:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// do something
}
}

How to get the item right clicked on from ContextMenuStrip?

I have a PictureBox.
If I right click the PictureBox, my ContextMenuStrip (right click menu) appears.
In that ContextMenuStrip is a ToolStripMenuItem (one of the options in the right click menu).
There is an event on the ToolStripMenuItem that handles what happens if that option is clicked.
We're starting from ToolStripMenuItem's "Clicked" function. I need to somehow get back to the PictureBox programmatically. From there, I can modify the PictureBox.
ToolStripMenuItem -> ContextMenuStrip -> PictureBox
How would I go about this?
If the click event handler for your menu item is named OnToolStripMenuItemClick, the following might be an approach to your problem:
private void OnToolStripMenuItemClick(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
var contextMenu = menuItem.Parent as ContextMenuStrip;
var pictureBox = contextMenu.SourceControl;
}
Of course, don't forget to check for null when accessing properties after conversion with as.
I'm not sure that I really understood your problem, but I guess you want to let users can return to the picturebox when they want to cancel current operation by clicking the right button. In this case, you should not implement your work in click event, because right and left button both can trigger the click event, instead, you should process your work in the event "MouseUp", like this:
private void menuItemBack_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("back item is clicked");
}
else
{
MessageBox.Show("I will come back.");
//do your return things here.
}
}
I've just come across this same problem in C#, and the path to the value seems to be something like:
sender.Owner.SourceControl;
However, since sender, Owner and so on are generic classes, I had to cast everything as follows:
PictureBox pb = (PictureBox) ((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl;
Ugly, but it works.

TaskBarButton middle mouse button event

I have a TaskBar with buttons. at the TaskBar there is a lot of events, but there is only one event at the click of a button.
TaskBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.TaskBarButtonClick);
I need an event to a button press TaskBar middle mouse button.
something like
if (e.Button == MouseButtons.Middle)
{
MessageBox.Show("Middle");
}
only taskbar
I know this example. I did that. the problem is that the event for the Taskbar. I need an event to the button provided on this TaskBar
e.Button is not of type MouseButtons. It is of type ToolBarButton. So it references the location on the toolbar that is clicked, not the location on the mouse used to make the click.
Toolbar Button
If you need to handle which toolbar button is clicked then reference this example for using the ToolBarButtonClickEventHandler works.
//add some buttons.
TaskBar.Buttons.Add(new ToolBarButton()); //index 0
TaskBar.Buttons.Add(new ToolBarButton()); //index 1
//add the handler
TaskBar.ButtonClick += new ToolBarButtonClickEventHandler (
this.taskbar_ButtonClick);
private void taskbar_ButtonClick (Object sender, ToolBarButtonClickEventArgs e)
{
// Evaluate the Button property to determine which button was clicked.
switch(TaskBar.Buttons.IndexOf(e.Button))
{
case 0:
//Whatever you want to do when the 1st toolbar button is clicked
break;
case 1:
//Whatever you want to do when the 2nd toolbar button is clicked
break;
}
}
Mouse Button
You could add an event handler for the MouseDown event to trap the Mouse button that was clicked.
TaskBar.MouseDown += new MouseEventHandler(this.taskbar_MouseDown);
private void taskbar_MouseDown(object sender, MouseEventArgs e)
{
// Determine which mouse button is clicked.
if(e.Button == MouseButtons.Middle)
{
MessageBox.Show("Middle");
}
}

How can I determine which mouse button raised the click event in WPF?

I have a button that I trigger OnClick whenever there is a click on that button. I would like to know which Mouse button clicked on that button?
When I use the Mouse.LeftButton or Mouse.RightButton, both tell me "realsed" which is their states after the click.
I just want to know which one clicked on my button. If I change EventArgs to MouseEventArgs, I receive errors.
XAML: <Button Name="myButton" Click="OnClick">
private void OnClick(object sender, EventArgs e)
{
//do certain thing.
}
You can cast like below:
MouseEventArgs myArgs = (MouseEventArgs) e;
And then get the information with:
if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
// do sth
}
The solution works in VS2013 and you do not have to use MouseClick event anymore ;)
If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button.
If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.
void OnClick(object sender, RoutedEventArgs e)
{
if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
{
// It's the right button.
}
else
{
// It's the standard left button.
}
}
Edit: The WPF equivalent to SystemInformation is SystemParameters, which can be used instead. Though you can include System.Windows.Forms as a reference to obtain the SystemInformation without adversely effecting the application in any way.
You're right, Jose, it's with MouseClick event. But you must add a little delegate:
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);
And use this method in your form:
private void MyMouseDouwn(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
this.Text = "Right";
if (e.Button == MouseButtons.Left)
this.Text = "Left";
}

Categories