469,578 Members | 1,409 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

How to Keep Dialog Open When Exception Occurs

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,

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com
Nov 16 '05 #1
5 3228
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

Nov 16 '05 #2
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

"Mohamoss" <mo************@egdsc.microsoft.com> wrote in message
news:db**************@cpmsftngxa10.phx.gbl...
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

Nov 16 '05 #3
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

Nov 16 '05 #4
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
"Mohamoss" <mo************@egdsc.microsoft.com> wrote in message
news:QR**************@cpmsftngxa10.phx.gbl...
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

Nov 16 '05 #5
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

"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:uB**************@TK2MSFTNGP11.phx.gbl...
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
"Mohamoss" <mo************@egdsc.microsoft.com> wrote in message
news:QR**************@cpmsftngxa10.phx.gbl...
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


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mad Hot dog | last post: by
2 posts views Thread by Ron Rohrssen | last post: by
3 posts views Thread by Andrew | last post: by
6 posts views Thread by Steve Long | last post: by
7 posts views Thread by mail747097 | last post: by
7 posts views Thread by Peter | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.