German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Basics of Events and Usage of Events in Visual C#

by Omer kamal July 21, 2006
This Tutorials covers Basics of Events in Visual C#- Reader level: Biginner.

We want to keep things simple. Now, We will come to the best part of Visual C# Called "Events".

 

What are events?

 

Events are self triggring Activities or Tasks in an application. To make it more easy we will take a very simple example:

 if you click a button it triggrs "onClick" Event of the Button. If "onClick" event was implemented it will do the given task

 

There are basically two ways to fire and generate events.


(1) Using Build-in EventHandler technique of Visual Environment.
(2) Creating Custom Delegate for Event triggring.

 

As we promised to keep things simple we will start with the simple method 1st . Here, we will discuss about (1). 

 

Events are heart beats of the Visual C# environment.  so , we will take an example and it will make things very clear.

 

Example;

 

Create a new Windows Application. Name it "VSharpEvents". Open "Form1" in design view if it is not opend by default. Drag a TextBox and Label to the Form. Right click on the TextBox and Click "Properties" It will open Properties Window if it was open already.

make sure that TextBox is seleted: Click "Events" Tab in the Properties window. It will Display all events of our TextBox named "TextBox1".

 

Now, scroll up and down and find "TextChange" Event. Double Click in the Box just right to it. It will automatically generate Event named "textBox1_TextChanged". Dont try to change the name because it is refered by some other code which is automatically generated in the "InitializeComponent()" Function. If you really intended to change the name then change the name in the both places.



this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

To

this.textBox1.TextChanged += new System.EventHandler(this.myName);

Where "myName" is your suggested name.


Now, we are inteded to do some thing intresting. Please replace your "Form1.cs" code with the Following.

using System;
using System.Windows.Forms;

namespace VSharpEvents {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
label1.Text="0";
    }
private void textBox1_TextChanged(object sender, EventArgs e) {
label1.Text=textBox1.Text.Length.ToString();
     }
   }
}

Run your application. Type any thing in the TextBox you will find it always gives you the Length of the string you type in.

 

You can play arround with different Events like that.

Visitors/Readers Comments
(for questions please use The Forum)



harsh
Nice !

01/11/2007 00:15:34 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)