The last control to examine is the TreeView control. This ActiveX control allows you to view your data in a manner similar to Windows Explorer, with the added feature of being able to include graphic images. An example of a TreeView control can be found on the WindowsCommonControlsTreeView form, which is in Chap14.mdb in the ExamplesChap14 folder on the accompanying CD-ROM. Figure 14.22 shows the WindowsCommonControlsTreeView form as a source object for the WindowsCommonControls form.
Notice in Figure 14.22 how the expanded category, called a node, is displaying a different image than an unexpanded one. This TreeView control uses three different images: unexpanded node, expanded node, and leaf node (sometimes called the bottom node). Figure 14.23 shows these images in the ImageList control called ocxImageList. (For more information on setting up the ImageList control, see the earlier section “Using the ImageList Control with the TabStrip Control.”)
After setting up the images, you need to set the ImageList property of the TreeView control to ocxListImageSub.
Note
You should call this ListImage control ocxListImageSub instead of just ocxListImage. Otherwise, Access gets confused if you have two ListImage controls open at the same time—even on two separate forms! (It's a problem because the WindowsCommonControlsTreeView form is used as a subform for the WindowsCommonControls form.) You can work around this by adding Sub to the subform ListImage control name.
In addition to setting up the ImageList control, you need to set the Style property so that it displays images with lines. Eight different styles are available:
Value | Description |
---|---|
0 | Text Only (default) |
1 | Picture, Text |
2 | Plus/Minus, Text |
3 | Plus/Minus, Picture, Text |
4 | Lines, Text |
5 | Lines, Picture, Text |
6 | Lines, Plus/Minus, Text |
7 | Lines, Plus/Minus, Picture, Text |
The style chosen for this example is 5 (Lines, Plus/Minus, Picture, Text). You can see Style 7, as well as other properties for the TreeView control, in Figure 14.24.
That's all you have to do to set up the TreeView for the current example on the WindowsCommonControlsTreeView form. The rest is performed in the following code, which is on the OnLoad event of the WindowsCommonControlsTreeView form (see Listing 14.15).
The code in Listing 14.15 actually takes the data from within tables and stores it into the nodes of the TreeView control. Like other ActiveX controls, the TreeView control uses a collection, called Nodes. You see the Nodes collection in the steps this code performs:
The TreeView control can use quite a few properties and methods through the Nodes collection and the control itself. This example just touched on some of the power of the TreeView control.
Tip
If performance becomes an issue when using the TreeView control, add nodes on lower levels just in time, as they're expanded, using the Expand event. You can add a dummy node at the higher level so that the little + shows up. (The plus sign doesn't show if there are zero child nodes.)
18.118.166.98