Treeview How to Remove Color from the Background of a Node After Click

Asked

Viewed 16 times

0

When the component was clicked but my intention is to remove this color blue half red if you use the zoom will notice these two color

1

I’ve tried all kinds of things but click it gets this color already look to remove this color from the selection in Design Visual Studio but n has the box showing the color of the node etc... I found nothing related to it.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        string nomeespecifico = e.Node.Text;

        foreach (var a in nodes)
        {
            treeView1.HideSelection = false;
            treeView1.SelectedNode = null;


            if (a.Text == nomeespecifico ){
            
                
                if (e.Button == MouseButtons.Right)
                {        
                    a.ForeColor = Color.Blue;
                    a.BackColor = Color.Transparent;

                    Console.WriteLine($"fui clicado direito {a.Text}");
                }
                else if (e.Button == MouseButtons.Left) {
                    treeView1.SelectedNode = a;
                    a.BackColor = Color.Transparent;
                    a.ForeColor = Color.Red;
                   

                    Console.WriteLine($"fui clicado esquerdo {a.Text}");
                }
            }
            
        }
    }

private void treeView1_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
    {
       
         selNode = 
            (TreeNode)treeView1.GetNodeAt(treeView1.
            PointToClient(Cursor.Position));
        if (selNode != null)
        {
            // faz a seleção ao automático quando o mouse passa pelo nó
            selNode.BackColor = Color.Transparent;
            selNode.ForeColor = Color.Blue;
          
        }
        Console.WriteLine("NodeMouseHover");
    }

1 answer

0

I found the solution in itself documentation. Follow the complete code below.

Font tagFont = new Font(FontFamily.GenericSansSerif,8, FontStyle.Bold);

treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;

treeView1.DrawNode += new DrawTreeNodeEventHandler(Ty_DrawNode);

treeView1.MouseDown += new MouseEventHandler(myTreeView_MouseDown);


    private void Ty_DrawNode(Object ob,DrawTreeNodeEventArgs e)
    {
        if ((e.State & TreeNodeStates.Selected)!= 0)
        {
           e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node));
            Font nodefont = e.Node.NodeFont;
            if (nodefont == null) nodefont = ((TreeView)ob).Font;

            e.Graphics.DrawString(e.Node.Text, nodefont, Brushes.White,
                Rectangle.Inflate(e.Bounds, 2, 0));
        }
        else
        {
            e.DrawDefault = true;
        }
    }

    private RectangleF NodeBounds(TreeNode node)
    {
        Rectangle bounds = node.Bounds;
        if (node.Tag != null)
        {
            Graphics g = treeView1.CreateGraphics();
            int tagwhite = (int)g.MeasureString(node.Tag.ToString(),
                tagFont).Width;
            bounds.Offset(tagwhite, 0);
            bounds = Rectangle.Inflate(bounds, tagwhite, 0);
            g.Dispose();
        }
        return bounds;
    }

    private void myTreeView_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);
        if (NodeBounds(clickedNode).Contains(e.X, e.Y))
        {
            treeView1.SelectedNode = clickedNode;
        }
    }

OBS: e.Graphics.FillRectangle(Brushes.Red, NodeBounds(e.Node)); just change the part of Brushes (and choose the color)

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.