treeview identical to windows explorer tree

  • Thread starter Thread starter Steve Richter
  • Start date Start date
S

Steve Richter

in windows explorer, the nodes immed under the "my computer" root node
appear with a minimum of indenting ( the +/- square is directly
underneath the root node ). In the .NET TreeView control the indent
is shifted one more indent level to the right, wasting valuable
horizontal space.

How can I instruct TreeView to indent from the root node the same way
as windows explorer does?

thanks,

-Steve
 
To my knowledge... you can't. But you can fake it. And I
always say, where you can fake it.... er, uh... you can
fake it.

Set the TreeView's border to none. Add a label just above
the TreeView with the label's BackColor set to the same
as the TreeView's and turn it's border off as well. It
shouldn't be difficult to line up the Label just above
the TreeView and get it to look like it's part of the
same control. Then just wire up the label's click event
to refresh the TreeView and process anything else that
you want(like collapsing all nodes). If you want to
complete the illusion, put both the Label and TreeView on
a Panel control, set their docking and anchor properties
to extend to the edges of the panel, and turn the panel's
border property on.

It could fool even the best trained eye.

Good Luck,
Jacob
 
Hi Steve,

I think I have tried Jacob's way, it realy appears like the windows
explorer.
But I think you need add the click event to the label control(Such as
expand the treeview).
Also, you should make the user can select your root node(actually the
lable's text). I think this need hit test, then draw the back color of your
label text.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jacob said:
To my knowledge... you can't. But you can fake it. And I
always say, where you can fake it.... er, uh... you can
fake it.

Set the TreeView's border to none. Add a label just above
the TreeView with the label's BackColor set to the same
as the TreeView's and turn it's border off as well. It
shouldn't be difficult to line up the Label just above
the TreeView and get it to look like it's part of the
same control. Then just wire up the label's click event
to refresh the TreeView and process anything else that
you want(like collapsing all nodes). If you want to
complete the illusion, put both the Label and TreeView on
a Panel control, set their docking and anchor properties
to extend to the edges of the panel, and turn the panel's
border property on.

that looks neat Jacob, thanks.

Turns out I was making a mistake in which node I was adding the sub
nodes to. I was doing something like this:
TreeView tv = new TreeView( ... ) ;
TreeNode Desktop = new TreeNode( ... ) ;
tv.Nodes.Add( Desktop ) ;

// now I start to make my mistake. I add "My Documents" and "My
Computer"
// as sub nodes of the DesktopNode.
TreeNode MyDocs = new TreeNode( ... ) ;
Desktop.Nodes.Add( MyDocs ) ;
TreeNode MyComputer = new TreeNode( ... ) ;
Desktop.Nodes.Add( MyComputer ) ;

// the nodes line up better when "My Documents" and "My Computer" are
nodes
// of the TreeView the same as the "Desktop" node.
tv.Nodes.Add( MyDocs ) ;
tv.Nodes.Add( MyComputer ) ;

thanks for the help,

-Steve
 
Back
Top