This will draw the TreeNode
text where the image should have been, getting rid of the white space.
You'll need to set the TreeView
's DrawMode
property to OwnerDrawText
. You can find the DrawMode
property in the properties panel.
Next when you add a node, set it's ImageIndex
and SelectedImageIndex
greater than the value of your yourImageListName.Images.Count
value. This is so no image will be drawn, but there will still be that white space you don't want.
Now to get rid the white space. Add a handle for the treeviews DrawNode
event. This can be done by going to the treeviews property panel and clicking the Icon in the panel that looks like a lighting bolt, then scroll till you see the text DrawNode
, double click it.
Now you just copy and paste this into the created method
if (e.Node.ImageIndex >= e.Node.TreeView.ImageList.Images.Count) // if there is no image
{
int imagewidths = e.Node.TreeView.ImageList.ImageSize.Width;
int textheight = TextRenderer.MeasureText(e.Node.Text, e.Node.NodeFont).Height;
int x = e.Node.Bounds.Left - 3 - imagewidths / 2;
int y = (e.Bounds.Top + e.Bounds.Bottom) / 2+1;
Point point = new Point(x - imagewidths/2, y - textheight/2); // the new location for the text to be drawn
TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.NodeFont, point, e.Node.ForeColor);
}
else // drawn at the default location
TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.TreeView.Font, e.Bounds, e.Node.ForeColor);