SIGN UP MEMBER LOGIN:    
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
share this article :
post comment
 

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

The type or namespace name 'WinForms' does not exist in the namespace 'System' (are you missing an assembly reference?) what should i do now please help me

Posted by ali Apr 30, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor