locked
Check to see if two textBoxes are NOT equal? RRS feed

  • Question

  • I would like to show a label and a picture if two textBoxes are NOT equal.  I am having some difficulty though.  I thought the best way to do this was with an if / else statement, but I guess not, as it doesn't work.

    Here is my code:

    if (textBox3.Text.ToString() == textBox4.Text)
                {
                   
                }

                else
                {
                    pictureBox6.Show();
                    label4.Show();
                }

    For whatever reason, the picture and label, which are hidden by default show no matter what.  I'm open to any ideas anyone might have.  Any help and information is greatly appreciated.

    Thanks,
    Thursday, May 10, 2007 7:27 PM

Answers

  • For starters, you don't need the .ToString() because textBox3.Text is already a string.  But that is not the problem.

     

    I don't have the full picture here because I don't know where this code snippet is in your program so it is possible that at some point the boxes are not equal and the objects are made visible then when you change the textboxes to be the same, there is nothing to hide them.

     

    Try putting:

     

    pictureBox6.Hide();

    label4.Hide();

     

    between the (now) empty { } after the if statement.

     

     

    Thursday, May 10, 2007 7:40 PM
  • well, you could use a different event, for example leave, validated, validating instead of text_changed. Text changed will fire every single keystroke (because it is changing the text value) and when you set the value of text in the program. You probably will want to handle loading values differently than user input.
    Thursday, May 10, 2007 10:47 PM
  • Use the TextChanged event:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          textBox1.TextChanged += CheckTextMatch;
          textBox2.TextChanged += CheckTextMatch;
        }
        private void CheckTextMatch(object sender, EventArgs e) {
          pictureBox1.Visible = textBox1.Text == textBox2.Text;
        }
      }

    Friday, May 11, 2007 7:23 AM
    Moderator

All replies

  • For starters, you don't need the .ToString() because textBox3.Text is already a string.  But that is not the problem.

     

    I don't have the full picture here because I don't know where this code snippet is in your program so it is possible that at some point the boxes are not equal and the objects are made visible then when you change the textboxes to be the same, there is nothing to hide them.

     

    Try putting:

     

    pictureBox6.Hide();

    label4.Hide();

     

    between the (now) empty { } after the if statement.

     

     

    Thursday, May 10, 2007 7:40 PM
  • Thanks for your response, that's what it appears to be.  Let me explain a bit more.  Both textBoxes are filled from .txt files in the Form1_Load.  The if / else is in the textBox3_TextChanged.  When I run the application and retype the text in the textBoxes, the image and label hide (I added the Hide code above as you suggested).  I'm not sure how to do this if it's literally miliseconds that the value is not equal.  I'm open to any suggestions or ideas you might have.  Any help and information is greatly appreciated.

    Thanks Again,

    Thursday, May 10, 2007 7:47 PM
  • well, you could use a different event, for example leave, validated, validating instead of text_changed. Text changed will fire every single keystroke (because it is changing the text value) and when you set the value of text in the program. You probably will want to handle loading values differently than user input.
    Thursday, May 10, 2007 10:47 PM
  • Use the TextChanged event:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          textBox1.TextChanged += CheckTextMatch;
          textBox2.TextChanged += CheckTextMatch;
        }
        private void CheckTextMatch(object sender, EventArgs e) {
          pictureBox1.Visible = textBox1.Text == textBox2.Text;
        }
      }

    Friday, May 11, 2007 7:23 AM
    Moderator
  • Thank you all very much for your help.  I really appreciate it and everything is working now.  I could not understand why it would say that they didn't match when they did...lol  Apparently my eyes weren't fast enough to see the time that they were in fact not equal...

    Thanks Again,
    Friday, May 11, 2007 1:06 PM