RSS

Treeview Control – chek child if parent is checked for C#

27 May
private void TreeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
	// For Select all to sub tree
	TreeNode parent_node = e.Node;
	bool is_checked = parent_node.Checked;
	for (int i = 0; i <= e.Node.Nodes.Count - 1; i++) {
		SetSubtreeChecked(parent_node.Nodes(i), is_checked);
	}
	bool blnUncheck = false;

	//---------------------------------------------------

	//Check to see if a parent node exists.

	if ((e.Node.Parent != null)) {
		//Loop through the child nodes.
		foreach (TreeNode child in e.Node.Parent.Nodes) {
			//Check to see if the current node is unchecked.
			if (child.Checked == true) {
				//Set the variable.
				blnUncheck = true;
			}
		}
		//Check the variable.
		if (blnUncheck == false) {
			//Check the parent node.
			e.Node.Parent.Checked = false;
		} else {
			//Uncheck the parent node.
			e.Node.Parent.Checked = true;
		}
	}
}
//'========================================================
//'          For Tree view Items Validation
//'========================================================
private void SetSubtreeChecked(TreeNode parent_node, bool is_checked)
{
	// Set the parent's Checked value.
	parent_node.Checked = is_checked;
	// Set the child nodes' Checked values.
	for (int i = 0; i <= parent_node.Nodes.Count - 1; i++) {
		SetSubtreeChecked(parent_node.Nodes(i), is_checked);
	}
}

Referenced by: http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/f4269ae8-f788-4637-a454-df0f6bf05db5/

Advertisements
 
Leave a comment

Posted by on May 27, 2011 in C#, Window Application

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: