| | |
Secondary form is closing prematurely
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
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?
C# Syntax (Toggle Plain Text)
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?
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
<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.
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
>>
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jul 2008
Posts: 66
Reputation:
Solved Threads: 6
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 any problem let me know
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
}
}
signOut
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
![]() |
Other Threads in the C# Forum
- Previous Thread: Backgroundworker
- Next Thread: Windows forms
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming properties radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tables tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






