8.20 Selecting Items Using the PathSeparator Property

 <  Day Day Up  >  

8.20 Selecting Items Using the PathSeparator Property

You want to programmatically select an item in the TreeView by passing an item path whose items are delineated by the current PathSeparator property of the TreeView control.


Technique

Because a TreeView displays information in a hierarchical fashion, you can use a path-based syntax to manipulate the individual tree nodes. The PathSeparator property is a string value that separates root nodes and the corresponding child nodes. By default, the PathSeparator is set to the character ' \ '.

To programmatically select an item using path-based syntax, create a method that parses the path using the PathSeparator property to access individual nodes. If the PathSeparator is a single character, you can use the String.Split method, which conveniently places each node's string value into a string array. Enumerate this array and for each string select the corresponding TreeNode object, as shown in Listing 8.6.

Listing 8.6 Selecting a TreeNode Programmatically
 private void SelectTreeNode( TreeView tv, string path ) {     string[] nodes = path.Split( new char[]{tv.PathSeparator[0]} );     TreeNodeCollection curNodes = tv.Nodes;     foreach( string curNode in nodes )     {         foreach( TreeNode child in curNodes )         {             if( child.Text == curNode )             {                 child.EnsureVisible();                 tv.SelectedNode = child;                 curNodes = child.Nodes;                 break;             }         }     } } 

Comments

Data that is arranged hierarchically carries with it the distinct advantage of being able to reference individual items using path-based syntax. The Windows file system uses paths to access individual files and folders. The Registry uses a path to access individual keys, and Extensible Markup Language (XML) and Extensible Stylesheet Language Transformations (XSLT) can use XPath to access a single node, group of nodes, or attributes, for example. You divide individual items within a path-based syntax string using a path separator. In most cases, it is a single character such as ' \ '. The TreeView control utilizes the PathSeparator property exactly for this purpose.

Each item within a TreeView control contains a property named FullPath , which, when called, returns a string containing a list of ordered items beginning with the root object of the tree, all separated using the currently defined PathSeparator . Unfortunately, no other method or property defined in the TreeView class takes advantage of an items path, which leaves any implementation details up to you. In the code shown in the "Technique" section, you can see a method that uses a string path to programmatically select an item within a tree view. It first splits the path into several individual strings and places them into an array by calling the Split method defined in the string class. Doing so creates an array of items that corresponds to the order or navigation of a tree control, where the first item in the array corresponds to the root item of a tree, the second item in the array corresponds to a child of the previous root, and so on. When the method returns, the tree control is expanded and the last item in the string path is the currently selected TreeNode object.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net