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;
}
}
Triveni ChapalaPosted Apr 27, 2025, 4:57 PM
Super Excellent Nice
Joe WilsonPosted Dec 28, 2015, 2:08 AM
Well, it was so useful.
Keerthana BhaskaranPosted Jan 19, 2013, 12:41 AM
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 scenarios Thanks in advance
ichsan deltaPosted Mar 6, 2011, 3:28 PM
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)); }
Vidya YadavPosted Feb 15, 2011, 4:53 AM
Can we perform the same functionality in asp.net
rimaa gabaraPosted Jun 6, 2010, 8:30 AM
hi i want change cursor mouse to the picture when mouse over to a picturebox(to the same picture in the picture box)
aliPosted Apr 30, 2010, 4:42 PM
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
aliPosted Apr 30, 2010, 4:38 PM
The type or namespace name 'WinForms' does not exist in the namespace 'System' (are you missing an assembly reference?) what should i do now
aliPosted Apr 30, 2010, 4:36 PM
The type or namespace name 'WinForms' does not exist in the namespace 'System' (are you missing an assembly reference?) what should i do now
Ketaki KulkarniPosted Feb 22, 2010, 6:12 AM
I would like to actually move the image on mouse move event. How can I achieve this?
ariane mae burcaPosted Feb 23, 2009, 10:14 PM
mR mAHESH cHAND, PLEASE HELP ME BOUT MY PROBLEM IN MY FINAL EXAM BOUT IN ASSEMBLY LANGUAGE "HANDLING MOUSE" I NEED A CODE OF THIS PROGRAM. THNX, ARIANE MAE BURCA FROM PHILIPPINES
Luiz RodolphoPosted Dec 4, 2007, 1:26 PM
Error: namespace name 'WinForms' does not exist in the namespace 'System' (are you missing an assembly reference?) I dont know why ?!?!
shunmuga sundaramPosted Sep 1, 2007, 6:25 AM
i want to resize my image only when i move mouse on my image. please help me in this task.
ramesh goudPosted Jun 4, 2007, 1:49 AM
Hi, how to capture the mouse position in dual monitors.....if it is on the first monitor ir should display a mesage that "first" .similarly for second Thanks Ramesh d
Rohit AgrawalPosted Mar 15, 2007, 5:19 AM
Hi sir.. I want to change the shape of Cursor for whole system..after click on Button. ###################### OnClick_Button..... { this.Cursor=Cursor.Hand; } ##################### How can set the Cursor........ I tried this could , but it gives Error.... Cursor cursorShape=OkButton.getCursor(); OkButton.SetCursor(CursorShape.getpredefinedCursor(OkButton.Hand_Cursor));
Rohit AgrawalPosted Mar 15, 2007, 5:10 AM
hi sir... how can i read the properties of other application or webapplication ..(such as Object Spy / WinSpy working)
Vijayaram BoopalanPosted Mar 9, 2007, 1:08 AM
Hi Sir, How to Move multiple controls in the winform during runtime in c#. This has to be done after selecting multiple controls in the form by drawing a rubber rectangle over the surface. Similar to that of VS.NET 2005 IDE. Can you help me by providing some samples and hints. Thanks in Advance
Sam KhanjarPosted Feb 27, 2007, 4:24 PM
Hi Mahesh Chand, thank you so much for your artical regarding "Handling mouse events in C#" which you have posted in C# corner website. I need your help as I am new to C# I have a list box which consists of people names I would like to use the right mouse button to select a name and the left mouse button to delete the selected name. I am looking forward to hear from you very soon... thanks in advance, Sam