VB2010 我在 Treeview 的 NodeMouseClick 中有一个非常好的例程,它根据用户单击的节点来隐藏/显示表单中的面板。简化版:
Private Sub tvw_NodeMouseClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvw.NodeMouseClick
Dim pNode As TreeNode = e.Node 'get the node that was clicked
Dim nodeName As String = pNode.Name 'get the name of the node
Select Case nodeName.ToLower
Case "gen"
pnlGeneral.Visible = True
pnlOrigin.Visible = False
Case "ogn"
pnlGeneral.Visible = False
pnlOrigin.Visible = True
Case Else
'do nothing
End Select
End Sub
我的问题是表单加载我想要选择一个默认节点,或者实际上就像用户单击默认节点一样完成事情。
Private Sub frm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'initialize the treeview
With tvw
.Nodes.Add("gen", "General", "gen", "gen")
.Nodes.Add("ogn", "Origin", "ogn", "ogn")
.SelectedNode = .Nodes.Item("gen")
End With
End Sub
.SelectedNode 不这样做,并试图弄清楚如何在 .NET 中执行此操作。在 VB6 中,我曾经使用 tvw_NodeClick tvw.Nodes("gen")。