在 VB.NET 中从 TreeView 显示表单
我正在尝试使用树视图控件显示特定表单,该控件的节点将其 tag
值设置为我需要显示的表单的实例。我在 DoubleClick 事件中的代码在第一次显示表单时效果很好,但之后我遇到了对象已处置异常。如您所见,我尝试通过重置标签来处理它,但这不起作用。有什么方法可以多次显示表单,而无需在出现异常时随时执行 switch 语句并将标签重置为正确的表单类型?我想要一些看起来更好的东西,就像我展示表格的方式一样。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fm2 As New Form2()
Dim fm3 As New Form3()
TreeView1.Nodes(0).Tag = fm2
TreeView1.Nodes(1).Tag = fm3
End Sub
Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
Try
CType(TreeView1.SelectedNode.Tag, Form).Show()
Catch odex As ObjectDisposedException
TreeView1.SelectedNode.Tag = New Form()
TreeView1_DoubleClick(sender, e)
Catch nrex As NullReferenceException
'No node selected, do nothing.
End Try
End Sub
End Class
I am attempting to show a specific form using a treeview control, the nodes of which have their tag
value set to an instance of the Form I need to show. The code I have in the DoubleClick event works great for the first time I show a form, but after that I get an object disposed exception. As you can see, I tried handling it by resetting the tag, but that didn't work. Is there any way I can show the form more than once without going through a switch statement anytime the exception comes up and resetting the tag to the right type of form? I'd like something nicer looking like the way I show the form.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fm2 As New Form2()
Dim fm3 As New Form3()
TreeView1.Nodes(0).Tag = fm2
TreeView1.Nodes(1).Tag = fm3
End Sub
Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
Try
CType(TreeView1.SelectedNode.Tag, Form).Show()
Catch odex As ObjectDisposedException
TreeView1.SelectedNode.Tag = New Form()
TreeView1_DoubleClick(sender, e)
Catch nrex As NullReferenceException
'No node selected, do nothing.
End Try
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是,如果用户关闭表单,该对象将被释放。
有几种方法可以处理这个问题,其中一些比另一种更优雅。
这实际上取决于这些表单正在做什么。通常,我会看到您的 DoubleClick 方法执行查找,然后创建实例,因为如果您创建实例,您使用的内存可能不需要,但考虑到您的应用程序,这可能是必要的。
查找示例
根据评论中的要求,如果我以不同的方式执行此操作,我将使用枚举值,甚至是标记的简单整数“键”值。然后我会创建如下所示的内容来展示它。
基本上集中它并按需启动表单,如果您想要更多控制或有很多不同的表单类型,您可以将其切换为另一种设计模式。
Your problem here is if the user closes the form, the object will have been disposed.
There are a few ways you can handle this, some more elegant than the other.
It really depends on what these forms are doing. Typically i would see your DoubleClick method doing a lookup then creating the instance, simply because if you create instances you are using memory that might not be needed, but that is something that might be necessary considering your application.
Lookup Example
As requested in the comments, if I were to do this a different way, I would use an enumeration value, or even a simple integer "key" value for the tag. Then I would create something like the following to show it.
Basically centralize it and launch the form on demand, you could switch this out for another design pattern if you wanted more control or had a lot of different form types.
当用户关闭表单时,该表单将被释放。一种选择是处理表单的关闭事件,只是隐藏它而不是关闭。
The form gets disposed when it is closed by the user. One option is to handle Closing event of the form ans just hide it instead of closing.