0

I'm beginner studying for c#. If I have 2 textbox, can I validate 2 textbox in 1 if statement?

For example:

{
    string emptytextbox
    if (textBox1.Text == "" || textBox2.Text == "" || textBox3.text == "")
    {
        //if textbox2 is empty, then messagebox will show
        emptytextbox = textBox2.text;
        mbox(emptytextbox + " must be filled");

        //messagebox will show "textBox2 must be filled"
        //but if textbox1&2 has ben filled, but textbox3 empty,then messagebox will show "textbox3 must be filled"
    }
}

Can I do this?

2
  • It depends on your requirement. If you want to performance some action when both text boxes are empty then you can have one if-statement, otherwise you need multiple if-condition either nested or separate. Commented Jun 14, 2014 at 17:03
  • There is better way to achieve this functionality. Are you working on windows form application. Commented Jun 14, 2014 at 17:06

3 Answers 3

1

One condition return a boolean value, there is no way to know witch one is the empty textbox. When you write an or expression the condition evaluate true if any of the components evaluate true. In this case when you go inside the condition the fact is that at least one of the conditions is true (by condition I mean textBox1.Text == "").

On this case the best way to perform the validation must be something like this:

void VerificationFunction()
    {
        CheckTextBox(textbox1);
        CheckTextBox(textbox2);
        CheckTextBox(textbox3);
    }

void CheckTextBox(TextBox tb)
    {
        if (string.IsNullOrEmpty(tb.Text))
        {
            MessageBox.Show(tb.Name + "must be filled");
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

can I validate 2 textbox in 1 "if" statement?

NO. Because you have to check every textBox and inform user about specific textBox which is left empty.

For all three textbox you can do something like that:

if (textBox1.Text == "")       
     MessageBox.Show("textBox1 must be filled");
if (textBox2.Text == "")       
     MessageBox.Show("textBox2 must be filled");
if (textBox3.Text == "")       
     MessageBox.Show("textBox3 must be filled");

It is good practice to have one method for the purpose.


Here is method which can be used to check each TextBox on the form. If there is any empty TextBox it will show message to user that textbox must be filled. Good practice is to inform user once.

private void ValidateTextBoxes()
    {
        foreach (Control c in this.Controls)
        {
            if (c is TextBox)
            {
                if (string.IsNullOrEmpty(c.Text))
                {
                    MessageBox.Show(c.Name + " must be filled");
                    break;
                }

            }
        }
    }

Comments

0

Best way is to do it separately

if (String.IsNullOrEmpty(textBox1.Text))
{
     textBox1.BorderBrush = System.Windows.Media.Brushes.Red;
}
if (String.IsNullOrEmpty(textBox2.Text))
{
     textBox2.BorderBrush = System.Windows.Media.Brushes.Red;
}

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.