C #, winform, Treeview, select a node, select all the corresponding child nodes, cancel the node, and cancel the parent node. tips:

Source: Internet
Author: User

During development, the Treeview control is often used to display the organizational structure, directory structure, and attribute checkedboxs.

The following is an example of selecting and canceling a node. If a node has a subnode, you can select all the subnodes. If you cancel the selected status of a node, if both the current node and the parent node exist, the selection of the parent node will be canceled.

:

CodeAs follows:

Code
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;

namespace windowsapplication6
{< br> Public partial class treeviewtest: form
{< br> Public treeviewtest ()
{< br> initializecomponent ();
}

private void treeviewtest_load ( Object sender, eventargs e)
{< br> treeview1.expandall ();
}

Private VoidTreeviewappsaftercheck (ObjectSender, treevieweventargs E)
{

If (E. Action = Treeviewaction. bymouse)
{
Textbox1.text = E. node. text;
If (E. node. Checked)
{
// Cancels the selected status of all parent nodes.
Setchildnodecheckedstate (E. node, True );

}
Else
{
// Cancels the selected status of all parent nodes.
Setchildnodecheckedstate (E. node, False );
// If a parent node exists, deselect the selected status of the parent node.
If (E. node. Parent ! =   Null )
{
Setparentnodecheckedstate (E. node, False );
}
}
}
}
// Cancels the selected status of all parent nodes.
Private   Void Setparentnodecheckedstate (treenode currnode, Bool State)
{
Treenode parentnode = Currnode. parent;

Parentnode. Checked = State;
If (Currnode. Parent. Parent ! =   Null )
{
Setparentnodecheckedstate (currnode. Parent, State );
}
}
// After a node is selected, all the child nodes of the node are selected.
Private   Void Setchildnodecheckedstate (treenode currnode, Bool State)
{
Treenodecollection nodes = Currnode. nodes;
If (Nodes. Count > 0 )
Foreach (Treenode TN In Nodes)
{

Tn. Checked = State;
Setchildnodecheckedstate (TN, State );
}
}
}
}

Related Article

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.