VIEWING DATA FILE MANAGER STYLE WITH THE TREEVIEW CONTROL

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.

Figure 14.22. The ImageList control is again used for the TreeView control to store the bitmaps displayed.


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.”)

Figure 14.23. The highlighted image is used for the expanded node.


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.

Figure 14.24. You can control how the TreeView looks by setting these properties.


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).

Listing 14.15. Chap14.mdb: Populating the TreeView Control
Private Sub Form_Load()

   Dim objCurrNode As Node

   Dim dbLocal As Database
   Dim dynCategories As Recordset, dynMovies As Recordset

   '-- Open the necessary recordset
   Set dbLocal = CurrentDb()
   Set dynCategories = dbLocal.OpenRecordset("Categories", dbOpenDynaset)
   Set dynMovies = dbLocal.OpenRecordset("qryMoviesByCategories", _
                     dbOpenDynaset)

   '-- Loop through the categories
   Do Until dynCategories.EOF

      '-- Add a top node (Category)
      Set objCurrNode = Me!ocxTreeView.Nodes.Add(, , _
               "Category" & CStr(dynCategories!CategoryCode), _
                                     dynCategories!Description, 1)

      '-- Establish which image to use for an expanded branch
      objCurrNode.ExpandedImage = 2

      If Not dynMovies.EOF Then
         '-- Add a branch to the current top node (Category)
         Do While dynMovies!CategoryCode = dynCategories!CategoryCode

            Set objCurrNode = Me!ocxTreeView.Nodes.Add( _
                  "Category" & CStr(dynCategories!CategoryCode), _
                      tvwChild, , dynMovies!Title, 3)

            dynMovies.MoveNext

            If dynMovies.EOF Then
               Exit Do
            End If

         Loop
      End If
      dynCategories.MoveNext
   Loop

   Me!ocxTreeView.Refresh

End Sub
					

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:

1.
Open the necessary recordsets.

2.
Loop through the Categories recordset. This is the top node in the TreeView.

3.
For each category, add a top node. This is performed with the Add method off the Nodes collection. The 1 in the Add method means it's a parent node:

Set objCurrNode = Me!ocxTreeView.Nodes.Add(, , _
         "Category" & CStr(dynCategories!CategoryCode), _
                               dynCategories!Description, 1)

4.
Establish which image to use for an expanded branch with the statement

objCurrNode.ExpandedImage = 2

5.
Add the branches to the current top node. The movies are looped through according to their category. The Add method is again used with a 3, which tells Access this is a leaf, or child node:

Do While dynMovies!CategoryCode = dynCategories!CategoryCode

   Set objCurrNode = Me!ocxTreeView.Nodes.Add( _
         "Category" & CStr(dynCategories!CategoryCode), _
             tvwChild, , dynMovies!Title, 3)

6.
Refresh the TreeView control.

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.)


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.166.98