Stop Dialog from Closing

  • Thread starter Thread starter Marcus Kwok
  • Start date Start date
M

Marcus Kwok

I have a form that I display using ShowDialog() with an OK button and a
Cancel button, and I have set the buttons' DialogResult properties using
the designer. When the user clicks on OK, I call a method to check the
values they typed in some TextBoxes, and if they entered invalid data, I
display a MessageBox telling them what they did wrong. However, after
the MessageBox closes, the dialog also closes so they are not able to go
back and change their data.

I can not check the values until after they click on OK because I have
multiple TextBoxes that they must fill in (though they can be filled in
any order), and the values in each box depend on what has been entered
into the other boxes, so I do not want to prematurely tell them their
data is invalid if they haven't finished entering everything yet.

How do I stop the dialog window from closing? I suppose I could remove
the DialogResult property from the button and set it
explicitly/programmatically if all the checks are successful, but I
would rather keep the property set on the button if possible.

I am using Managed C++ (Visual Studio .NET 2003).


A simplified version of my code:

System::Void button_ok_Click(System::Object * sender, System::EventArgs * e)
{
if (!valid_data()) {
MessageBox::Show(S"Error!");
// What to put here to prevent window from closing?
}
}
 
Marcus Kwok wrote:
....
How do I stop the dialog window from closing? ....
A simplified version of my code:

System::Void button_ok_Click(System::Object * sender, System::EventArgs * e)
{
if (!valid_data()) {
MessageBox::Show(S"Error!");
// What to put here to prevent window from closing?
}
}

Put there a flag which would indicate whether the box can be closed
or not. Then in OnClosing event (you have to subscribe to it, of course)
do something like:

protected override void OnClosing(CancelEventArgs e)
{
if(canClose)
base.OnClosing (e);
else
e.Cancel = true;
}

Sorry, it is in C#, but I hope it can be translated easily.
 
Hi Marcus,

You might try posting to "microsoft.public.dotnet.languages.vc". I
think the experts monitoring that group would be better able to help
you. I don't normally see many VC++ questions coming here.

I'm not familiar with VC++, but the logic I would use would be :

1. Set a boolean variable in the Ok button Click method. For eg.
Invalid = true;
2. Check the value in the Form Closing event, if Invalid is True, then
I would set the Cancel property of the EventArgs (e) variable to
prevent closing the form.

I hope you get the idea, and it *is* implement-able using this logic in
VC++.

Regards,

Cerebrus.
 
Marcus Kwok said:
I have a form that I display using ShowDialog() with an OK button and a
Cancel button, and I have set the buttons' DialogResult properties using
the designer. When the user clicks on OK, I call a method to check the
values they typed in some TextBoxes, and if they entered invalid data, I
display a MessageBox telling them what they did wrong. However, after
the MessageBox closes, the dialog also closes so they are not able to go
back and change their data.

Instead of assigning 'DialogResult' to the controls, assign it to the form
at runtime:

\\\
if (Success)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK
this.Close()
}
///
 
Immediately after you determine that there are errors, set
this.DialogResult = DialogResult.None,

That should keep the dialog running.

Regards,

John Parrish
 
Marcus Kwok said:
I have a form that I display using ShowDialog() with an OK button and a
Cancel button, and I have set the buttons' DialogResult properties using
the designer. When the user clicks on OK, I call a method to check the
values they typed in some TextBoxes, and if they entered invalid data, I
display a MessageBox telling them what they did wrong. However, after
the MessageBox closes, the dialog also closes so they are not able to go
back and change their data.

Thank you to everybody who responded. I decided to go the route of
adding a boolean variable to the class, which I check in the form's
Closing event, and it works great!
 
Back
Top