Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:37:42+00:00 2026-06-07T21:37:42+00:00

I have a treeview and a imageList which contain 1 icon (folder.ico), I want

  • 0

I have a treeview and a imageList which contain 1 icon (folder.ico), I want to set icon only for root node, child node will don’t have icon, so I try to set image index for child node but it have some problem, look the picture:
enter image description here

My code:

        ImageList imageList = new ImageList();
        imageList.Images.Add(Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug\\","") + "\\Images\\folder.ico"), Color.Transparent);
        treeView1.ImageList = imageList;
        foreach (TreeNode node in treeView1.Nodes)
        {
            foreach (TreeNode node2 in node.Nodes)
            {
                node2.ImageIndex = 100;
                node2.SelectedImageIndex = 100;
            }
        }

Thanks

EDIT
I create a Custom TreeView at the answer of @Killercam:

class CustomTreeView : TreeView
{
public const int NOIMAGE = -1;

public CustomTreeView()
    : base()
{
    // .NET Bug: Unless LineColor is set, Win32 treeview returns -1 (default), .NET returns Color.Black!
    base.LineColor = SystemColors.GrayText;
    base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
}

protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
    // Space between Image and Label.
    const int SPACE_IL = 3;  

    // We only do additional drawing.
    e.DrawDefault = true;
    base.OnDrawNode(e);
    if (base.ShowLines && base.ImageList != null && e.Node.ImageIndex == NOIMAGE
        // exclude root nodes, if root lines are disabled
        //&& (base.ShowRootLines || e.Node.Level > 0))
            )
    {
        // Using lines & images, but this node has none: fill up missing treelines

        // Image size
        int imgW = base.ImageList.ImageSize.Width;
        int imgH = base.ImageList.ImageSize.Height;

        // Image center
        int xPos = e.Node.Bounds.Left - SPACE_IL - imgW / 2;
        int yPos = (e.Node.Bounds.Top + e.Node.Bounds.Bottom) / 2;

        // Image rect
        Rectangle imgRect = new Rectangle(xPos, yPos, 0, 0);
        imgRect.Inflate(imgW / 2, imgH / 2);

        using (Pen p = new Pen(base.LineColor, 1))
        {
            p.DashStyle = DashStyle.Dot;

            // Account uneven Indent for both lines.
            p.DashOffset = base.Indent % 2;

            // Horizontal treeline across width of image
            // account uneven half of delta ItemHeight & ImageHeight.
            int yHor = yPos + ((base.ItemHeight - imgRect.Height) / 2) % 2;

            //if (base.ShowRootLines || e.Node.Level > 0)
            //{
            //    e.Graphics.DrawLine(p, imgRect.Left, yHor, imgRect.Right, yHor);
            //}
            //else
            //{
            //    // for root nodes, if root lines are disabled, start at center
            //    e.Graphics.DrawLine(p, xPos - (int)p.DashOffset, yHor, imgRect.Right, yHor);
            //}

            e.Graphics.DrawLine(p,
                    (base.ShowRootLines || e.Node.Level > 0) ? imgRect.Left : xPos - (int)p.DashOffset,
                    yHor, imgRect.Right, yHor);
            if (!base.CheckBoxes && e.Node.IsExpanded)
            {
                // Vertical treeline , offspring from NodeImage center to e.Node.Bounds.Bottom
                // yStartPos: account uneven Indent and uneven half of delta ItemHeight & ImageHeight
                int yVer = yHor + (int)p.DashOffset;
                e.Graphics.DrawLine(p, xPos, yVer, xPos, e.Node.Bounds.Bottom);
            }
        }
    }
}

protected override void OnAfterCollapse(TreeViewEventArgs e)
{
    base.OnAfterCollapse(e);
    if (!base.CheckBoxes && base.ImageList != null && e.Node.ImageIndex == NOIMAGE)
    {
        // DrawNode event not raised: redraw node with collapsed treeline
        base.Invalidate(e.Node.Bounds);
    }
}
}

Then use it in my code:

private void TestCustomTreeView_Load(object sender, EventArgs e)
    {
        // @Killercam EDIT: Set the default Image to one that is not used.
        valForm.siteTreeView.ImageIndex = 100;
        valForm.siteTreeView.SelectedImageIndex = 100;

        TemplateCustomTreeView myTree = new TemplateCustomTreeView();
        myTree.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

        myTree.Location = new System.Drawing.Point(6, 26);
        myTree.Name = "tree";
        myTree.Scrollable = true;
        myTree.Size = new System.Drawing.Size(320, 296);
        myTree.ImageList = imageList1;
        /*Add item*/
        TreeNode node = new TreeNode();
        node.Name = "abc1";
        node.Text = "abc1";
        node.ImageIndex = 0;
        myTree.Nodes.Add(node);
        TreeNode node3 = new TreeNode();
        node3.Name = "abc2";
        node3.Text = "abc2";
        node3.ImageIndex = -1;            
        node.Nodes.Add(node3);
        ////
        TreeNode node2 = new TreeNode();
        node2.Name = "abc3";
        node2.Text = "abc3";
        node2.ImageIndex = 0;
        myTree.Nodes.Add(node2);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { myTree });

    }
}

The result still not work, It’s still have a folder icon before the text!
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:37:44+00:00Added an answer on June 7, 2026 at 9:37 pm

    You can’t do what you require

    "All I want is don't show icon at child node."
    

    without overriding the control. I also have found that you cannot display different images for different nodes with the standard WinForms TreeView. Below is some code that will make the TreeView look better; that is will draw the small section of the tree line for the sub-nodes.

    class CustomTreeView : TreeView
    {
        public const int NOIMAGE = -1;
    
        public CustomTreeView()
            : base()
        {
            // .NET Bug: Unless LineColor is set, Win32 treeview returns -1 (default), .NET returns Color.Black!
            base.LineColor = SystemColors.GrayText;
            base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
        }
    
        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            // Space between Image and Label.
            const int SPACE_IL = 3;  
    
            // We only do additional drawing.
            e.DrawDefault = true;
            base.OnDrawNode(e);
            if (base.ShowLines && base.ImageList != null && e.Node.ImageIndex == NOIMAGE
                // exclude root nodes, if root lines are disabled
                //&& (base.ShowRootLines || e.Node.Level > 0))
                    )
            {
                // Using lines & images, but this node has none: fill up missing treelines
    
                // Image size
                int imgW = base.ImageList.ImageSize.Width;
                int imgH = base.ImageList.ImageSize.Height;
    
                // Image center
                int xPos = e.Node.Bounds.Left - SPACE_IL - imgW / 2;
                int yPos = (e.Node.Bounds.Top + e.Node.Bounds.Bottom) / 2;
    
                // Image rect
                Rectangle imgRect = new Rectangle(xPos, yPos, 0, 0);
                imgRect.Inflate(imgW / 2, imgH / 2);
    
                using (Pen p = new Pen(base.LineColor, 1))
                {
                    p.DashStyle = DashStyle.Dot;
    
                    // Account uneven Indent for both lines.
                    p.DashOffset = base.Indent % 2;
    
                    // Horizontal treeline across width of image
                    // account uneven half of delta ItemHeight & ImageHeight.
                    int yHor = yPos + ((base.ItemHeight - imgRect.Height) / 2) % 2;
    
                    //if (base.ShowRootLines || e.Node.Level > 0)
                    //{
                    //    e.Graphics.DrawLine(p, imgRect.Left, yHor, imgRect.Right, yHor);
                    //}
                    //else
                    //{
                    //    // for root nodes, if root lines are disabled, start at center
                    //    e.Graphics.DrawLine(p, xPos - (int)p.DashOffset, yHor, imgRect.Right, yHor);
                    //}
    
                    e.Graphics.DrawLine(p,
                            (base.ShowRootLines || e.Node.Level > 0) ? imgRect.Left : xPos - (int)p.DashOffset,
                            yHor, imgRect.Right, yHor);
                    if (!base.CheckBoxes && e.Node.IsExpanded)
                    {
                        // Vertical treeline , offspring from NodeImage center to e.Node.Bounds.Bottom
                        // yStartPos: account uneven Indent and uneven half of delta ItemHeight & ImageHeight
                        int yVer = yHor + (int)p.DashOffset;
                        e.Graphics.DrawLine(p, xPos, yVer, xPos, e.Node.Bounds.Bottom);
                    }
                }
            }
        }
    
        protected override void OnAfterCollapse(TreeViewEventArgs e)
        {
            base.OnAfterCollapse(e);
            if (!base.CheckBoxes && base.ImageList != null && e.Node.ImageIndex == NOIMAGE)
            {
                // DrawNode event not raised: redraw node with collapsed treeline
                base.Invalidate(e.Node.Bounds);
            }
        }
    }
    

    This will give you a TreeView that looks like:

    CustomTreeView

    Here my master node (Node[0]) is the one without a specified Image and is what you require for your File1/File2 nodes.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TreeView with binded ItemsSource. I want to select my root(first) node.
I am creating a treeview which wil have folder images for directories and want
I have a treeview which contains, per node, a key and text. However, there
I have a treeView , and i want to know if some node can
I have a TreeView control which displays two things: 1) Folder 2) Item Where
I have treeview and 2 imagelist first image list contains images only 42x42 and
I have a custom TreeView for which I'd like to hide the ImageList dropdown
I have a TreeView windows forms control with an ImageList , and I want
I have treeview control with one level of parent and child nodes, each node
I have a treeview (winforms) which have different item types on it. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.