|
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.
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show(textBox1.Text);
}
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
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 = "";
}
}
}
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 t
|