ARTICLE

Handling Mouse Events in C#

Posted by Mahesh Chand Articles | C# Language January 25, 2001
This article explains how to handle mouse events in C# or VB. In C#, you write a delegate and then write an event handler.
Reader Level:

Tools Used : Visual C# .NET

Handling events in C# is little bit tricky than in C++ or VB. In C#, you write a delegate and then write an event handler.  These event handlers are overridable public events defined in the Control or other WinForms classes. 

1. Write a Delegate

I want to handle Mouse Down events and do something when left or right mouse buttons are pressed. Write this line of code in your InitializeComponent function.

this.MouseDown += new System.WinForms.MouseEventHandler(this.Form_MouseDown);

2. Write the Event

Now you write the event handle. The output parameter of your event returns System.WinForms.MouseEventArgs object which gives you the details about mouse down such as what button is pressed or how many times. Here are MouseEventArgs members.

MouseEventArgs members 

Button Indicates which mouse button was pressed. It could be Left, Right, Middle, or None.
Clicks Indicates the number of times the mouse button was pressed and released. 
Delta Indicates a signed count of the number of detents the mouse wheel has rotated. 
X The x-coordinate of mouse click.
Y The y-coordinate of  mouse click. 

Event Handler

private void Form_MouseDown(object sender, System.WinForms.MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
MessageBox.Show(this,"Left Button Click");
break;
case MouseButtons.Right:
MessageBox.Show(this,"Right Button Click" );
break;
case MouseButtons.Middle:
break;
default:
break;
}
 
}

Login to add your contents and source code to this article
post comment
     

Hi, This is urgent. pls help me. I have got a scenario. There will be a button filled with a colour placed in the windows form, the colour of the button should change when we move the mouse and place that over the button.(the changes should happen only when moving the mouse and placing over the button and not by clicking the button)I am told that it can be done in two ways either by writing a code or by making some changes in the properties. So could u pls help in both the ways since I am new to this. pls give me complete codings so that i can learn and try for different scenariosThanks in advance

Posted by Keerthana Bhaskaran Jan 19, 2013

help me,.. how to move image in panel after copy from picturebox... my code private void pnlPicture_DragDrop(object sender, DragEventArgs e) { Panel panBack = (Panel)sender; Graphics g = panBack.CreateGraphics(); g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(e.X, e.Y)); }

Posted by ichsan delta Mar 06, 2011

Can we perform the same functionality in asp.net

Posted by Vidya Yadav Feb 15, 2011

i think your article are very cheap.
and not useful for every one
thanks, mori from malysia

Posted by morteza azizi Jul 27, 2010

hi
i want change cursor mouse to the picture when mouse over to a picturebox(to the same picture in the picture box)

Posted by rimaa gabara Jun 06, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts