How to trap or capture mouse events in Windows Forms?

by Jagadish Pulakhandam on 4/10/2011 10:56:04 AM
Rated 0 from 0 votes
Brief: This sample demonstrates different mouse events which can be programmed using Windows Forms.
Posted to: Microsoft Windows Forms - Mouse Events
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

This sample demonstrates the following mouse events:

  • Click
  • MouseDown
  • MouseEnter
  • MouseLeave
  • MouseUp
  • MouseClick
  • MouseHover
  • MouseMove
Following is the screen shot:



It is also worth to take a look at a couple of properties modified for Textbox (multiline, anchoring etc.).

Following is the code:

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.namespace WinFormMouseEventsDemo
11.{
12.    public partial class Form1 : Form
13.    {
14.        public Form1()
15.        {
16.            InitializeComponent();
17.        }
18. 
19.        private void btnDemo_Click(object sender, EventArgs e)
20.        {
21.            this.txtMsgs.Text += "Clicked" + Environment.NewLine;
22.            TextboxAutoScroll();
23.        }
24. 
25.        private void btnDemo_MouseDown(object sender, MouseEventArgs e)
26.        {
27.            this.txtMsgs.Text += "Mouse Down" + Environment.NewLine;
28.            TextboxAutoScroll();
29.        }
30. 
31.        private void btnDemo_MouseEnter(object sender, EventArgs e)
32.        {
33.            this.txtMsgs.Text += "Mouse Enter" + Environment.NewLine;
34.            TextboxAutoScroll();
35.        }
36. 
37.        private void btnDemo_MouseLeave(object sender, EventArgs e)
38.        {
39.            this.txtMsgs.Text += "Mouse Leave" + Environment.NewLine;
40.            TextboxAutoScroll();
41.        }
42. 
43.        private void btnDemo_MouseUp(object sender, MouseEventArgs e)
44.        {
45.            this.txtMsgs.Text += "Mouse Up" + Environment.NewLine;
46.            TextboxAutoScroll();
47.        }
48. 
49.        private void btnDemo_MouseClick(object sender, MouseEventArgs e)
50.        {
51.            this.txtMsgs.Text += "Mouse Click" + Environment.NewLine;
52.            TextboxAutoScroll();
53.        }
54. 
55.        private void btnDemo_MouseHover(object sender, EventArgs e)
56.        {
57.            this.txtMsgs.Text += "Mouse Hoever" + Environment.NewLine;
58.            TextboxAutoScroll();
59.        }
60. 
61.        private void btnDemo_MouseMove(object sender, MouseEventArgs e)
62.        {
63.            this.txtMsgs.Text += "Mouse moving" + Environment.NewLine;
64.            TextboxAutoScroll();
65.        }
66. 
67.        private void btnClear_Click(object sender, EventArgs e)
68.        {
69.            this.txtMsgs.Text = string.Empty;
70.            TextboxAutoScroll();
71.        }
72. 
73.        private void TextboxAutoScroll()
74.        {
75.            this.txtMsgs.SelectionStart = this.txtMsgs.Text.Length;
76.            this.txtMsgs.ScrollToCaret();
77. 
78.        }
79.    }
80.}

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following