1)action on a control is called as an event
2)in .Net, always event takes 2 args only always first arg will be of type object but second arg varies(changes) from event to event
C#.Net events are divided into 5 groups
1)mouse related events
2)focus related events
3)drag related events
4)key related events
5)other types of events
Working with mouse related events
1)click
2)double click
3)mouse enter
4)mouse leave
5)mouse down
6)mouse up
7)mouse move
8)mouse hover
Example on mouse move
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place a picturebox
properties:
set image=select an image
sizemode=stretch image
code for Form1_mousemoveevent
(to open this event, choose form1-properties->click on yellow colored Icon->double click on mousemove)
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.X + " " + e.Y;
pictureBox1.Location = new Point(e.X , e.Y);
Cursor.Hide();
}
}
}
Goto F5
Example on mouse down
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual Studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
code for Form1_mousedownevent
(to open this event, choose form1-properties->click on yellow colored Icon->double click on mousedown)
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
// mouseeventargs holds, the actual position of mousepointer in e.x,e.y
PictureBox p = new PictureBox();
p.Image =new Bitmap("e:\\fishleft.gif");
p.SizeMode = PictureBoxSizeMode.AutoSize;
p.Location = new Point(e.X, e.Y);
this.Controls.Add(p);
MessageBox.Show("\\tiger");
}
}
}
Execute the project F5
Working with focus related events:
1)leave: will be executed(fired) while cursor is coming out from a control
2)enter: Will be fired while cursor is coming into a control
Example on Leave event
Q)if Eno textbox is empty, Then display a blinking icon
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place 2 labels & 2 textboxes as shown
Eno
Ename
Place Errorprovider control->properties and set blink style=always blink
Code for textbox_Leave event
(to open this event, choose textbox1-properties->click on yellow colored Icon->double click on leaveevent)
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length == 0) //textbox is empty
{
errorProvider1.SetError(textBox1, "Eno should not be empty");
textBox1.Focus();//move the cursor into textbox1
}
else
errorProvider1.SetError(textBox1, "");
//dont give any space in between
}
}
}
Excute the project F5
Working with drag related events:
To work with drag related events, Allowdrop property is must set to true.
Example on dragrelated events
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place a textbox->properties set multiline=true
scroll bars=both
allow drop=true
code for textbox1_dragenter event
(to open this event, choose textbox1-properties->click on yellow colored Icon->double click on drageenterevent)
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 WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
//check the type of dragged data
if (e.Data.GetDataPresent(DataFormats.Text) == true)
{
textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
}
else
MessageBox.Show("this data is not supported");
}
}
}
Execute the project F5
Example on display dragged image
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
code for form1_drageenter event
(to open this event, choose textbox1-properties->click on yellow colored Icon->double click on leaveevent)
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
{
string[] x = (string[])e.Data.GetData(DataFormats.FileDrop);
this.BackgroundImage = new Bitmap(x[0]);
}
}
}
}
Execute the project F5