【发布时间】:2012-07-11 03:27:57
【问题描述】:
我有一个由控件列表构建的表单。但是,他们通过删除和重建这些控件来刷新这些控件中的数据。这就是它变得敏感的地方。当我第一次单击另一个文本框时发生错误,该文本框触发了前一个文本框中的离开事件,该文本框调用清理功能以重建所有控件。单击的文本框包含在已销毁项目列表中,这就是错误“无法访问已命名的已处置对象”发生的原因。但是,我只是不知道在哪里处理 System.ObjectDisposedException 因为我无法在创建表单时捕获它。
这是崩溃日志
System.ObjectDisposedException: Can not access a disposed object named "TextBox".
Object name: "TextBox".
Has System.Windows.Forms.Control.CreateHandle ()
Has System.Windows.Forms.TextBoxBase.CreateHandle ()
Has System.Windows.Forms.Control.get_Handle ()
Has System.Windows.Forms.Control.set_CaptureInternal (Boolean value)
Has System.Windows.Forms.Control.WmMouseDown (Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc (Message & m)
Has System.Windows.Forms.TextBoxBase.WndProc (Message & m)
Has System.Windows.Forms.TextBox.WndProc (Message & m)
Has System.Windows.Forms.ControlNativeWindow.OnMessage (Message & m)
Has System.Windows.Forms.ControlNativeWindow.WndProc (Message & m)
at System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我也尝试使用声明 if Control.Isdisposed then return 但似乎离开或 mousedown 事件并不关心它:S
你们能帮我找到我可以在这个表单上处理这个错误的地方吗? 我无法通过调试来跟踪它,它只是在 End Sub 之后弹出。
在 vb.net FrameWorks 1.1 中编码
这是我销毁目标对象的代码
Private Sub viderRecursiveStack(ByVal control As Control)
Dim stack As New stack
Dim ctl As control
Dim enfantAssocie As ArrayList
stack.Push(control)
While stack.Count > 0
ctl = CType(stack.Pop, control)
If Not ctl Is Nothing Then
If TypeOf ctl Is Panel Then
'Cree la liste des enfants associés
enfantAssocie = New ArrayList(ctl.Controls)
For Each ctli As control In enfantAssocie
If Not TypeOf ctli Is EasyDeal.Controls.EasyDealLabel3D AndAlso** Not TypeOf ctli Is EasyDeal.Controls.EasyDealButton Then
stack.Push(ctli)
ctl.Controls.Remove(ctli)
End If
Next
Else
RemoveHandler ctl.Leave, AddressOf txtEquipAddCommissionChanged
ctl.Dispose()
End If
End If
End While
End Sub
【问题讨论】:
-
正如@SteveDog 建议的那样,您可以绕过这个错误,但代码将非常脆弱,并且很可能依赖于 Windows 和框架版本。如果是我,我会试图找到另一种描述你想要的行为的方式。
-
我明白了,如果可能的话,我想自己处理 System.ObjectDisposedException!
-
这不是您可以处理的异常,它是您代码中的错误。表单的成员,如“TextBox1”,正在引用已释放的控件。不要在上面贴创可贴,修复错误。例如,重新运行 InitializeControls() 应该可以修复它。
-
帕桑特先生对这一点是正确的(也是:)),充其量你会解决一个症状。在代码附近嗅探,例如在某处添加另一个事件处理程序,它会再次中断。
标签: vb.net events exception controls dispose