Free C# Tutorials

home
Stay at Home and Learn

The TreeView Control in C# .NET

 
Computer Tutorials List

 

 

 

 

A useful control you can add to a form is the TreeView control. You've seen these plenty of times, but here's what they look like:

A TreeView

TreeViews have plus symbols that you can click. When you do, you expand that item (called a Node). The Node that's selected in the image above is the "My Computer" Node.

To add one to your form, expand the Toobox and locate TreeView, which is one of the Common Controls (we've chopped a few controls out, in the image below):

The TreeView Control

Draw one on your form, in Panel1. Resize it to fill the whole of Panel1, and your form will look something like this:

A TreeView Control on a C# Form

At the moment, though, you can move the TreeView. When you do, it will spill over to the other panel, and outside of the container itself. To stop this from happening, click on the TreeView control to select it. Now locate the Dock property. Click the down arrow to see the following:

Dock the TreeView Control

Click the big grey square in the middle of the Dock dropdown. This is the Fill option. It will Fill Panel1. Your form will then look like this when you run it:

What your Form should look like

We haven't added any nodes to the TreeView yet. Let's do that now.

 

<-- SplitContainer | Adding Nodes to a TreeView in C# -->

<--Back to the C# .NET Contents Page

View all our Home Study Computer Courses