943,522 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1243
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 8th, 2008
0

Secondary form is closing prematurely

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))
  2.  
  3. this.DialogResult = DialogResult.None;
  4. MessageBox.Show("Error message goes here");
  5.  
  6. else
  7.  
  8. 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GT2010 is offline Offline
5 posts
since Nov 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GT2010 is offline Offline
5 posts
since Nov 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

Quote ...
if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))
This is always true!
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GT2010 is offline Offline
5 posts
since Nov 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

Yes.
<sarcasm>dont set it to ok when its not </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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Dec 8th, 2008
0

Re: Secondary form is closing prematurely

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
>>
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Dec 9th, 2008
0

Re: Secondary form is closing prematurely

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
Quote ...
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
Reputation Points: 10
Solved Threads: 23
Junior Poster
Renukavani is offline Offline
123 posts
since Jul 2008
Dec 9th, 2008
0

Re: Secondary form is closing prematurely

Renukavani, that code could be made so much simpler - which is why i didnt post code, because people need to understand not just copy
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Dec 9th, 2008
0

Re: Secondary form is closing prematurely

Besides what LizR said, your else clauses never get executed for reasons I mentioned earlier...
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Backgroundworker
Next Thread in C# Forum Timeline: Windows forms





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC