To capture the data entered by the user in the textbox (TextBox) there are mainly 4 events. 3 of the four occur when the user is pressing the keys to enter information to the TextBox, these are: KeyDown
, KeyPress
and KeyUp
. The fourth event occurs when the Text
property of the TextBox changes, that is, when the user finishes entering the information in the TextBox and it loses focus.
If you use the events KeyDown
, KeyPress
and KeyUp
to verify the user's input, you should bear in mind that these events occur in an order, which is as follows:
KeyDown
KeyPress
KeyUp
And when implementing them in your code, they would be built like this:
KeyDown
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(textBox1.Text.Equals("1234"))
{
MessageBox.Show("debo lanzar la segunda ventana");
}
}
KeyPress
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(textBox1.Text.Equals("1234"))
{
MessageBox.Show("debo lanzar la segunda ventana");
}
}
KeyUp
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if(textBox1.Text.Equals("1234"))
{
MessageBox.Show("debo lanzar la segunda ventana");
}
}
If you want to verify the ones written by the user in the TextBox, the code of the function launches the event would be the following:
TextChanged
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Equals("1234"))
{
MessageBox.Show("debo lanzar la segunda ventana");
}
}
Remember that in order to execute these functions you must relate the function to the TextBox event. Now it only depends on when or how often you need to validate the user's input.
An event, according to this page of the MSDN site of Microsoft is:
Events provide a means for a class or object to inform other classes or objects when something relevant happens. The class that sends (or produces) the event is called an editor and the classes that receive (or control) the event are called subscribers.
More content on the Microsoft MSDN site:
In these pages you will find more information about the events TextBox.KeyDown , TextBox.KeyPress , TextBox.KeyUp and TextBox.TextChanged