C # Perfect class for checkbox Selection in winform Treeview

Source: Internet
Author: User
Public   Static   Class Treeviewcheck { /// <Summary>      /// Series node checked attribute Control      /// </Summary>      /// <Param name = "E"> </param>      Public   Static   Void Checkcontrol (treevieweventargs e ){ If (E. Action! = Treeviewaction. Unknown ){ If (E. node! = Null &&! Convert. isdbnull (E. node) {checkparentnode (E. node ); If (E. node. nodes. Count> 0) {checkallchildnodes (E. node, E. node. Checked );}}}} # Region Private Method // Change the status of all subnodes      Private   Static   Void Checkallchildnodes (treenode Pn, Bool Ischecked ){ Foreach (Treenode TN In Pn. nodes) {tn. Checked = ischecked; If (TN. nodes. Count> 0) {checkallchildnodes (TN, ischecked );}}} // Change the selected status of the parent node. this parameter is used only when all child nodes are not selected. You can modify the selected status as needed.      Private   Static   Void Checkparentnode (treenode curnode ){ Bool Bchecked = False ; If (Curnode. parent! = Null ){ Foreach (Treenode Node In Curnode. Parent. nodes ){ If (Node. Checked) {bchecked = True ; Break ;}} If (Bchecked) {curnode. Parent. Checked = True ; Checkparentnode (curnode. Parent );} Else {Curnode. Parent. Checked = False ; Checkparentnode (curnode. Parent );}}} # Endregion }

Call Method: In the aftercheck event of Treeview

Private void treeviewappsaftercheck (Object sender, treevieweventargs E)

{

Treeviewcheck. checkcontrol (E)

}

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.