.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
abhays
Clintonzz
arronlee
mattyclown
cathyhill345

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Working with the TextBox Events!

Posted By:Hefin Dsouza       Posted Date: April 29, 2009    Points: 25    Category: C#    URL: http://hefindsouza.blog.com  
 

Working With TextBox Events in a Simple Way!

Introduction: The TextBox is a very common control that is available in the .Net ToolBox. In this article I will be covering how to work with some of the popular events of the TextBox class.

What is a TextBox?
The TextBox is a Control which is used for taking input from the user. It is popularly known as a Text Entry Control.

Now I will deal with some of the events in the TextBox Class.
TextChanged :-The event is fired whenever the value of the Text property of the TextBox control changes. It is also the Default event of the TextBox. We can use this event in scenarios where I want to make some event occur when the user is changing the text in the TextBox. In The Bellow Example I will show how the TextBox TextChanged event is used to identify the Text Being entered in the TextBox.
[code]
private void textBox1_TextChanged(object sender, EventArgs e)

{

MessageBox.Show(textBox1.Text);

}
[/code]

In this example whenever you type in the TextBox a message box will pop-up showing the Text in the TextBox.
The same can also be used to identify whether the Value of the TextBox is a Number or No. Example
[code]
private void textBox1_TextChanged(object sender, EventArgs e)

{

if (textBox1.Text != "")

{

Try

{

int num = Convert.ToInt32(textBox1.Text);

}

catch (Exception ex)

{

MessageBox.Show("Not A Number");

textBox1.Text = "";

}

}

}
[/code]
Leave:-This event occurs when the TextBox loses focus and the TextBox is no longer active. You can use this event when you want to fire an event after the user leaves the textbox.
In the below example I have demonstrated how to use the Leave event to check that the TextBox accepts a proper Age before leaving.
[code]
private void textBox1_Leave(object sender, EventArgs e)

{

if (Convert.ToInt32(textBox1.Text) < 18)

      {

            MessageBox.Show("Not A valid Age");

            textBox1.Focus();

      }

}
[/code]
Enter:- This event occurs when the TextBox gains focus. You can use this event if you want to notify the user before he starts entering data in the TextBox.

In the below example I have demonstrated how to use the TextBox's Enter event to Notify the user to enter a Age in the TextBox whenever the textbox gains focus.
[code]
private void textBox1_Enter(object sender, EventArgs e)

{

MessageBox.Show("Must Enter Age");

}
[/code]

Conclusion :These are some of the events which can be used with the TextBox. There are only few there are more events in the TextBox class. If you want to know more about any events add a response.

Regards Hefin Dsouza


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend