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");
}
}
Related
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?
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?
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
}
}
I'm using C# windows form application, and i'm facing a problem resetting a button's back Color.
By clicking a button I need it to change its back color and reset the back color of previously pressed button.
Please note that i have lots of buttons in the form and i'm using "sender" to apply same clicking event to all buttons.
You'll need to keep track of the last button that was clicked. Add a private field and then manipulate it in the on click event handler:
class Form1 : Form
{
private Button _lastButtonClicked;
protected void ClickHandler(object sender, EventArgs e)
{
if (_lastButtonClicked != null)
_lastButtonClicked.BackColor = Color.whatever;
_lastButtonClicked = sender as Button;
_lastButtonClicked = Color.newcolor;
}
}
I have a mousemove event that takes the position of the cursor and outputs it to two labels (X and Y), the value dynamically changes as I hover around. I have a mousedown event that when clicked, the same values are outputted to a textbox. How can I combine the mousedown and mousemove events so that when I hover AND hold down the mouse button, the textbox value dynamically changes as I move.
You can interrogate the mouse buttons in your Move event handler, i.e. :
void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
String tipText = String.Format("({0}, {1})", e.X, e.Y);
trackTip.Show(tipText, this, e.Location);
}
}
Track the mouse down and mouse up events to set a variable determining whether or not the mouse button is pressed (ie set in down unset in mouse up) then just check this variable in mouse_move
see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspx
for an example
Use
private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
}
}
like this and in second if you will have a condition when your mosue moved and mouse Left button is down.