I have a form with 4 different textboxes. Each box needs to have a certain number of characters entered in so I'm writing if/else statements. For example:

if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))

     this.DialogResult = DialogResult.None;
     MessageBox.Show("Error message goes here");

else

     this.DialogResult = DialogResult.OK;

It goes on like that for all 4 textfields with one problem -- if the last field is valid we go back to the main application form and the user doesn't get a chance to fix his mistakes. I could write a fairly long if statement, so that if any one of those values is not the length I want it to be an all encompassing message is returned to the user, but that isn't very practical or elegant. At the end of all 4 if statements I want some piece of code that says, 'if any one of these if statements returned DialogResult.None then don't exit, even if the last if statement returned DialogResult.OK'

Any tips?

I would suggest you use an OK/Cancel button system, as while an item could even be considered valid, doesnt mean the user doesnt want to change it - it also means you only have 2 ways of leaving your form, the buttons or they hit close.

Hmm, yes I am using an OK/Cancel system. The code above is executed after data is entered into the fields and the user clicks OK.

if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))

This is always true!

OK, I've fixed it so it's not always true. But my problem still stands, if the last if/else statement sets dialog result as OK the form exits although the first textbox.Length may be incorrect. Is there any way around this? And thanks for all the replies thus far.

Yes.
<sarcasm>dont set it to ok when its not :P</sarcasm>
The thing is thats exactly what you're doing

You're doing a number of tests on clicking an OK button. ALL of those tests must pass before you can move on, not just 1.

Either you have to make your if a big big if, or you do your logic so that if 1 of the tests fails you can tell. Easiest way to do that, is set the a boolean to false, and set it to true if any condition fails.. then at the end move on if its still false.

You normally don't set DialogResult.OK yourself. You get it after pressing an OK button or something.
Like so :
if(this.DialogResult == DialogResult.OK)
>>check textboxes, if not ok set DialogResult to None or Cancel
else
>>

hi gt2010
i think this may be useful to u. chk this way

and all bool var declare outside the procedure like public
this write it in ok button click

        if ((dlg1.nameText.Length > 5) || (dlg1.nameText.Length < 8))
        {
            this.DialogResult = DialogResult.None;
            MessageBox.Show("Error message goes here");
            firstbool = false; //if its  DialogResult.None;
        }
        else
        {
            firstbool = true; //if its DialogResult.ok;
            //ur wish ? u want to do
        }

        if ((dlg2.nameText.Length > 5) || (dlg2.nameText.Length < 8))
        {
             this.DialogResult = DialogResult.None;
             secondbool = false; //if its  DialogResult.None;
        }
        else
        {
            secondbool = true; //if its  DialogResult.ok;
            //ur wish ? u want to do
        }

        if((dlg3.nameText.Length > 5) || (dlg3.nameText.Length < 8))
        {
             this.DialogResult = DialogResult.None;
             thirdbool = false; //if its  DialogResult.None;
        }
        else
        {
            thirdbool = true; //if its  DialogResult.ok;
            //ur wish ? u want to do
        }

        if ((dlg4.nameText.Length > 5) || (dlg4.nameText.Length < 8))
        {
            this.DialogResult = DialogResult.None;
            fourthbool = false; //if its  DialogResult.None;
        }
        else
        {
            fourthbool = true; //if its  DialogResult.ok;
            //ur wish ? u want to do
        }

        if (firstbool == true & secondbool == true & thirdbool == true & fourthbool ==true)
        {
            //specify where u want to go 
        }
        else
        {
            //stay in that form
        }
    }

if any problem let me know

Renukavani, that code could be made so much simpler - which is why i didnt post code, because people need to understand not just copy

Besides what LizR said, your else clauses never get executed for reasons I mentioned earlier...

As ddanbe said before, first check if user click Ok, Start checking the input. If one if put fail, mark it down (etc changing its textcolor). Change the DialogResult back to None. Should be :

if(this.DialogResult == DialogResult.OK)
{
	string error;
	if ((dlg1.nameText.Length < 5) || (dlg1.nameText.Length > 8))
	{
		this.DialogResult = DialogResult.None;
		error = "1, ";
	}
	if (this.DialogResult == DialogResult.Ok)
	{
		//specify where u want to go
	}
	else
	{
		//stay in that form
	}
}

Set the dialog result to someting *ONLY* if all conditions are met.

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.