locked
If / Else Code Question

    Question

  • Hello, I have in my program, two textBoxes, which are populated with text from two different .txt files.  I have it setup so that if the text in the two Textboxes do not match, that a Picturebox and a Label show.  For whatever reason, though the Textboxes do not match, it is not showing the Picturebox or Label.  I was hoping someone could point me in the right direction, as I cannot understand why this would be.  Thank you.

    Here is my code:

    private void Form2_Load(object sender, EventArgs e)
            {
                textBox3.Text = File.ReadAllText(Environment.CurrentDirectory + @"\Files\PVersion.txt");

                try
                {
                    textBox4.Text = File.ReadAllText(@"S:\SHDev\Updates\BugSubmiter\PVersion.txt");
                }

                catch
                {
                }

    private void textBox4_TextChanged(object sender, EventArgs e)
            {
                if (textBox3.Text.ToString() == textBox4.Text)
                {
                    pictureBox6.Hide();
                    label4.Hide();
                }

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

    Thank you.
    Wednesday, May 16, 2007 2:16 PM

Answers

  • Your code as posted works for me.  There must be something else going on.

     

     

    Wednesday, May 16, 2007 5:04 PM

All replies

  • Here is a guess:

     

    The attempt to load textBox4 from the file fails for some reason (file not found for example).  Since the textBox is not changed (because of the try/catch structure), the textBox4_TextChanged event never fires.  You are left with two textBoxes with different values and the label/pictureboxes remain in the default state (hidden, I presume).

     

     

     

    Wednesday, May 16, 2007 2:44 PM
  • Thank you very much for your response.  If the text is changing, why wouldn't it fire the textchanged event?  This does work sometimes, but it kinda has to work all of the time...lol.  I tried putting a catch statement in and the Textbox appears to load without any problem, as the catch was never fired.  I can run the application and see both of the Textboxes and one will have the one value, and the other will have the other value, yet it doesn't show the Picturebox or Label.  They are hidden by default.  I'm really confused, as (In my head at least), I feel like this should work...lol

    Thank you.
    Wednesday, May 16, 2007 2:52 PM
  • It does work if I manually change the text in the Textboxes.  I don't know if that helps any.  I just don't get it.  This "should" work, shouldn't it?  lol

    Thank you.
    Wednesday, May 16, 2007 2:55 PM
  • Your code as posted works for me.  There must be something else going on.

     

     

    Wednesday, May 16, 2007 5:04 PM
  • For one thing, you are swallowing any exceptions that may be occurring in the Load event.  Remove the try catch or at least display the exception so you will know if one is occurring.

     

    Chris

    Wednesday, May 16, 2007 5:59 PM