![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
A Demonstration I could go on and on explaining various details of how the TreeView control works, but I think the best way to explain it is to show you. We will create a simple program that demonstrates the most important features of the TreeView control. Start a Standard EXE project. Place the following controls on the form, leaving all properties at their default values except for the ones listed:
Note that you must set the Index property of the Text Box control to 0 to designate it as a control array. Even though the array has only this one member at design time, you can add array members at runtime. This would not be possible if the Index property were left at its default blank value. Arrange the controls as shown in Figure 7.1. Next, use the Add Form command on the Project menu to add a new form to the project. Assign this form the Name property of frmAddNode and the Caption property Add a Node. Put the following controls on the form:
Next, you need to draw two Option Buttons on each of the Frame controls. Remember, you must draw each Option Button within the borders of the Frame control, so they can be treated as a group. On the Frame labeled Node level, put these two Option Buttons:
Then, on the Frame labeled Node type, put these two Option Buttons:
Your finished form will look like Figure 7.2. Before we turn our attention to the code, lets save the project. I used the names TreeView1.frm and frmAddNode.frm for the form files, and Treeview for the project file. The code for this project is presented in Listings 7.1 (code in TreeView1.frm) and 7.2 (code in frmAddNode.frm). I am not going to hold your hand through all the details of adding code, as I am sure you know the required procedures by now. Furthermore, you should be able to figure out how the code works by looking at the comment lines.
Listing 7.1 Code in TreeView1.frm, the main form in the TreeView demonstration program. Option Explicit Global variable to hold the Index property of the currently selected node. Dim CurrentNode As Integer Private Sub cmdAddNode_Click() Dim newNode As Node Dim nodeType As Integer Dim relation As Integer Dim picture As String Display the add a node form. frmAddNode.Show 1 If user selected Cancel, don't do anything. If frmAddNode.txtNodeName.Text = Then Exit Sub Set picture type for document or folder. If frmAddNode.optDocument = True Then picture = leaf Else picture = closed End If Specify child node or sibling node. If frmAddNode.optSibling.Value = True Then relation = tvwNext Else relation = tvwChild End If Be sure the current node is expanded. TV1.SelectedItem.Expanded = True Create the new node. Set newNode = TV1.Nodes.Add( _ TV1.SelectedItem.Index, _ relation, , _ frmAddNode.txtNodeName.Text, _ picture) Deselect the current node and select the new node. TV1.SelectedItem.Selected = False TV1.Nodes(newNode.Index).Selected = True CurrentNode = newNode.Index If (TV1.Nodes.Item(CurrentNode).Tag = doc) Then Text1(CurrentNode).Visible = False End If Unhighlight the highlighted node (if any) and highlight the new node. Set TV1.DropHighlight = Nothing Set TV1.DropHighlight = newNode If the new node is a document node, create its Text Box and make it visible. If frmAddNode.optDocument = True Then newNode.Tag = doc Load Text1(newNode.Index) Text1(newNode.Index).Text = Str$(newNode.Index) Text1(newNode.Index).Visible = True Else newNode.Tag = folder End If End Sub Private Sub Form_Load() Dim img As ListImage Dim s As String Dim newNode As Node Initialize current node pointer. CurrentNode = -1 Load the ImageList control with bitmaps. Change the path to the bitmap files if necessary. This code assumes they are in the same folder as the application. Set img = ImageList1.ListImages.Add _ (, open, LoadPicture _ (App.Path & \open.bmp)) Set img = ImageList1.ListImages.Add _ (, closed, LoadPicture _ (App.Path & \closed.bmp)) Set img = ImageList1.ListImages.Add _ (, leaf, LoadPicture _ (App.Path & \leaf.bmp)) Clear the TreeView control. TV1.Nodes.Clear Set Treeview control properties. TV1.ImageList = ImageList1 TV1.Style = tvwTreelinesPlusMinusPictureText TV1.LineStyle = tvwTreeLines Get top node name. s = InputBox(Name for top-level node?) If s = Then End Create Node object. Add the first node. Set newNode = TV1.Nodes.Add(, , , s, closed) newNode.Selected = True CurrentNode = newNode.Index Set TV1.DropHighlight = newNode newNode.Tag = folder End Sub Private Sub TV1_NodeClick(ByVal Node As Node) Called when the user clicks a node. If the previous node was a document, hide its Text Box. If TV1.Nodes.Item(CurrentNode).Tag = doc Then Text1(CurrentNode).Visible = False End If CurrentNode = Node.Index If the newly selected node is a document, show its Text Box. If Node.Tag = doc Then Text1(Node.Index).Visible = True End If Highlight the newly selected node. Set TV1.DropHighlight = Nothing Set TV1.DropHighlight = Node End Sub Listing 7.2 Code in frmAddNode.frm, the second form in the TreeView demonstration program. Private Sub Command1_Click(Index As Integer) Select Case Index Case 0: OK button If txtNodeName.Text = Then MsgBox (You must enter a node name!) txtNodeName.SetFocus Exit Sub Else Hide End If Case 1: Cancel button txtNodeName.Text = Hide End Select End Sub
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |