How to Keep Dialog Open When Exception Occurs

J

John Bowman

Hi,

I need some dialog handling help, I must be missing something obvious here
but I can't figure out the solution to the following problem...

Simple Win Forms app that has button on the form. The click event handler
for the button creates another dialog (as a form object) and calls it's
ShowDialog method to dsiplay it modally. The secondary dialog then appears
that has OK and Cancel buttons and the user can interact as expected. Now
when the user chooses OK some settings need to be recorded to disk, so I
trap the possible exceptions and dutifully show a MessageBox when needed
informing them what went wrong. The problem is that once the exception has
been handled and the error MessageBox dimissed, the secondary dialog also
disappears. I want it to stay open until they can either store everything
correctly or choose Cancel. Basically, I need to abort the OK click event
handler prematurely so the secondary dialog stays open. How do I do that?

TIA,
 
M

Mohamoss

hi Tia
What you have here is a message box. For MessageBox clicking
ok or cancel trigger the close event ( so it will be closed once you click
ok or cancel no matter if form one has error or not ) . What you can do is
that in your event handler "that exist in form one "you reopen the message
box once you done handling the error. However, this is not a neat solution.
A good solution would be to implement an error provider object
on form one, and enable validation for all the controls where the user can
enter an incorrect date. This way you grantee that the use cannot leave a
control on form one unless he/ she has entered a valid input of an
acceptable range.
hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
J

John Bowman

Mohamed,

Thanks for the response. I know the MessageBox error dialog will go away
because it is called in the "OK" button's click event and is modal. However,
it's the second form (the one containing the "OK" button) that is a dialog
that I want to leave open until no errors writing to disk occur. It really
has nothing to do w/ what the user enters in the form, as the user is never
actually allowed to "enter" anything, just selections. These choices need to
be saved to disk someplace. If anything goes wrong during the write (aka an
exception), then leave the dialog open after the error MessageBox is
dismissed.

John
 
M

Mohamoss

hi
would you post a Snap of the code where the problem happen i.e. the
exception is raised . ......................
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
J

John Bowman

Mohamed,

Thanks, here's the example code.... It's incredibly simple.

Form1 has a button, it's click event has the following code:

CDlg MyDlg = new CDlg();

MyDlg.ShowDialog();


CDlg is a simple windows forms dialog containing 2 buttons (OK and Cancel),
but simulates an options dialog where several settings need to be choosen by
the user. When the user chooses the "OK" button on this dialog, some
error(s) (aka exceptions) might occur while the user's settings are being
saved to disk. The CDlg form's OK button's click event has code like the
following...

try

{

//Simulate a disk error of some kind while saving the settings the user has
choosen on this dialog

throw new FileNotFoundException("Could not save to file XYZ....");

}

catch(FileNotFoundException eFileNotFound)

{

MessageBox.Show(eFileNotFound.Message);

}


Now, when the user responds to the MessageBox by pressing it's OK button, it
goes away, this is fine. But so does MyDlg, which is NOT what I want. I
want MyDlg to stay right where it is. How do I do this?

John
 
J

John Bowman

Hi,

I found the "trick". I needed to set the DialogResult for the 2nd form to
None in it's Ok button's event handler when an error occurs after the
MessageBox. This causes the ShowDialog method to not exit and the dialog
stays's open.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top