I have two textbox and I want to have two different messagebox validation for each.

It goes like this

if(text1.Text.Length == 0)
{
messagebox.Show("error 1");
}
else
{
OK
}

Where would I put the textbox 2 for a different messagebox? Else if will not work. Need help

I'm not sure what you are asking, but maybe this will help:

if (text1.Text.Length == 0) {
    messagebox.Show("error 1");
} else {
    if (text2.Text.Length == 0) {
        messagebox.Show("error 2");
    } else {
        // both are ok, do whatever here
    }
}

hi, you can try this way:

string a = textBox1.Text;
            string b = textBox2.Text;
            if (a != String.Empty && b != String.Empty)
            { 
                //both ok!
            }
            else if (a == String.Empty)
                MessageBox.Show("Error 1");
            else
                MessageBox.Show("Error 2");

I'm sorry, else if works. But how to change the back color effectively using else if statement? It will be quite too long if I say,

else if (text.Length == 0)
{
/message
}

else if (text.Length != 0)
{
//message
}

Is it just fine?

You mean the backColor of textBoxes?
If so, depending on which? On emtpy textBox or not empty?

On button click event:

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != String.Empty)
                textBox1.BackColor = Color.Green;
            else
                textBox1.BackColor = Color.Red;
        }

I have three textboxes so "else if" necessary, right?

Yes, it is. If you want to chack one ot ther other state.

Then it would be like this?

if (text1.Length == 0)
{
/message
}

else if (text2.Length == 0)
{
//message
}
else if (text2.Length != 0)
{
/message
}

else
{
//message
}

Use custom validation tools textbox validation message like

msgbox.text=" validation message" ;

this code use in between custom validation start and end tags.

Becuase you have to check all 4 textBoxes, its better to use only if statement through all three of them:

if (text1.Length == 0)
{
   //message if is empty
}
else
{
    //message if NOT empty!
}

if (text2.Length == 0)
{
    //message if its empty
}
else
{
    //message if NOT empty!
}
if (text2.Length != 0)
{
    //message if its empty
}
else
{
    //message if NOT empty!
}

Is this better than doing it in validating event?

The above code is not suitable because if it's not empty then it will be added to database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.