Using a TreeView
If, however, you choose to use a TreeView control to display the data you select, you will find that there is no convenient interface that you can use to keep your code from changing.
For each node you want to create, you create an instance of the TreeNode class and add the root TreeNode collection to another node. In our example version using the TreeView, we'll add the swimmer's name to the root node collection and the swimmer's time as a subsidiary node. Here is the entire TreeAdapter class.
public class TreeAdapter:LstAdapter { private TreeView tree; //------ public TreeAdapter(TreeView tr) { tree=tr; } //------ public void Add(Swimmer sw) { TreeNode nod; //add a root node nod = tree.Nodes.Add(sw.getName()); //add a child node to it nod.Nodes.Add(sw.getTime().ToString ()); tree.ExpandAll (); } //------ public int SelectedIndex() { return tree.SelectedNode.Index ; } //------ public void Clear() { TreeNode nod; for (int i=0; i< tree.Nodes.Count ; i++) { nod = tree.Nodes [i]; nod.Remove (); } } //------ public void clearSelection() {} }
The TreeDemo program is shown in Figure 14-3.
Figure 14-3 The TreeDemo program