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

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

C# how do I ensure the selected node remains highlighted when focus lost

c#treeview

提问by Brad

I have changed the Treeview.HideSelection = false; But how do I insure that when focus is lost that the selected item remains the original selected color?

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

EDIT:

编辑:

I have a listview on a form that holds a list of process events. Alongside the Treeview on the same form is a series of selections that the user completes to classify the event in the listview. However, when the user makes a selection on one of the classification controls the blue highlighted selected Treeview item turns to a grey color. I was hoping to find the property that defines this color to make it the same color blue.

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

Any suggestions.

有什么建议。

Update:

更新:

 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);
    }
}

采纳答案by scottm

Setting ListView.HideSelectionto true means that when focus is lost, it will hide the selection. By setting HideSelectionto false, the selected item will still have the color indicator showing which item is selected.

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

回答by Rob Kennedy

Generally, you don't. The change in color is one of the visual cues that indicate which control has the focus. Don't confuse your customers by getting rid of that.

一般来说,你不会。颜色的变化是指示哪个控件具有焦点的视觉提示之一。不要通过摆脱它来混淆你的客户。

If you want to buck the convention, then you can make your control owner-drawn, and then you can paint the items whatever color you want.

如果你想打破惯例,那么你可以让你的控件自绘,然后你可以为项目涂上你想要的任何颜色。

Another option, in your case, is to use a drop-down combo box instead of a list box. Then the current selection is always clear, no matter whether the control has the focus. Or, you could consider using a grid, where each event has all its settings given separately, and then "selection" doesn't matter at all.

在您的情况下,另一种选择是使用下拉组合框而不是列表框。那么当前的选择总是清晰的,不管控件是否有焦点。或者,您可以考虑使用网格,其中每个事件的所有设置都单独给出,然后“选择”根本不重要。

回答by Schmuli

If I were doing it, I would simply have an extra Label alongside the ListView, above the classification controls being selected, that would indicate which process event has been selected. You can also use said Label to add extra details about the event (if any).

如果我这样做,我只会在 ListView 旁边有一个额外的标签,在选择的分类控件上方,这将指示已选择哪个流程事件。您还可以使用所述标签添加有关事件的额外详细信息(如果有)。

This way, you are sticking to standard UI conventions andmaking it that much clearer to the user what their current selection is.

这样,您就可以坚持标准的 UI 约定,让用户更清楚他们当前的选择是什么。

回答by dtyrant

I like the HideSelection = false; answer, because:

我喜欢 HideSelection = false; 回答,因为:

  1. It's easy

  2. I have a search function that cycles through the nodes and marks the relevant ones by changing it's background to yellow, when the user clicks on the node a textbox fills with the relevant info attached to that node, before I used this method, if the user clicked on the textbox to scroll through it, it would unhighlight the node and make it hard to keep track of which node was selected, this way it is still highlighted in a light gray colour showing it is not in focus, opposed to the blue highlight which is used when it is in focus. I could have 'painted' the node but with the yellow background for search results would have made life more complicated than it needed to be.

  3. Did I mention it was easy?

  1. 这很简单

  2. 我有一个搜索功能,它循环浏览节点并通过将其背景更改为黄色来标记相关节点,当用户单击节点时,在我使用此方法之前,文本框会填充附加到该节点的相关信息,如果用户单击文本框滚动浏览它,它会取消突出显示节点并使其难以跟踪选择了哪个节点,这样它仍然以浅灰色突出显示,表明它没有聚焦,而不是蓝色突出显示当它处于焦点时使用。我本可以“绘制”节点,但是搜索结果的黄色背景会使生活变得比它需要的更复杂。

  3. 我有没有提到这很容易?

回答by Ploy

I use this code; it works for me.

我用这个代码;这个对我有用。

design: Mytreeview.HideSelection = Trueyou will manual highlight the lose focus selected node.

设计:Mytreeview.HideSelection = True您将手动突出显示失去焦点的选定节点。

Private Sub MyTreeview_Leave(sender As Object, e As EventArgs) Handles MyTreeview.Leave
    MyTreeview.SelectedNode.BackColor = Color.LemonChiffon
End Sub

Private Sub MyTreeview_BeforeSelect(sender As Object, e As TreeViewCancelEventArgs) Handles MyTreeview.BeforeSelect
    If MyTreeview.SelectedNode IsNot Nothing Then 
        MyTreeview.SelectedNode.BackColor = Color.White
End Sub