TreeView Sample Applications
The following sample applications are included when you purchase the Visual Basic 6 Tutorial Download Package or the Visual Basic 6 "Extra" Sample Applications from The VB Programmer.
1. Basic TreeView Database Demo 1
This is a sample program that shows how data from related tables in a database would typically be displayed in a TreeView control. Salesmen are displayed in the high-level nodes of the tree. When a salesman node is expanded, the companies that the salesman is responsible for are displayed at the next level of the tree. When a company is clicked, data for the contacts associated with that company are displayed in the ListView in the lower portion of the screen. In this sample application, all of the data for the TreeView (the salesmen and their associated companies) is loaded at the same time (this in contrast to the technique used for loading the TreeView in the second demo, described below).
2. Basic TreeView Database Demo 2
This sample program behaves identically to the first demo, described above. The difference from a programming perspective is in the way that the TreeView is loaded. Here, only the salesmen nodes are loaded initially (each with a "dummy" child node below it so that the "+" appears on the left of the node). When a salesman node is expanded, a query is run to get the companies associated with that salesman (the company data is displayed as children nodes below the salesman node, and the "dummy" node is deleted). This "dynamic" loading technique is practical when dealing with large amounts of data, where it would take too long to fully load the tree all at once.
3. TreeView Drag-and-Drop (with Scrolling)
This sample application shows how to perform drag and drop (with scrolling) within a TreeView. The sample program shows salesmen with companies they are responsible for calling upon. A user (such as the sales manager) can re-assign a company to a different salesman by dragging the company node below one salesman and dropping it on another salesman. The changes that the user makes are saved back to the database.
4. Checkbox Bug and a Resolution
The TreeView has a Checkboxes property, which when True, displays a checkbox on each node, allowing the user an additional way to interact with the control. This is all well and good, but there are some "issues" with this feature of the TreeView.
Two issues are documented by Microsoft in these Knowledge-Base articles:
However, I had the pleasure of finding another problem which I have not seen documented anywhere. It has to do with pre-checking specific nodes of a TreeView before the form is displayed. For example, suppose I am reading in previously saved "yes/no" values from a file or database table and want to display these as checked in the appropriate nodes of the TreeView during the Form_Load event. This does not work as expected. The screen shot below shows the form immediately after the Form_Load event. In the Form_Load event, Node indexes 4, 6, 11, 12 were checked with code (i.e., their Checked property was set to True). As you can see, no nodes are checked:
The resolution was to move the code the checks the nodes to a Timer event that fires one quarter of a second after the Form_Load event completes. As you can see, the problem is resolved and the desired nodes are checked: