Skip to main content

DisplayTreeView: Part 1

The DisplayTreeView method creates the tree for the left hand side of the GUI. In this example (covering this page and the following page), you will add the code that creates the tree and adds the Name property for each Contact.

DisplayTreeView creates two level 1 nodes: BusinessNode and PersonalNode. For each Contact instance, DisplayTreeView creates a node containing the Name and ID values. The Name property is displayed by the node. The ID value is stored in the node's Tag property. DisplayTreeView then adds the node to the appropriate level 1 node depending upon the value of the Contact instance's ContactType property.

The first part of the method does the following:

  1. Declares the contactNode variable.

  2. Invokes BeginUpdate on treeView1. This prevents the display of the tree from being painted while the tree is being built or rebuilt.

  3. Creates the level 1 nodes PersonalNode and BusinessNode.

Here is the first part of the method. Add the body of the method to the DisplayTreeView stub in PhoneFormObj.cs.


private void DisplayTreeView() {
 TreeNode contactNode = null;
 treeView1.BeginUpdate();
 treeView1.Nodes.Clear();
 TreeNode PersonalNode = new TreeNode("Personal");
 TreeNode BusinessNode = new TreeNode("Business");  
 
 ...

Note:

The first exercise for Part II asks you to modify DisplayTreeView so that it displays the phone number information as well as the contact names.

FeedbackOpens in a new tab