954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Secondary form is closing prematurely

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?

GT2010
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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.

GT2010
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))


This is always true!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

GT2010
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Yes.
dont set it to ok when its not :P
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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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
>>

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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
	}
}
hieuuk
Light Poster
44 posts since Nov 2008
Reputation Points: 11
Solved Threads: 4
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You