I have two textbox that corresponds to the my two combobox.

if (purposecb.Text == "-Select One-")
            {
                MessageBox.Show("Please select the purpose of the visitor's visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                purposecb.Focus();
            }
            else if (placevisitcb.Text == "-Select One-")
            {
                MessageBox.Show("Please select the place where the visitor would visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                placevisitcb.Focus();
            }
            else if (purposecb.Text == "-Others Specify-")
            {
                if (specifypurposetb.Text.Length == 0)
                {
                    MessageBox.Show("Please specify the purpose of the visitor's visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    specifypurposetb.Focus();
                }
            }
            else if (placevisitcb.Text == "-Others Specify-")
            {
                if (specifyplacetb.Text.Length == 0)
                {
                    MessageBox.Show("Please specify the place where the visitor would visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    specifyplacetb.Focus();
                }
            }

the conditions stops on this line

if (specifypurposetb.Text.Length == 0)
                {
                    MessageBox.Show("Please specify the purpose of the visitor's visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    specifypurposetb.Focus();
                }

Once the condition above becomes false, the conditions after it doesn't work. As if it's the stopping line of the condition.

The code I provided are only part of a larger condition.

Recommended Answers

All 8 Replies

I guess when you say "the conditions stops" mean you are not entering the if statement. This can only mean one thing: specifypurposetb.Text.Length is not equal to zero.

Yes you are correct. specifypurposetb.Text.Length is not equal to zero if the user inputs something, but it doesn't mean that the condition should stop. There are still conditions that comes after

else if (placevisitcb.Text == "-Others Specify-")
            {
                if (specifyplacetb.Text.Length == 0)
                {
                    MessageBox.Show("Please specify the place where the visitor would visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    specifyplacetb.Focus();
                }
            }

Unfortunately, it doesn't read the above condition once specifypurposetb.Text.Length is not equal to zero.

Don't know exactly what your intentions are, but if you don't execute the if statement on line 3 the next statement that will be executed will be after your if else if statements.

What do you mean? I just want to run this code

else if (placevisitcb.Text == "-Others Specify-")
            {
                if (specifyplacetb.Text.Length == 0)
                {
                    MessageBox.Show("Please specify the place where the visitor would visit.", "message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    specifyplacetb.Focus();
                }
            }

But it doesn't.

I have this

if
{
}
else if
{
}
else if
{
}
else
{
}

It works but if I use an if statement inside the if(nested), the following else if conditions and else doesn't work. I really need some help

That is exactly what I meant in my previous answer.
Say that the else if on line 4 becomes true, lines 5 and 6 get executed. After that the program continues after line 12 (line 13)
The code is somewhat the same with a select case statement.
If you don't want that behavior use only if instead of if else if.

commented: thanks for pointing things out +1

I'm using the if else if conditions for my validation so I need to use if else if to validate all the conditions and use the method inside else, if all return false.

This is the confusing part

if (combobox.text != "-select one-")
{
if (textbox.text.length == 0)
{
//do something
}
}
else if (textbox2.text.length == 0)
{
//do something
}
else
{
//do something
}

Line 1 returns true if the combobox text is not "select one".
I used this condition to make textbox mandatory if an item is selected in combobox.

Line 3 returns false if the textbox has a length not equal to 0

I think this causes a conflict because line returns true.

I understand it now. I should use the && operator so if the part of the conditions returns false, it will return false regardless other parts return true.

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.