How to traverse and find the specified CheckBox status in the Windows Form Control TreeView

Source: Internet
Author: User

As a common Control, the TreeView Control gives us a lot of capabilities to present hierarchical data sources. Its powerful customization and ease of use are very popular with client application developers. However, when displaying the status of the CheckBox control in front of the tree control, we sometimes need to traverse the entire tree to get the setting status of all checkpoints.

Before using the sample code, we will first demonstrate the relevant types:

1. TreeView class

Public classTreeView: Control {

// Set the events triggered between and before the CheckBox is selected in the tree

       public event TreeViewEventHandler AfterCheck;
       public event TreeViewCancelEventHandler BeforeCheck;
// Set the CheckBoxes enabling status, code-level use
       public bool CheckBoxes { get; set; }
// Obtain the parent node at the highest level of the current child node
       public TreeNode TopNode { get; set; }

}

2. TreeNode class:

 public class TreeNode : MarshalByRefObject, ICloneable, ISerializable{
// CheckBox status of the current node
   public bool Checked { get; set; }
// Additional special information
   public object Tag { get; set; }
// Traverse the attributes of the node linked list
  public TreeNode PrevNode { get; }
  public TreeNode FirstNode { get; }
  public TreeNode NextNode { get; }

}

Now we can operate the CheckBox in the TreeView through the relevant attributes and methods provided by the above two classes.

Intent:

We need to obtain information about all the selected checkboxes. For convenience, Tag is used in the TreeView node to append the CheckBox ID information.

Key steps:

1,Add an AfterCheck event to set the CheckBox status of the TreeNode

If you select the CheckBox, set the TreeNode Tag to "True". Otherwise, set it to null. The Code is as follows:

 Private void nodeTree_AfterCheck (object sender, TreeViewEventArgs e ){
TreeNode node = e. Node;

If (node. Tag = null)
Node. Tag = tag; // additional node Information
Else

Node. Tag = null;

}

2,Search for the root node of the TreeView

Because the TopNode attribute of the TreeView can obtain the top-level node of the selected node, we can use it to traverse forward and get the root node. The Code is as follows:

 TreeNode node = nodeTree. TopNode; // obtain the root node of the TreeView. Note that there is only one root node.

// Each time the root node is searched

While (node. PrevNode! = Null)

Node = node. PrevNode;

 

3,Traverse all Tree nodes to get the corresponding status

For the convenience of the example, we didn't use the method of getting data from the database, but we used the built-in XML data source, and we presented the result to the user in the form of MessageBox to display the status. [The Tree recursion algorithm is used here] the relevant code is as follows:

Traversal method

 Private void TraversNodes (TreeNode parent ){

TreeNode node = parent;

If (node! = Null ){

If (node. Tag! = Null & node. Tag. ToString () = tag)

NodesTag. AppendFormat ("node: {0} has checked \ n", node. Name );

// Recursively traverse the tree

If (node. FirstNode! = Null ){

TraversNodes (node. FirstNode );

}

If (node. NextNode! = Null ){

TraversNodes (node. NextNode );

}

}

}

Result

 

Tip:

Here is just a relatively simple example of the traversal algorithm of the built-in control in TreeView. Because no relevant information is found on the Internet, it is written to a friend who needs it for reference. Sample Code

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.