我的Tree视图有2个Root(Masters,Transactions)节点,每个Root有1个子节点(See Tree view in Image)
Masters-> Party master
交易 - >订单接受
点击Party Master(子节点)时我想显示frmPartymaster.vb
表单
和
单击订单接受(子节点)时显示frmorder.vb
表单
我在选择事件后尝试树视图(见下面的代码)
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
For Each node As TreeNode In Me.TreeView1.Nodes
GetChildren(node)
Next node
End Sub
Function GetChildren(ByVal parentNode As TreeNode) As List(Of String)
Dim nodes As List(Of String) = New List(Of String)
GetAllChildren(parentNode, nodes)
Return nodes
End Function
Sub GetAllChildren(ByVal parentNode As TreeNode, ByVal nodes As List(Of String))
For Each childNode As TreeNode In parentNode.Nodes
nodes.Add(childNode.Text)
GetAllChildren(childNode, nodes)
Next
End Sub
Above Code返回Return nodes
变量
现在我正在寻找
如何使用nodes
变量值
点击儿童时如何显示适当的表格 节点(我知道子节点没有点击事件)
告诉我找到任何其他有效方式而不是afterselect
事件
因为它每次都遍历所有根节点和子节点
答案 0 :(得分:0)
根据您提供的代码,您甚至不会尝试实例化另一个类,具体取决于您选择的节点。所有这一切都是将节点添加到列表中,这不是必需的......
此解决方案可以添加 任何 类,其类型为Form
。
有几种方法可以实现,但我只提供了一种方法来实现您想要实现的目标。
Imports System.Reflection
Public Class Form1
Private Const FORM_PARTY_MASTER As String = "frmPartyMaster"
Private Const FORM_ORDER As String = "frmorder"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mNode As New TreeNode With {.Text = "Masters"}
mNode.Nodes.Add(New TreeNode With {.Text = "Party Masters", .Tag = FORM_PARTY_MASTER})
Dim transNode As New TreeNode With {.Text = "Transaction"}
transNode.Nodes.Add(New TreeNode With {.Text = "Order/Acceptance", .Tag = FORM_ORDER})
TreeView1.Nodes.AddRange({mNode, transNode})
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim obj As Form
Try
If e.Node.Tag IsNot Nothing Then
obj = CType(Activator.CreateInstance(Type.GetType("WindowsApplication5." & e.Node.Tag.ToString, False)), Form)
If obj IsNot Nothing Then
Using obj
obj.ShowDialog()
End Using
End If
End If
Catch ex As Exception
'handle your exception
End Try
End Sub
End Class
<强>解释强>
Tag
的{{1}}属性,我们将使用它来获取我们需要的形式的实例。TreeNode
事件。您将看到我如何创建您需要的任何控件的实例;没有案例或是否有陈述。AfterSelect
更改为您的命名空间。