none
Function Problem - Check empty textboxes RRS feed

  • Question

  • Hello, i've a problem with this function. Error = errorProvider

      public bool IsEmpty { get; set; }
    
    public void CheckEmpty(List<TextBox> txt)
            {
                foreach (TextBox t in txt)
                {
    
                    if (t.Text == string.Empty)
                    {
                        IsEmpty = true;
                        error.SetError(t, "Error");
                     
                    }
    
                    else
                    {
                        error.Clear();
    
                    }
    
                }
    
                if (IsEmpty == true)
                {
                    MessageBox.Show("Eror!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                List<TextBox> txt = new List<TextBox>() { textBox1, textBox2 };
                CheckIsEmptyPart(txt);
    
            }

    Now, when I write some in textBox1 and texBox2 - is ok - no error. When textbox1 and textbox2 is empty - is ok, shows up Error Text, but now when I write some in textboxes - Icons error disappear and shows up again Error Text,although textboxes doesn't empty. How can I change the function that was ok?

    Thanks




    • Edited by cash115 Monday, March 3, 2014 1:15 PM
    Monday, March 3, 2014 1:13 PM

Answers

  • There's nothing in your code that you posted to turn IsEmpty back to false. So once it's set to true it never goes back to false.

    Try:

      private void button2_Click(object sender, EventArgs e)
            {
                List<TextBox> txt = new List<TextBox>() { textBox1, textBox2 };
                CheckIsEmptyPart(txt);
                IsEmpty = false;
    
            }

    Monday, March 3, 2014 2:29 PM

All replies

  • There's nothing in your code that you posted to turn IsEmpty back to false. So once it's set to true it never goes back to false.

    Try:

      private void button2_Click(object sender, EventArgs e)
            {
                List<TextBox> txt = new List<TextBox>() { textBox1, textBox2 };
                CheckIsEmptyPart(txt);
                IsEmpty = false;
    
            }

    Monday, March 3, 2014 2:29 PM
  • Oo thanks! It's works :)
    Monday, March 3, 2014 4:53 PM