在 VB.NET 中从 TreeView 显示表单

发布于 2024-09-03 22:17:33 字数 988 浏览 12 评论 0原文

我正在尝试使用树视图控件显示特定表单,该控件的节点将其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-09-10 22:17:33

这里的问题是,如果用户关闭表单,该对象将被释放。

有几种方法可以处理这个问题,其中一些比另一种更优雅。

  1. 此时为 FormX_Closed() 添加处理程序,您可以重置标记引用
  2. 您可以切换到“显示对话框”进程,然后在 DoubleClick() 方法结束之前重置

这实际上取决于这些表单正在做什么。通常,我会看到您的 DoubleClick 方法执行查找,然后创建实例,因为如果您创建实例,您使用的内存可能不需要,但考虑到您的应用程序,这可能是必要的。

查找示例

根据评论中的要求,如果我以不同的方式执行此操作,我将使用枚举值,甚至是标记的简单整数“键”值。然后我会创建如下所示的内容来展示它。

Select Case myTag
    Case 1
        Dim formInstance As New Form1()
        formInstance.Show()
    Case Else
        Dim formInstance As New Form2()
        formInstance.Show()
End Select

基本上集中它并按需启动表单,如果您想要更多控制或有很多不同的表单类型,您可以将其切换为另一种设计模式。

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.

  1. Add a handler for FormX_Closed() at this time you can reset the tag reference
  2. You could switch to a "Show dialog" process and then reset before your DoubleClick() method ends

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.

Select Case myTag
    Case 1
        Dim formInstance As New Form1()
        formInstance.Show()
    Case Else
        Dim formInstance As New Form2()
        formInstance.Show()
End Select

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.

北笙凉宸 2024-09-10 22:17:33

当用户关闭表单时,该表单将被释放。一种选择是处理表单的关闭事件,只是隐藏它而不是关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文