Secondary form is closing prematurely

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 5
Reputation: GT2010 is an unknown quantity at this point 
Solved Threads: 0
GT2010 GT2010 is offline Offline
Newbie Poster

Secondary form is closing prematurely

 
0
  #1
Dec 8th, 2008
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #2
Dec 8th, 2008
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: GT2010 is an unknown quantity at this point 
Solved Threads: 0
GT2010 GT2010 is offline Offline
Newbie Poster

Re: Secondary form is closing prematurely

 
0
  #3
Dec 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,979
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #4
Dec 8th, 2008
if ((dlg.nameText.Length > 5) || (dlg.nameText.Length < 8))
This is always true!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: GT2010 is an unknown quantity at this point 
Solved Threads: 0
GT2010 GT2010 is offline Offline
Newbie Poster

Re: Secondary form is closing prematurely

 
0
  #5
Dec 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #6
Dec 8th, 2008
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,979
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #7
Dec 8th, 2008
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
>>
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 66
Reputation: Renukavani is an unknown quantity at this point 
Solved Threads: 6
Renukavani Renukavani is offline Offline
Junior Poster in Training

Re: Secondary form is closing prematurely

 
0
  #8
Dec 9th, 2008
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
signOut
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #9
Dec 9th, 2008
Renukavani, that code could be made so much simpler - which is why i didnt post code, because people need to understand not just copy
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,979
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Secondary form is closing prematurely

 
0
  #10
Dec 9th, 2008
Besides what LizR said, your else clauses never get executed for reasons I mentioned earlier...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC