Reputation: 23
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
Upvotes: 2
Views: 990
Reputation: 4713
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");
}
}
Upvotes: 1