• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Events in C# .Net

Events

Action on a control is called an “Event”.

In C #. Net, always events takes two arguments

Always first argument will be object but second argument type may vary from event to event.

C #.net events are divided in to 4 groups

Mouse Related events

Focus Related events

Drag Related events

Key Related events

Mouse Related Events

Click

Double click

Mouse up

Mouse Down

House leave.

House enter

Mouse More

Mouse Mover: this event will be fired , when mouse pointer placed on a control for some time.

A program to use company logo as mouse pointer

Open windows forms Application Project

Place a picture Box control àproperties

Image = Select an Image.

Size  Mode = Stretch Image

Code for Form1 Mouse More Event

{

Picture Box 1. Location = new Point  (e. x, e. y);

Cursor . Hide ();

}

Developing Animated Text

Open Windows Forms Application Project

Form àProperties àBack color  = Black

Place a button

Place a Timer with Enabled = false

 

Using System . Drawing .Drawing 2D

 

Code in GD

Screenshot_66

String S = “MICRO SOFT TECHNOLOGIES”

Font f = new  Font  (“Arial ”, 50);

Linear Gradient Brush  b = new   Linear  Gradient Brush    (new point  (100, 100), new Point  (200, 300), color. Red, color .yellow );

Int x =5;

Graphics g; // abstract class

 

Code for Button 1_ click

{

g = this .create Graphics ();

Draw String (S, f,b 10,10);

Timer 1. Enabled = true;

}

 

Code for Timer 1_Tick event

{

X = X+3;

If (X > 300)

X= 0;

 

Linear Gradient Brush   b1 = new  Linear  Gradient Brush     (new point  (100, 100), new Point  (x, 300), color. Red, color .yellow );

);

draw String (S, f,b, 10, 10);

 

Focus Related Events

 

Screenshot_67

Leave: This event will be executed while cursor is coming out from a control.

Enter: This event will be executed while cursor is Entering in to a control.

 

Note :-

Generally these events will be used in Data Validations

 

Example on Leave Event 

Screenshot_68

 

Open Windows Forms Application Project

Place two Labels and two text boxes as shown

Place error Provider control àProperties àBack Style = Always Blink

Code for Text Box1 Leave Event

{

If (text Box1. Text = “”)

Error Provider1. Set Error (text Box 1,”Eno should not be Empty “);

Else

Error Provider1. Set Error (text Box 1,” “ .); // No Space

}

Observations

Error Provider1. Set Error () takes two arguments

Arg 1 : The control Name, after which a blinking  Icon need to be displayed.

Arg 2 : Error Message , if this  is empty , then  blinking  Icon will Will not be displayed.

 

Drag Related Events

C # supports four Types of Drag Related Events

 

Screenshot_69

 

To work  with Drag Related Events, ALLOW DROP property must be set to true.

Example: –

A program for dragging the data from MS – word in to .net text box.

Open WFAP

Place a text box – – >  properties – – >  Allow drop = true Multiline = true scroll bars = both

Code for Text Box 1 Drag Enter Event

{

If  (e. data. Get data present  (Data Formats .Text)  == true)

 

{

String s = (String) . e. Data . Get Data (Data Formats. Text) ;

Text Box 1.Text = s;

}

else

Message Box. Show (“Not valid”);

Summary
Review Date
Reviewed Item
Events in C# .Net
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Events in C# .Net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.