Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 84.1k

Best event for entering and checking integer in windows forms

Jan 30 2022 8:46 AM

Hi
What's the best equivalent op Console.ReadLine() with Int.TryParse check in windows forms? 
I'm using a texbox for an integer which must be checked. I tried with using a button with the button1_Click event or no button but using the textBox1_TextChanged event
My opnion is that using the textBox1_TextChanged event has the advantage not to have to click on a button, but has the drawback that each digit of the number is checked. So i think using a button with the button1_Click would be better. Or is there still another better way?
Thanks. 
 
int g1 = 0;
 private void button1_Click(object sender, EventArgs e)
        {
            int som = 0; 
            if (int.TryParse(textBox1.Text, out g1) == false)
            {
                textBox1.Text = ""; 
                ActiveControl = textBox1; 
                return;
            }
            label3.Text = g1.ToString();
        }
----------------------------
int g1 = 0;
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            int som = 0;
            if (int.TryParse(textBox1.Text, out g1) == false)
            {
                textBox1.Text = ""; 
                ActiveControl = textBox1; 
                return;
            }
            label3.Text = g1.ToString();
        }
 


Answers (2)