C# 如何以编程方式单击 TreeView TreeNode 使其在列表中突出显示并触发 AfterSelect 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840899/
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-08-05 03:43:34  来源:igfitidea点击:

How can I programmatically click a TreeView TreeNode so it appears highlighted in the list and fires the AfterSelect event?

c#winformstreeview

提问by ThomW

I have a TreeView control in a Winforms app, and basically the objective is to display a form that contains a TreeView control, and I want to display the form with a node opened (easy - EnsureVisible) and selected.

我在 Winforms 应用程序中有一个 TreeView 控件,基本上目标是显示一个包含 TreeView 控件的表单,我想显示一个打开(简单 - 确保可见)并选中节点的表单。

The problem that I'm running into is that when I set the TreeView control's SelectedNode property, the node isn't highlighted and the AfterSelect event isn't firing as I would expect it to. The AfterSelect thing is easy to code around, but the lack of highlighting is annoying.

我遇到的问题是,当我设置 TreeView 控件的 SelectedNode 属性时,节点没有突出显示,并且 AfterSelect 事件没有像我期望的那样触发。AfterSelect 的东西很容易编码,但缺少突出显示很烦人。

采纳答案by great_llama

Is it because the TreeView doesn't have focus? Does setting the TreeView's HideSelection property to False change the behavior you're seeing?

是不是因为 TreeView 没有焦点?将 TreeView 的 HideSelection 属性设置为 False 是否会改变您所看到的行为?

回答by Chris Persichetti

After you set the SelectedNode. Try selecting the treeView. Worked for me anyway.

设置 SelectedNode 后。尝试选择树视图。无论如何都为我工作。

private void button1_Click(object sender, EventArgs e)
{
this.treeView1.SelectedNode = this.treeView1.Nodes[1];
this.treeView1.Select();
}

回答by Jacob Adams

By highlighting, I am assuming you mean to make the text bold. It's not as elegant as some of the other client side technologies, but you could handle the node being changed and make the selected node bold with something like

通过突出显示,我假设您的意思是将文本加粗。它不像其他一些客户端技术那样优雅,但是您可以处理正在更改的节点并使用类似的东西使所选节点加粗

treeNode.Font = new Font(treeNode.Font, treeNode.Font.Style | treeNode.Bold);

回答by CSK

Try this to make the selected node bold:

试试这个使所选节点加粗:

selectedNode.NodeFont = new System.Drawing.Font(
    selectedNode.TreeView.Font,
    selectedNode.TreeView.Font.Style | FontStyle.Bold);

// You need to append an emptry string to work around this bug: 
// http://support.microsoft.com/kb/937215
selectedNode.Text += string.Empty;

回答by Tavousi

Okay I asked the question a little bit to soon I guess. Found a solution:

好吧,我想我很快就问了这个问题。找到了解决办法:

//clear background
RadTreeNodeCollection nodes = rtrvNetworkAll.Nodes;
foreach (RadTreeNode n in nodes)
{
        this.ClearRecursive(n);
}
//search a node with the build in find function
foreach (RadTreeNode n in nodes)
{
        this.FindRecursive(n);
}

// recursively move through the treeview nodes
private void FindRecursive(RadTreeNode treeNode)
{
        foreach (RadTreeNode tn in treeNode.Nodes)
        {
                // if the text properties match, color the item
                if (tn.Text == this.txtSearch.Text)
                {
                    tn.BackColor = Color.Yellow;
                }
                FindRecursive(tn);
        }
}

private void ClearRecursive(RadTreeNode treeNode)
{
       foreach (RadTreeNode tn in treeNode.Nodes)
       {
                tn.BackColor = Color.White;
                ClearRecursive(tn);
       }
}    

回答by Derek W

For me the issue was that the TreeViewcontrol did not necessarily have focus and therefore the selected node was not showing up as highlighted. The following code worked for me:

对我来说,问题是TreeView控件不一定具有焦点,因此所选节点未显示为突出显示。以下代码对我有用:

treeView.Focus();
treeView.SelectedNode = myTreeNode;

I ran into this exact issue while implementing some drag-and-drop functionality for a TreeViewcontrol.

我在为TreeView控件实现一些拖放功能时遇到了这个确切的问题。

回答by Darren Quinn

The easiest way to programmatically do this (that I have found) is to fake the click event. I needed to do this as when I searched for a node using TreeView.Nodes.Find(), I needed it to click each level of the hierarchy on the way down. So I basically did the following:

以编程方式执行此操作的最简单方法(我发现)是伪造点击事件。我需要这样做,因为当我使用 TreeView.Nodes.Find() 搜索节点时,我需要它在向下的过程中单击层次结构的每个级别。所以我基本上做了以下事情:

tvMyTreeView_NodeMouseClick(tvMyTreeView, new TreeNodeMouseClickEventArgs(myNode, MouseButtons.Left, 1, 0, 0));

Which fired my event and faking a left single click on the myNode node. Inside that event you can format e.Node however you would want to colourise it. Hope this helps.

这触发了我的事件并假装在 myNode 节点上单击左键。在该事件中,您可以格式化 e.Node,但是您希望对其进行着色。希望这可以帮助。