我正在为 Excel 中的用户窗体编写 TreeView 。我希望允许表单的用户能够编辑任何节点的名称并了解labeledit 属性是必需的,但是我不确定编写代码的方式。感谢任何形式的帮助。
最佳答案
您需要 Microsoft 提供的 ActiveX 控件,该控件可以追溯到 Visual Basic 6.0 时代(VBA 是 VB6 的变体)
试试这个
https://msdn.microsoft.com/en-us/library/ms172635(v=vs.90).aspx
通过转到控件工具箱,然后从列表中选择其他控件,将控件放置在窗体上 Microsoft TreeView Control,版本 6.0
使用对象浏览器并选择TreeView
类可以调查您需要使用的方法和事件。必须将 LabelEdit
属性设置为 tvwAutomatic
并允许系统处理编辑,并使用 AfterLabelEdit
捕获事件,或者设置 >LabelEdit
属性设置为 tvwManual
,如果用户双击该节点,则您捕获这是 DoubleClick
事件并手动调用 StartLabelEdit
>,使用 AfterLabelEdit
验证编辑。
一些链接:
VB Coding Tip Treeview - Label-Editing
一些示例代码
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
注意:点击根节点可以展开子节点(不明显)。
关于vba - 在 Excel TreeView VBA 中编辑节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41376043/