我正在为excel中的用户形式编写树景。我想允许该表单的用户能够编辑任何节点的名称,并了解需要标签的属性,但是我不确定编写代码的方法。感谢任何形式的帮助。
您需要一个从Microsoft的ActiveX控件,可追溯到Visual Basic 6.0时代(VBA是VB6的变体)
尝试这个
https://msdn.microsoft.com/en-us/library/ms172635(v = vs.90).aspx
一个人通过转到控制工具箱,然后选择其他控件,然后从列表Microsoft TreeView Control, version 6.0
使用对象浏览器并选择TreeView
类允许对需要使用的方法和事件进行调查。一个人必须将LabelEdit
属性设置为tvwAutomatic
,并允许系统处理编辑,然后使用AfterLabelEdit
或一个将LabelEdit
属性设置为tvwManual
,并且用户双击节点上的用户,然后您陷阱,这是DoubleClick
事件,并手动地将致电StartLabelEdit
,使用AfterLabelEdit
进行验证编辑。
一些链接:
LabeLedit属性
VB编码提示Treeview-标签编辑
一些示例代码
Option Explicit
Private Sub TreeView1_DblClick()
Dim nodSelected As MSComctlLib.Node
Set nodSelected = TreeView1.SelectedItem
If nodSelected.Text <> "root" Then
TreeView1.StartLabelEdit
End If
End Sub
Private Sub UserForm_Initialize()
TreeView1.Style = tvwTreelinesPlusMinusText
TreeView1.LabelEdit = tvwManual
'Add some nodes to the TreeView
Dim nodRoot As MSComctlLib.Node
Set nodRoot = TreeView1.Nodes.Add(Key:="root", Text:="root")
'
Dim nodChildren(1 To 2) As MSComctlLib.Node
Set nodChildren(1) = TreeView1.Nodes.Add(nodRoot, tvwChild, "child 1", "child 1")
Set nodChildren(2) = TreeView1.Nodes.Add(nodRoot, tvwChild, "child 2", "child 2")
Dim nodGrandChildren(1 To 3) As MSComctlLib.Node
Set nodGrandChildren(1) = TreeView1.Nodes.Add(nodChildren(1), tvwChild, "grandchild 1", "grandchild 1")
Set nodGrandChildren(2) = TreeView1.Nodes.Add(nodChildren(2), tvwChild, "grandchild 2", "grandchild 2")
Set nodGrandChildren(3) = TreeView1.Nodes.Add(nodChildren(2), tvwChild, "grandchild 3", "grandchild 3")
End Sub
Private Sub TreeView1_AfterLabelEdit(Cancel As Integer, NewString As String)
' Make sure that we have a value in the Label
If Len(NewString) < 1 Then
' The Label is empty
MsgBox "Error! You must enter a value"
Cancel = True
Else
MsgBox "You successfully edited label to " & NewString
End If
End Sub
注意:单击根以展开子节点(不明显)。