Hi
I need to set my main form as the owner of an error dialog box shown when
there's an exception in a background thread. This is to make sure that the
error form is closed down if the user pulls their security card while the
error window is being shown.
In visual studio 2003, my main form set a static "owner" variable within my
frmWarning class definition and if the error form was needed (and null owner
passed) then this static variable was used. It's not pretty but it was the
only way I could think of for making sure that error dialogs could be closed
by the main form.
This worked fine when there was an error thrown somewhere in my thread loop
in vis studio 2003.
I've just transferred it all across to 2005 and now my error form won't
appear. If I remove the line that looks for the static owner member variable
then it shows ok. But that's no good because then pulling the card doesnt
shut the form down.
I guess I'm doing something naughty, but is there a way of changing this
following function so that I can use the frmWarning.owner properly plus if
the main form (created by main process) then tries to close the form (that
has been created by the thread) later then that will work fine too.
thank you (hope that was all understandable)
Claire
public static DialogResult ShowError(System.Windows.Forms.IWin32Window
Owner, string ErrorMessage)
{
string fnName = "ShowError";
DialogResult result = DialogResult.OK;
frmWarning frm = new frmWarning();
try
{
frm.lblText.Text = ErrorMessage;
// if (Owner == null) Owner = frmWarning.owner;
result = frm.ShowDialog(Owner);
}
catch (Exception e)
{
Log.Exception(typeof(frmWarning), fnName, e);
}
finally
{
frm.Dispose();
}
return result;
}
/// <summary>
/// Destroy child forms which are owned by the
/// root form. Closing the root alone won't destroy the
/// child forms automatically. All children have to be
/// created by passing their owning form in ShowDialog(Form owner)
/// parameter
/// An iterative function called from main form.
/// </summary>
/// <param name="Root">The base form</param>
static public void DestroyOwned(Form Root)
{
for ( int d=Root.OwnedForms.GetLength(0) - 1; d >= 0; d--)
{
DestroyOwned(Root.OwnedForms[d]);
Root.OwnedForms[d].Close();
}
}// function