Mouse Events:::
There are several kinds of mouse events: (list from an MSDN page)
- Click
This event occurs when the
mouse button is released, typically before the MouseUp event. The handler for
this event receives an argument of type EventArgs. Handle this event when you
only need to determine when a click occurs.
- MouseClick
This event occurs when the
user clicks the control with the mouse. The handler for this event receives an
argument of type MouseEventArgs. Handle this event when you need to get
information about the mouse when a click occurs.
- DoubleClick
This event occurs when the
control is double-clicked. The handler for this event receives an argument of
type EventArgs. Handle this event when you only need to determine when a
double-click occurs.
- MouseDoubleClick
This event occurs when the
user double-clicks the control with the mouse. The handler for this event
receives an argument of type MouseEventArgs. Handle this event when you need to
get information about the mouse when a double-click occurs.
- MouseDown
This event occurs when the
mouse pointer is over the control and the user presses a mouse button. The
handler for this event receives an argument of type MouseEventArgs.
- MouseEnter
This event occurs when the
mouse pointer enters the border or client area of the control, depending on the
type of control. The handler for this event receives an argument of type
EventArgs.
- MouseHover
This event occurs when the
mouse pointer stops and rests over the control. The handler for this event
receives an argument of type EventArgs.
- MouseLeave
This event occurs when the
mouse pointer leaves the border or client area of the control, depending on the
type of the control. The handler for this event receives an argument of type
EventArgs.
- MouseMove
This event occurs when the
mouse pointer moves while it is over a control. The handler for this event
receives an argument of type MouseEventArgs.
- MouseUp
This event occurs when the
mouse pointer is over the control and the user releases a mouse button. The
handler for this event receives an argument of type MouseEventArgs.
- MouseWheel
This event occurs when the
user rotates the mouse wheel while the control has focus. The handler for this
event receives an argument of type MouseEventArgs. You can use the Delta
property of MouseEventArgs to determine how far the mouse has scrolled.
As you may gather, EventArgs provides less information for us than
MouseEventArgs.
Example..........(1)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace
mouseeventsexample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
Form_MouseDown(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
MessageBox.Show("Left Button
Click");
break;
case MouseButtons.Right:
MessageBox.Show("Right Button
Click");
break;
default:
break;
}
}
Example....(2)
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
mouseeventsexample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object
sender, EventArgs e)
{
textBox3.Text = (int.Parse(textBox1.Text)
+ int.Parse(textBox2.Text)).ToString();
}
private void textBox1_MouseEnter(object
sender, EventArgs e)
{
TextBox t = (TextBox)sender;
t.Font = new Font("Arial",
20F);
}
private void textBox1_MouseLeave(object
sender, EventArgs e)
{
TextBox t = (TextBox)sender;
t.Font = new Font("Microsoft
Sans Serif", 8.25F);
}
}
}
Comments
Post a Comment