本例实现双击节点,弹出对话框并显示节点信息
先对treeview1加入些数据,代码如下:
Sub Example()
Dim N As New TreeNode, I As Integer, J As Integer
For I = 0 To 3
With N.Nodes.Add((I * 15).ToString & "-" & ((I + 1) * 15 - 1).ToString)
For J = 0 To 14
.Nodes.Add((J + I * 15).ToString)
Next
End With
Next
Dim NodeToAdd As TreeNode
For I = 1 To 24
NodeToAdd = N.Clone
NodeToAdd.Text = I.ToString
TreeView1.Nodes.Add(NodeToAdd)
Next
End Sub
在Treeview1_click事件中加入如下代码:
Dim N As TreeNode = CType(sender, Windows.Forms.TreeView).SelectedNode
'//如果树节点不为0,则显示提示信息,
If N.GetNodeCount(False) = 0 Then
'//当前节点的文本字符
Dim S As String = N.Text
'//如果字符长度为1,则在前而加0
If S.Length = 1 Then
S = S.Insert(0, "0") '//指定位置插入字符
End If
'//上一树节点的字符加当前节点字符
S = N.Parent.Text & ":" & S
‘//当然你还可以显示再上级节点的字符
‘//代码就变为:S=N.Parent.Parent.Text & “:” & S
MessageBox.Show("你选择的节字符是: " & S)
End If