- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary
THE TREEVIEW CONTROL 747
To add an image to the collection, click the Add button. You’ll be prompted to select an image file through the Open File dialog box. Each image you select is added to the list. When you select an image in this list, the properties of the image are displayed in the same window—but you can’t change the properties. One property of the ImageList control you can set in the Properties window is the TransparentColor property, which is a color that will be treated as transparent for all images (this is also known as the key color).
Each image stored in the ImageList control can be identified by an Index value, which is the order in which the image was added to the Images collection. You’ll see later how to assign an image to an item of the TreeView or ListView control.
The other method of adding images to an ImageList control is to call the Add method of the Images collection, which contains all the images stored in the control. The Images collection provides the usual Add and Remove methods. To add an image at runtime, you must first create an Image object with the image (or icon) you want to add to the control and then call the Add method as follows:
ImageList1.Images.Add(image)
where image is an Image object with the desired image. You will usually call this method as follows:
ImageList1.Images.Add(Image.FromFile(path))
where path is the full path of the file with the image. Images is a collection of Image objects, not the files where the pictures are stored. This means that the image files need not reside on the computer on which the application will be executed. This is another good reason to store your images in an ImageList control, instead of copying them along with the executables.
The TreeView Control
Let’s start our discussion with a few simple properties that you can set at design time. To experiment with the properties discussed in this section, open the TreeViewDemo project in this chapter’s folder on the CD. The project’s main form is shown in Figure 16.6. After setting some properties (they are discussed next), run the project and click the Populate button to populate the control. After that, you can click the other buttons to see the effect of the various property settings on the control. We’ll discuss the other buttons on the form in the following sections.
CheckBoxes If this option is enabled, a check box appears in front of each item. If the control displays check boxes, you can also select multiple items. If not, you’re limited to a single selection.
FullRowSelect This True/False value determines whether the entire row of the selected item will be highlighted and whether an item will be selected even if the user clicks outside the item’s text.
HideSelection This property determines whether the selected item will remain highlighted when the focus is moved to another control.
HotTracking This True/False value determines whether items are highlighted as the pointer hovers over them. When this property is True, the TreeView control behaves like a Web document with the items acting as hyperlinks—they turn blue while the pointer hovers over them.
However, you can’t capture this action from within your code. There’s no event to report that the pointer is hovering over an item.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
748 Chapter 16 THE TREEVIEW AND LISTVIEW CONTROLS
Figure 16.6
The TreeViewDemo project demonstrates the basic properties and methods of the TreeView control.
Indent This property indicates the indentation level in pixels. The same indentation applies to all levels of the tree—each level is indented by the same amount of pixels with respect to its parent level.
ShowLines The ShowLines property is a True/False value that determines whether the items on the control will be connected to their parent items with lines. The lines connect each node to its parent node, and it’s customary to display them.
ShowPlusMinus The ShowPlusMinus property is a True/False value that determines whether the plus/minus button is shown next to tree nodes that have children. The plus button is displayed when the node is collapsed, and it causes the node to expand when clicked. Likewise, the minus sign is displayed when the node is expanded and causes the node to collapse when clicked.
ShowRootLines This is another True/False property that determines whether there will be lines between each node and root of the tree view. Experiment with the ShowLines and ShowRootLines properties to find out how they affect the appearance of the control.
Sorted This property determines whether the items in the control will be automatically sorted or not. The control sorts each level of nodes separately. In our globe example, it will sort the continents, then the countries within each continent, and then the cities within each country. Once the control has been sorted, you can’t undo the operation. The existing items will remain sorted with respect to one another. You can turn off the Sorted property and insert more items. The new items will not be sorted.
Text This is the text of the currently selected node. Use this property to retrieve the user’s selection.
TopNode This is the first visible node in the TreeView control. It’s the first node in the control if its contents haven’t been scrolled, but it can be any node, at any level, if the control has been scrolled. The TreeNode property returns a TreeNode object, which you can use to manipulate the node from within your code.
VisibleCount This property returns the number of items that are visible on the control.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE TREEVIEW CONTROL 749
Adding New Items at Design Time
Let’s look now at the process of populating the TreeView control. Adding an initial collection of items to a TreeView control is trivial. Locate the Nodes property in the Property box, and you’ll see that its value is Collection. This string simply indicates that the control’s items form a collection. To add items, click the button with the ellipses, and the TreeView item editor window will appear, as shown in Figure 16.7. To add the root item, just click the Add Root button. The new item will be named Node0 by default. You can change its name by selecting the item in the list. When its name appears in the Label box, change the item’s name to anything you like.
Figure 16.7
The TreeNode
Editor window
You can add items at the same level as the selected one by clicking the Add Root button, or you can add items under the selected node by clicking the Add Child button. The Add Child button adds a new node under the selected node. Follow these steps to enter the root node GLOBE, a child node for Europe, and two more nodes under Europe: Germany and Italy. I’m assuming that you’re starting with a clean control. If your TreeView control contains any items, clear them all by selecting one item at a time in the list and clicking the Delete button.
Click the Add Root button first. A new node will be added automatically to the list of nodes, and it will be named Node0. Select it with the mouse, and its name will appear in the Label box. Here you can change its name to GLOBE.
Then click the Add Child button, which will add a new node under the GLOBAL root node. Select it with the mouse as before, and change its name to Europe. Then select the newly added node in the list and click the Add Child button again. Name the new node Germany. You’ve successfully added a small hierarchy of nodes. To add another node under Europe, select the Europe node in the list and click the Add Child button again. Name the new button Italy. Continue adding a few cities under each country. It’s likely that you will add child nodes under the wrong parent. This will happen if you forget to select the proper parent node before clicking the Add Child button. Each new node you add in the Editor’s window isn’t selected automatically. You must switch to the list of nodes and select the proper parent node with the mouse.
Note that when a node is deleted, all the nodes under it are deleted, too. Moreover, this action can’t be undone. So, be careful when deleting nodes.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
750 Chapter 16 THE TREEVIEW AND LISTVIEW CONTROLS
Click the OK button to close the Editor’s window and return to your form. The nodes you’ve added to the TreeView control are there, but they’re collapsed. Only the root nodes are displayed with the plus sign in front of their names. Click the plus sign to expand the tree and see its child nodes. The TreeView control behaves the same at design time as it does at runtime—as far as navigating the tree goes, at least.
The nodes added to a TreeView control at design time will appear each time the form is initialized. You can add new nodes through your code, and you will see how this is done in the following section.
Adding New Items at Runtime
Adding items to the control at runtime is a bit more involved. You’ve read about the tree structure; now you’ll learn how this structure is exposed by the object model of the TreeView control. All the items belong to the control’s Nodes collection, which is made up of TreeNode objects. To access the Nodes collection, use the following expression, where TreeView1 is the control’s name and Nodes is a collection of TreeNode objects:
TreeView1.Nodes
This expression returns a collection of TreeNode objects, which is called TreeNodeCollection, and it exposes the proper members for accessing and manipulating the individual nodes. The control’s Nodes property is the collection of all root nodes.
To access the first item, use the expression TreeView.Nodes(0) (this is the GLOBE node in our example). The Text property returns the item’s value, which is a string. TreeView1.Nodes(0).Text is the value of the first item on the control. The second item on the same level is TreeView1.Nodes(1).Text, and so on. The Text property is the string you’ve added to the control at design time. If you use the TreeView control to store objects, then the object’s ToString property will be displayed at each node.
The following statements will print the strings shown below them in bold (these strings are not part of the statements; they’re the output the statements produce).
Console.WriteLine(TreeView1.Nodes(0).Text)
GLOBE
Console.WriteLine(TreeView1.Nodes(0).Nodes(0).Text)
Europe
Console.WriteLine(TreeView1.Nodes(0).Nodes(0).Nodes(1).Text)
Italy
Let’s take a closer look at these expressions. TreeView1.Nodes(0) is the first root node, the GLOBE node. Under this node, there’s a collection of nodes, the TreeView1.Nodes(0).Nodes collection. Each node in this collection is a continent name. The first node in this collection is Europe, and you can access it with the expression TreeView1.Nodes(0).Nodes(0). Likewise, this node has its own Nodes collection, which are the countries under the specific continent.
Note Notice that a TreeView control contains many Nodes collections. The TreeView.Nodes collection represents all the root nodes. Each node in this collection has its own Nodes collection, which represents the child nodes under the root. Each one of these nodes may have its own Nodes collection, and so on, up to any level.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE TREEVIEW CONTROL 751
The Nodes.Add Method
The Add method adds a new node to the Nodes collection. The Add method accepts as an argument a string or a TreeNode object. The simplest form of the Add method is
newNode = Nodes.Add(nodeCaption)
where nodeCaption is a string that will be displayed on the control (you can’t add objects to the TreeView control). Another form of the Add method allows you to add a TreeNode object directly:
newNode = Nodes.Add(nodeObj)
To use this form of the method, you must first declare and initialize a TreeNode object:
Dim nodeObj As New TreeNode nodeObj.Text = “Tree Node” nodeObj.ForeColor = Color.BlueViolet TreeView1.Nodes.Add(nodeObj)
The TreeNode object exposes a number of properties for setting its appearance. You can change its foreground and background colors, the image to be displayed in front of the node (ImageIndex property), the image to be displayed in front of the node when the node is selected (SelectedImageIndex property), and more, including the NodeFont property. You will see shortly how to assign images to the nodes of a TreeView control.
The last overloaded form of the Add method allows you to specify the index in the current Nodes collection, where the node will be added:
newNode = Nodes.Add(index, nodeObj)
The nodeObj Node object must be initialized as usual. The Add method inserts the new node into the current Nodes collection.
Note The Add method is a method of the Nodes collection, not of the TreeView control. You can’t apply the Add method to the control.
If you call the Add method on the TreeView1.Nodes collection, as we’ve done in the last few examples, you’ll add a root item. If you call it on a child’s Nodes collection, you’ll add another item to the existing collection of child items. If your control contains a root item already, then this item is given by the expression
TreeView1.Nodes(0)
To add a child node to the root node, use a statement like the following:
TreeView1.Nodes(0).Nodes.Add(“Asia”)
The expression TreeView1.Nodes(0) is the first root node. Its Nodes property represents the nodes under the root node, and the Add method of the Nodes property adds a new node to this collection.
To add another element on the same level as the previous one, just use the same statement with a different argument. To add a country under Asia, use a statement like the following:
TreeView1.Nodes(0).Nodes(1).Nodes.Add(“Japan”)
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
752 Chapter 16 THE TREEVIEW AND LISTVIEW CONTROLS
This can get quite complicated, as you can understand. The proper way to add child items to a node is to create a TreeNode variable that represents the parent node, under which the child nodes will be added. The ContinentNode variable, for example, represents the node Europe:
Dim ContinentNode As TreeNode
ContinentNode = TreeView1.Nodes(0).Nodes(0)
The expression TreeView1.Nodes(0) is the first root node. The property Nodes(0) is the third child of the previous node (in our case, the Europe node). Then, you can add child nodes to the
ContinentNode node:
ContinentNode.Nodes.Add(“France”)
ContinentNode.Nodes.Add(“Germany”)
To add yet another level of nodes, the city nodes, create a new variable that represents the country of the city. The Add method actually returns a TreeNode object, so you can add a country and a few cities with the following statements:
Dim CountryNode As TreeNode
CountryNode = ContinentNode.Nodes.Add(“Germany”)
CountryNode.Nodes.Add(“Berlin”)
CountryNode.Nodes.Add(“Frankfurt”)
Then, you can continue adding countries through the ContinentNode variable:
CountryNode = ContinentNode.Nodes.Add(“Italy”)
CountryNode.Nodes.Add(“Rome”)
VB6 VB.NET
A feature of the old TreeView control many of you will miss is the Keys property. Because there’s no Keys property, the new Add method doesn’t support relations. With the VB6 version of the control, you could add nodes in any order. All you had to do was to specify a relation between the new node and an existing node. This relation was usually a parent/child relation, which would make the new node a child node of the specified parent node. Without the Keys property, you can’t uniquely identify the parent node in the tree.
The Count Property
This property returns the number of nodes in the Nodes collection. Again, this is not the total number of nodes in the control, just the number of nodes in the current Nodes collection. The expression
TreeView1.Nodes.Count
returns the number of all nodes in the first level of the control. In the case of the Globe example, it returns the value 1. The expression
TreeView1.Nodes(0).Nodes.Count
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
THE TREEVIEW CONTROL 753
returns the number of continents in the Globe example. Again, you can simplify this expression with an intermediate TreeNode object:
Dim Continents As TreeNode Continents = TreeView1.Nodes(0)
Console.WriteLine(“There are “ & Continents.Nodes.Count.ToString & _ “ continents on the control”)
The Clear Method
The Clear method removes all the child nodes from the current node. If you apply this method to the control’s root node, it will clear the control. To remove all the countries under the Germany node, use a statement like the following:
TreeView1.Nodes(0).Nodes(2).Nodes(1).Nodes.Clear
This example assumes that the third node under Globe corresponds to Europe and the second node under Europe corresponds to Germany.
The Item Property
The Item property retrieves a node specified by an index value. The expression
Nodes.Item(1)
is equivalent to the expression
Nodes(1)
The Remove Method
The Remove method removes a node from the Nodes collection. Its syntax is
Nodes.Remove(index)
where index is the order of the node in the current Nodes collection. To remove the selected node, call the Remove method on the SelectedNode object without arguments:
TreeView1.SelectedNode.Remove
Or you can apply the Remove method to a TreeNode object that represents the node you want to remove:
Dim Node As TreeNode
Node = TreeView1.Nodes(0).Nodes(7)
Node.Remove
The FirstNode, NextNode, PrevNode, and LastNode Properties
These four properties allow you to retrieve any node at the current segment of the tree. Let’s say the current node is the Germany node. The FirstNode property will return the first city under Germany (the first node in the current segment of the tree) and the LastNode will return the last city under
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
