Note the following when operating the node checkbox in the Treeview:

Source: Internet
Author: User

Recently, a user asked me to write a demo program to demonstrate how to select a node in the Treeview so that all its child nodes are selected. The parent node changes the status based on the changes of the current node. During implementation, I did this in the aftercheck event of the Treeview. However, if the program has been run, the program overflows. Take a closer look, it turns out that when the checked attribute of a node is modified, the aftercheck event of the Treeview is re-matched, resulting in a chain reaction. Through the event parameters, you can get the action attribute. After viewing msdn, you can find that you should judge the action in the event to avoid chain reactions.

 

The general method is as follows:

Private void trvdbbinding_aftercheck (Object sender, system. Windows. Forms. treevieweventargs E)

{

If (E. Action! = Treeviewaction. Unknown)

{

// Event call by mouse or key-Press

Setnodecheckstatus (E. node, E. node. Checked );

}

}

 

Private void setnodecheckstatus (treenode TN, bool checked)

{

If (Tn = NULL) return;

// Check children nodes

Foreach (treenode tnchild in TN. nodes)

{

Tnchild. Checked = checked;

Setnodecheckstatus (tnchild, checked );

}

 

// Set parent check status

Treenode tnparent = tn;

Int nnodecount = 0;

While (tnparent. parent! = NULL)

{

Tnparent = (treenode) (tnparent. Parent );

Nnodecount = 0;

Foreach (treenode tntemp in tnparent. nodes)

If (tntemp. Checked = checked)

Nnodecount ++;

If (nnodecount = tnparent. nodes. Count)

Tnparent. Checked = checked;

Else

Tnparent. Checked = false;

}

}

 

Similar events include aftercollapse, afterexpand, and afterselect. To avoid the chain reaction of the event, you must judge the action.

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.