使用 VBA 在 TreeView 中选择一个节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/459589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:11:14  来源:igfitidea点击:

Select a Node in a TreeView with VBA

excelvbaexcel-vbatreeview

提问by daharon

I have a TreeViewwithin a UserFormin Excel. When a Node is selected from the TreeView, a ListBoxis populated with data.
When an item in the ListBoxis double-clicked, a separate UserFormis shown which allows the user do to stuff.
Once the user returns back to the TreeView UserForm, I want the Nodethat was selected previously to be highlighted.

我有一个TreeView内部的UserFormExcel中。当从 中选择一个节点时TreeView,aListBox将填充数据。
ListBox双击 中的项目时,UserForm会显示一个单独的项目,允许用户进行操作。
一旦用户返回到TreeView UserForm,我希望Node突出显示先前选择的 。

The problem is that the UserFormbasically resets itself, and I can't figure out how to select a Nodewith VBA.

问题是UserForm基本上会自行重置,我不知道如何Node使用 VBA选择一个。

I'm at the point where I'm debating on whether or not I can just manually fire a NodeClickevent, as everything else I've tried has failed.

我现在正在争论是否可以手动触发NodeClick事件,因为我尝试过的其他一切都失败了。

Any tips?

有小费吗?

回答by Arnold

I know this is an old question but i had trouble enough finding a decent answer myself so here we go. Index or Key's are not always available. On create I place the fullpath of the node in the node.tag. On double-click I recall the tag value, later on I search the treeview node collection for the tag. To much? not efficient? maybe but its works likes a charm every time and i can use my own identifiers in the tag based on the purpose of the treeview. The find code:

我知道这是一个老问题,但我自己很难找到一个像样的答案,所以我们开始吧。索引或键并不总是可用。在创建时,我将节点的完整路径放在 node.tag 中。双击时,我会调用标记值,稍后我会在树视图节点集合中搜索标记。多吗?效率不高?也许但它的作品每次都像魅力一样,我可以根据树视图的目的在标签中使用我自己的标识符。查找代码:

Sub MyTreeview_FindNode(strKey As String)
Dim myNode As Node

   For Each myNode In Me.Treeview.Nodes
       If myNode.Tag = strKey Then
          myNode.Selected = True
          myNode.EnsureVisible
          End If
       Next
End Sub 

回答by Zdzis?aw Kes

In my Excel works:

在我的 Excel 作品中:

TreeView1.Nodes(key).Selected = True

回答by Ryan

You have several options. First, when the TreeView UserForm displays the second UserForm, you either need to:

您有多种选择。首先,当 TreeView UserForm 显示第二个 UserForm 时,您需要:

  1. Save the ID of the selected node (use a form-level or module-level variable). Then you need to write a method to select the node upon return to the form. IIRC, you need to have a unique 'Key' or ID for each node element and then use TreeView.Select for the node returned from TreeView.FindNode. -- or --
  2. Hide the TreeView UserForm instead of closing it (Me.Hide). When the second UserForm is closed (or OK/Cancel pressed), then show the TreeView UserForm again (TreeViewForm.Show).
  1. 保存所选节点的 ID(使用表单级或模块级变量)。然后您需要编写一个方法来在返回表单时选择节点。IIRC,您需要为每个节点元素具有唯一的“键”或 ID,然后对从 TreeView.FindNode 返回的节点使用 TreeView.Select。 - 或者 -
  2. 隐藏 TreeView UserForm 而不是关闭它 (Me.Hide)。当第二个用户窗体关闭(或按下确定/取消)时,再次显示 TreeView 用户窗体 (TreeViewForm.Show)。

回答by Bas Verlaat

Try this: objTreeview.Object.Nodes(KEY_NAME).Selected=TRUE

试试这个:objTreeview.Object.Nodes(KEY_NAME).Selected=TRUE

Where KEY_NAME is the key (string) that you used to create the node

其中 KEY_NAME 是您用于创建节点的键(字符串)