C# WinForms TreeView - 如何手动“突出显示”节点(就像它被点击一样)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838807/
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
WinForms TreeView - how to manually "highlight" node (like it was clicked)
提问by Petr
I would need to know how to let the programatically selected node make graphically in the state "selected" like the user clicked on it. SelectedNode only makes this one internally selected. Thank you very much!
我需要知道如何让以编程方式选择的节点以图形方式显示为“已选择”状态,就像用户单击它一样。SelectedNode 只会在内部选择这个。非常感谢!
采纳答案by Pondidum
The reason it does not show as highlighted is due to the tree view not having focus. This is in a button click event on my test form:
它没有突出显示的原因是树视图没有焦点。这是在我的测试表单上的按钮单击事件中:
TreeView1.SelectedNode = TreeView1.Nodes(2);
TreeView1.Focus();
Which highlights the node properly. if you remove the Focus();
call it doesn't highlight until you click into the tree view (anywhere in the tree view, not necessarily on to the node that you want to be selected).
这正确突出显示了节点。如果您删除Focus();
调用,它不会突出显示,直到您单击进入树视图(树视图中的任何位置,不一定是您要选择的节点)。
回答by theraneman
Not sure, but can you not change the background color of that node?
不确定,但是您不能更改该节点的背景颜色吗?
回答by Tim Robinson
The underlying Win32 control supports this (think it's TVIS_DROPHILITED
), but I can't see the same functionality exposed through the TreeView
control.
底层的 Win32 控件支持此功能(认为是TVIS_DROPHILITED
),但我看不到通过该TreeView
控件公开的相同功能。
As theraneman says, you could fake it with the TreeNode.ForeColor
and BackColor
properties...
正如 theraneman 所说,你可以用TreeNode.ForeColor
和BackColor
属性来伪造它......
回答by Samball
This works for me for .net 3.5: Set the treeview component's DrawModeproperty to: OwnerDrawAllThen in the DrawNodeevent write the following:
这适用于 .net 3.5:将树视图组件的DrawMode属性设置为:OwnerDrawAll然后在DrawNode事件中写入以下内容:
if (((e.State & TreeNodeStates.Selected) != 0) && (!MyTreeView.Focused))
e.Node.ForeColor = Color.Blue;
else
e.DrawDefault = true;
And in the BeforeSelectevent have:
并且在BeforeSelect事件中有:
if (MyTreeView.SelectedNode != null)
MyTreeView.SelectedNode.ForeColor = Color.Black;
e.Node.ForeColor = Color.Blue;
回答by OOZ
I don't know if it helps you or not but check the taborder of the the page and make sure that the tree view control has tab order of 0
我不知道它是否对您有帮助,但请检查页面的 taborder 并确保树视图控件的 tab order 为 0
回答by Anonymous
I had an similar issue and wanted to have a TreeView
node selected (highlighted) on form load.
Maybe someone has the same problem, too.
我有一个类似的问题,并希望在 form load 上TreeView
选择(突出显示)一个节点。也许有人也有同样的问题。
I first tried Pondidum's solution. Without success.
But then I found the solution in another thread: Simply set the TabIndex
of the TreeView
to 0.
In that case you don't need to set the focus. Just choose the node that should be selected by using SelectedNode
and set the TabIndex
. That's it.
我首先尝试了 Pondidum 的解决方案。没有成功。但后来我发现在另一个线程解决方案:只需设置TabIndex
的TreeView
为0。在这种情况下,将焦点设置你不需要。只需选择应使用的节点SelectedNode
并设置TabIndex
. 就是这样。
回答by Mohit Agarwal
TreeView1.SelectedNode.BackColor = SystemColors.HighlightText; // This will work
Above solutions will only set the focus on it but will not change the highlight view of it.
以上解决方案只会将焦点设置在它上面,而不会改变它的突出显示视图。
回答by gridtrak
Here is what I got to work:
这是我的工作:
void myProcedure()
{
// Hookup a DrawMode Event Handler
this.myTV.DrawNode += myTV_DrawNode;
// Set DrawMode and HideSelection
this.myTV.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.myTV.HideSelection = false;
// Make sure the TreeView has Focus
this.myTV.Focus();
// Make sure the TreeView is Selected
this.myTV.Select();
// If the TreeView has a Node, I want to select the first Node to demonstrate.
if (this.myTV.Nodes.Count > 0)
{
// Make sure the node is visible
this.myTV.Nodes[0].EnsureVisible();
// Make sure the Node is Selected
this.myTV.SelectedNode = myTV.Nodes[0];
}
// Make sure the SelectedNode IS the Node that we programmatically want to select.
textBox1.Text = this.myTV.SelectedNode.Text;
// if we display sanityCheck1 string, it actually is the correct node.text
// Make sure .NET runtime knows the Node is selected
textBox1.Text += " is Selected = " + this.myTV.SelectedNode.IsSelected.ToString();
}
Following up: laaltoanswered the How to HighLight the TreeView.Node. The following code in the DrawNode Event Handler, from samball's answer, properly highlights the TreeView.Node based on its Selected State.
跟进: laalto回答了如何突出显示 TreeView.Node。DrawNode 事件处理程序中的以下代码,来自samball的回答,根据其选定状态正确突出显示 TreeView.Node。
private void myTV_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
// first, let .NET draw the Node with its defaults
e.DrawDefault = true;
// Now update the highlighting or not
if (e.State == TreeNodeStates.Selected)
{
e.Node.BackColor = SystemColors.Highlight;
e.Node.ForeColor = SystemColors.HighlightText;
}
else
{
e.Node.BackColor = ((TreeView)sender).BackColor;
e.Node.ForeColor = ((TreeView)sender).ForeColor;
}
}
Platform = C# .NET 4.5 in Windows 10, Visual Studio 2015
平台 = Windows 10 中的 C# .NET 4.5,Visual Studio 2015
回答by vominhtien961476
TreeView1.SelectedNode = TreeView1.Nodes(2);
this.ActiveControl = TreeView1;
This works for me (.net 4.7)
这对我有用(.net 4.7)