How to create a TreeView node with no image?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a TreeView with an associted imagelist. I want some nodes to have no
icon. How do I do this? How can I create an "empty icon"?
 
Hi Juan,

When we associate an imagelist to a treeview, the default image index for
each treenode in the treeview is 0. If we want some treenodes to have no
image, we could set the ImageIndex and SelectedImageIndex properties of the
treenode to a value that is equal to or greater than the value of the
images count in the imagelist.

For example, if there're 10 images in the imagelist, we may set the
ImageIndex and SelectedImageIndex properties of a treenode to 10. Thus,
this treenode has no image associated with it any more.

However, when you try the above in your program, you will see a part of the
treeline of the treenode without an image is missing. This area of the
missing treeline was prepared for showing the image. Never mind, we could
draw the missing treeline by ourselves.

To draw the treeline by ourselves, we should set DrawMode property of the
treeview to TreeViewDrawMode.OwnerDrawAll and handle the DrawNode event. To
refresh the treeview properly when a treenode is collapsed, we need handle
the AfterCollapse event of the treeivew as well.

The following is a sample. This requires that a TreeView called treeView1
has been added on the form.

using System.Drawing.Drawing2D;
public Form1()
{
this.treeView1.LineColor = SystemColors.GrayText;
this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
this.treeView1.DrawNode += new
DrawTreeNodeEventHandler(treeView1_DrawNode);
this.treeView1.AfterCollapse += new
TreeViewEventHandler(treeView1_AfterCollapse);
}

void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
const int SPACE_IL = 3; // space between Image and Label

// we only do additional drawing
e.DrawDefault = true;

if (this.treeView1.ShowLines && this.imageList1 != null &&
e.Node.ImageIndex >= this.imageList1.Images.Count)
{
// Image size
int imgW = this.imageList1.ImageSize.Width;
int imgH = this.imageList1.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;

using (Pen p = new Pen(this.treeView1.LineColor, 1))
{
p.DashStyle = DashStyle.Dot;
// draw the horizontal missing treeline
e.Graphics.DrawLine(p, (xPos - imgW / 2), yPos, (xPos +
imgW / 2), yPos);

if (!this.treeView1.CheckBoxes && e.Node.IsExpanded)
{
// draw the vertical missing treeline
e.Graphics.DrawLine(p, xPos, yPos, xPos,( yPos +
imgW / 2));
}
}
}
}

void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
if (!this.treeView1.CheckBoxes && this.imageList1 != null &&
e.Node.ImageIndex >= this.imageList1.Images.Count)
{
this.treeView1.Invalidate(e.Node.Bounds);
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top