C# 如何确保所选节点在失去焦点时保持突出显示

标签 c# treeview

我改变了 Treeview.HideSelection = false; 但是,我如何确保在失去焦点时所选项目保持原始所选颜色?

编辑:

我在一个包含流程事件列表的表单上有一个 ListView 。在同一个表单上的 Treeview 旁边是一系列选择,用户完成这些选择以对 ListView 中的事件进行分类。但是,当用户在其中一个分类控件上进行选择时,蓝色突出显示的所选 Treeview 项目变为灰色。我希望找到定义此颜色的属性,使其成为相同的蓝色。

任何建议。

更新:

 public partial class myTreeView : TreeView
{
    TreeNode tn = null;
    public myTreeView()
    {
        InitializeComponent();
    }

    protected override void OnAfterSelect(TreeViewEventArgs e)
    {
        if (tn != null)
        {
            tn.BackColor = this.BackColor;
            tn.ForeColor = this.ForeColor;
        }
        tn = e.Node;
        base.OnAfterSelect(e);
    }
    protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
    {

        e.Node.BackColor = Color.Green;
        e.Node.ForeColor = Color.White;
        base.OnBeforeSelect(e);
    }
    protected override void OnGotFocus(System.EventArgs e)
    {

        base.OnGotFocus(e);
    }

    protected override void OnLostFocus(System.EventArgs e)
    {

        if (tn != null)
        {
            tn.BackColor = Color.Green;
            tn.ForeColor = Color.White;
        }
        // tn.BackColor = Color.Red;

        base.OnLostFocus(e);
    }
}

最佳答案

ListView.HideSelection 设置为 true 意味着当失去焦点时,它将隐藏选择。通过将 HideSelection 设置为 false,所选项目仍将有颜色指示器显示哪个项目被选中。

关于C# 如何确保所选节点在失去焦点时保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/456174/

相关文章:

c# - 将 WebProxy 与 HttpClient.SendAsync() 和 HttpRequestMessage 结合使用

c# - 如何在运行时在 wpf 中调整用户控件的大小

c# - 通过获取文件路径来排列 TreeView?

c# - 将自定义类的集合绑定(bind)到 TreeView

c# - C# 中 Concurrency::combinable<T> 的模拟?

c# - 程序集引用链是否需要传递?

c# - Monitor.Enter(object, ref bool) 比 Monitor.Enter(object) 有什么优势?

c# - 您可以数据绑定(bind) TreeView 控件吗?

xml - 将 XML 节点绑定(bind)到 TreeView 节点

JavaFX:向嵌套 CheckBoxTreeItem[s] 的复选框添加 CTRL 单击功能