How to set colors to half word of a tree view node in C#??

newnode.backcolor=color.red ; ------> this gives colors to all the nodes in the treeview.


But i need to get colored only a first word. Eg: Java Programs ....i need to color the word Java and not Programs

Thank u....... waiting for the replies..........need it urgent............

Hi,
not sure if you can achieve colouring just a part of the whole word of a node. But you can use different colors for different nodes in a treeview by accessing the particular node and applying style of forecolor or backcolor as required.

Thanks for ur reply.....


but i need to highlight a part of a node....Actually in a treeview a list of names will be displayed like

Divya Vasu
Helen James
now i have to highlight the husbands name alone......

like below

Divya Vasu
Helen James

Set DrawMode property of treeview with OwnerDrawText or OwnerDrawAll.

private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
            TreeNode root = new TreeNode("Programming");
            treeView1.Nodes.Add(root);

            root.Nodes.Add("Java Programming");
            root.Nodes.Add("C Programming");
        }

        void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            string[] s = e.Node.Text.Split(' ');
            e.Graphics.DrawString(s[0], new Font("Arial",10f) ,Brushes.Red , e.Bounds.Location);
            if (s.Length > 1)
            {
                Point newst = e.Bounds.Location;
                newst.X = newst.X + (int) e.Graphics.MeasureString(s[0],new Font("Arial",10f)).Width ;
                e.Graphics.DrawString(s[1], new Font("Arial", 10f), Brushes.Green, newst);
            }
        }

Thank u So much...... I got the output Perfectly..................

Once again Thanks a Lot ............................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.