TreeView Class Key Points

时间:2021-05-14 16:01:05

TreeView keep selected node highlighted

public QualityCheck()
        {
            InitializeComponent();

            //trvIndexName.HideSelection = false;
            //trvIndexName.SelectedNode.BackColor = Color.BlueViolet;  // cause DLL problem

            trvIndexName.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            trvIndexName.DrawNode += new DrawTreeNodeEventHandler(trvIndexName_DrawNode);

        }

        void trvIndexName_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            ) && (!trvIndexName.Focused))
                e.Node.ForeColor = Color.Blue;
            else
                e.DrawDefault = true;
        }

        private void trvIndexName_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (trvIndexName.SelectedNode != null)
                trvIndexName.SelectedNode.ForeColor = Color.Black;
            e.Node.ForeColor = Color.Blue;
        }