Capture value of a TextBox and check that it is equal to another value in C #

2

I would like that when the user puts exactly something in a TextBox , like for example 1234 , that another window can be opened later, but only if he puts that in particular.

So, how can I check that what the user entered is 1234 ?

    
asked by user43825 21.05.2017 в 18:30
source

2 answers

1

You must add the KeyPress Event to your TextBox to validate each time a key is pressed:

 private void textBox1_KeyDown(object sender, KeyEventArgs e) 
 { 
    //validamos el texto que tiene el textBox
    if(textBox1.text.equals("1234"))
    {
        MessageBox.Show("debo lanzar la segunda ventana");
    } 
 }

I hope it serves you Greetings.

    
answered by 21.05.2017 в 19:39
1

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

        
    answered by 25.04.2018 в 14:16