Well, I'm still wondering around in the "land of the lost exception"...
It seems that controls that are bound to a class and the class throws an
error in the SET method of the bound member, the exception cannot be
caught. I have wrapped my entire application in all the global
exception handlers I can find, and still to no avail. In fact all the
Try/Catches and Exception event handlers are worthless...
Put on your advanced thinking caps, and help me solve this one, please!
To recreate the problem:
*************** *************** *************** ************
1. In a new WindowsForm project, add Module1 as follows:
*************** *************** *************** ************
'----------------------------------------------
'This module is right out of the text book for
'handling unhandled exceptions:
'----------------------------------------------
Module Module1
Public Sub main()
Try
SubMain()
Catch ex As Exception
HandleUnhandled Exception(ex)
End Try
End Sub
Private Sub SubMain()
AddHandler AppDomain.Curre ntDomain.Unhand ledException, AddressOf
OnUnhandledExce ption
AddHandler Application.Thr eadException, AddressOf
OnGuiUnhandedEx ception
Application.Run (New Form1)
End Sub
Private Sub OnUnhandledExce ption(ByVal sender As Object, ByVal e As
UnhandledExcept ionEventArgs)
HandleUnhandled Exception(e.Exc eptionObject)
End Sub
Private Sub OnGuiUnhandedEx ception(ByVal sender As Object, ByVal e
As System.Threadin g.ThreadExcepti onEventArgs)
HandleUnhandled Exception(e.Exc eption)
End Sub
Private Sub HandleUnhandled Exception(ByVal o As Object)
Dim e As Exception = DirectCast(o, Exception)
If Not e Is Nothing Then
Debug.WriteLine ("Exception = " + e.GetType().ToS tring)
Debug.WriteLine ("Message = " + e.Message)
Debug.WriteLine ("FullText = " + e.ToString())
Else
Debug.WriteLine ("Exception = " + o.GetType().ToS tring)
Debug.WriteLine ("FullText = " + o.ToString())
End If
MsgBox(e.Messag e)
End Sub
End Module
'----------------------------------------------
'Here is the class that the textbox will be
'bound to. Note how it throws an error if you
'try to change the Name property to "Bob"
'----------------------------------------------
Public Class XClass
Private myName As String = String.Empty
Public Property Name() As String
Get
Return myName
End Get
Set(ByVal Value As String)
If Value = "Bob" Then
Throw New Exception("Name Cannot Be Bob")
Else
myName = value
End If
End Set
End Property
End Class
*************** *************** *************** ************
2. Now add a form to the project with the following code:
*************** *************** *************** ************
Public Class Form1
Inherits System.Windows. Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows. Forms.TextBox
Friend WithEvents TextBox2 As System.Windows. Forms.TextBox
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.TextBox1 = New System.Windows. Forms.TextBox
Me.TextBox2 = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
'
'TextBox1
'
Me.TextBox1.Loc ation = New System.Drawing. Point(24, 48)
Me.TextBox1.Nam e = "TextBox1"
Me.TextBox1.Siz e = New System.Drawing. Size(248, 20)
Me.TextBox1.Tab Index = 0
Me.TextBox1.Tex t = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Loc ation = New System.Drawing. Point(24, 80)
Me.TextBox2.Nam e = "TextBox2"
Me.TextBox2.Siz e = New System.Drawing. Size(248, 20)
Me.TextBox2.Tab Index = 1
Me.TextBox2.Tex t = "TextBox2"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(292, 181)
Me.Controls.Add (Me.TextBox2)
Me.Controls.Add (Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)
End Sub
#End Region
Private myXclass As New XClass
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
TextBox1.DataBi ndings.Add("Tex t", myXclass, "Name")
End Sub
End Class
*************** *************** *************** ************
3. Run the project and set textbox 1 to "Roger" note that you can still
tab to textbox2 or close the form...
4. Now change textbox1 to "Bob" -- you can no longer tab to another
control nor can you close the form... and despite all the error handling
in the program, you can't catch the error!
*************** *************** *************** ************
SO... WHO CAN FIGURE THIS ONE OUT??? BIG PRIZES, AWARDS, FAME AND GLORY
AWAIT YOU... (OR AT LEAST A BIG THANK YOU FROM ME, IF NOTHING ELSE!)
GOOD LUCK, AND THANK YOU IN ADVANCE!!!
*************** *************** *************** ************
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!