C
Christopher W. Douglas
I am working on a project in VB.NET using Visual Studio.NET 2003. I use
dialog windows to get user information, and throw exceptions when there are
user errors, such as leaving a field blank or entering invalid information.
My exception handler reads the error message and handles it accordingly
based on the message.
My problem is, I am trying to find a way to avoid closing the dialog window
when it is a minor error. I can do this by setting DialogResult to None,
but the problem is in determining which form threw the exception. Using
Exception.TargetSite.DeclaringType I can get the Type of the window, and
thus get the Name. For example, if the class of the dialog window is
"frmFilter", the DeclaringType's Name is "frmFilter". I built a case
statement based on the name, but I would rather have a more dynamic answer.
What I really want to do is to somehow convert the Type object into the
actual Object. I know you can go the other direction with GetType, but I
can't seem to find in help how to go the other way.
I also saw the SetValue property, but as far as I can tell I need to have
the actual object (the dialog form) in order to make SetValue work. If I
HAD the actual object available, I could just set the property from the
object and be done with it.
Here is an example/pseudo-code:
I have a form class, frmFilter.
I have an object of frmFilter, called FilterForm.
There is an exception thrown from function cmdOK_Click in FilterForm.
In the exception handler, I determine that the exception does not warrant
closing the form, so I want to set FilterForm.DialogResult =
DialogResult.None.
I declare MyType as System.Reflection.Type
Exception.TargetSite gives me cmdOK_Click, which is the offending method.
Exception.TargetSite.DeclaringType gives me the Type "frmFilter", which I
assign to MyType
Now, I need to find a way to convert (?) MyType into the actual object,
FilterForm. Or find an easy way to set the property of MyType.DialogResult
to DialogResult.None. CType doesn't work, since MyType is a Type, which I
understand is similar to a Class, not the actual Object FilterForm.
I appreciate any help you guys can provide.
dialog windows to get user information, and throw exceptions when there are
user errors, such as leaving a field blank or entering invalid information.
My exception handler reads the error message and handles it accordingly
based on the message.
My problem is, I am trying to find a way to avoid closing the dialog window
when it is a minor error. I can do this by setting DialogResult to None,
but the problem is in determining which form threw the exception. Using
Exception.TargetSite.DeclaringType I can get the Type of the window, and
thus get the Name. For example, if the class of the dialog window is
"frmFilter", the DeclaringType's Name is "frmFilter". I built a case
statement based on the name, but I would rather have a more dynamic answer.
What I really want to do is to somehow convert the Type object into the
actual Object. I know you can go the other direction with GetType, but I
can't seem to find in help how to go the other way.
I also saw the SetValue property, but as far as I can tell I need to have
the actual object (the dialog form) in order to make SetValue work. If I
HAD the actual object available, I could just set the property from the
object and be done with it.
Here is an example/pseudo-code:
I have a form class, frmFilter.
I have an object of frmFilter, called FilterForm.
There is an exception thrown from function cmdOK_Click in FilterForm.
In the exception handler, I determine that the exception does not warrant
closing the form, so I want to set FilterForm.DialogResult =
DialogResult.None.
I declare MyType as System.Reflection.Type
Exception.TargetSite gives me cmdOK_Click, which is the offending method.
Exception.TargetSite.DeclaringType gives me the Type "frmFilter", which I
assign to MyType
Now, I need to find a way to convert (?) MyType into the actual object,
FilterForm. Or find an easy way to set the property of MyType.DialogResult
to DialogResult.None. CType doesn't work, since MyType is a Type, which I
understand is similar to a Class, not the actual Object FilterForm.
I appreciate any help you guys can provide.