Windows Develop Bookmark and Share   
 index > Windows Forms General > Drawable Items in TreeView
 

Drawable Items in TreeView

I have a tree view that i want to show some nodes in it. how can i create nodes that can draw themselves without using imagelist in treeview?
Actually i dont want to use any image index and imagelist.
caligula  Tuesday, July 04, 2006 7:18 AM
The .NET 2.0 version of TreeView supports owner draw. See this article for the properties and events you'll need to use as well as an example.

nobugz  Tuesday, July 04, 2006 9:06 AM

First derive a treeview class from System.Windows.Forms.TreeView.

Please see the follwoing class.

public class MyTree : System.Windows.Forms.TreeView

{

public MyTree()

{

//Indicates that full drawing will be done by us.

this.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;

}

/// <summary>

/// Called when each node is drwn

/// </summary>

/// <param name="e"></param>

protected override void OnDrawNode(DrawTreeNodeEventArgs e)

{

Image im = (Image)e.Node.Tag;

e.Graphics.DrawImage(im,new Point(0,0));

}

}

The image which we have to draw is add as tag.

The code for adding the node will be as following.

Image im = Image.FromFile("C:\\SmallImage.JPG");

TreeNode node = new TreeNode();

node.Tag = im;

treeView1.Nodes.Add(node);

Nidheesh T.Mani  Tuesday, July 04, 2006 10:03 AM
The .NET 2.0 version of TreeView supports owner draw. See this article for the properties and events you'll need to use as well as an example.

nobugz  Tuesday, July 04, 2006 9:06 AM

First derive a treeview class from System.Windows.Forms.TreeView.

Please see the follwoing class.

public class MyTree : System.Windows.Forms.TreeView

{

public MyTree()

{

//Indicates that full drawing will be done by us.

this.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;

}

/// <summary>

/// Called when each node is drwn

/// </summary>

/// <param name="e"></param>

protected override void OnDrawNode(DrawTreeNodeEventArgs e)

{

Image im = (Image)e.Node.Tag;

e.Graphics.DrawImage(im,new Point(0,0));

}

}

The image which we have to draw is add as tag.

The code for adding the node will be as following.

Image im = Image.FromFile("C:\\SmallImage.JPG");

TreeNode node = new TreeNode();

node.Tag = im;

treeView1.Nodes.Add(node);

Nidheesh T.Mani  Tuesday, July 04, 2006 10:03 AM

yes , and if you just wanna draw the icons for the nodes and let the treeview do the left a couple of lines can be added to your code:

e.DrawDefault = true;

base.OnDrawNode(e);

thats it.

caligula  Tuesday, July 04, 2006 12:16 PM

You can use google to search for other answers

Custom Search

More Threads

• how to add an image to the cursor in win appl
• ListView images question
• How to display Access Key without press Alt key?
• Visual Studio 2008 on target framework 2.0 Issue
• How to capture lose focus event and reset focus
• WJView
• Casting! Impossible?
• Combobox???
• Working with the backgroundworker component.
• ToolStripItem Container