none
how to check any of the parent node is checked in TreeView control

    Question

  • Hi,

        When I check/uncheck a node in a TreeView, I want to see whether any of the parent nodes in the heirarchy are checkd.  I want to check/uncheck parent nodes accordingly.  I looked so many forums but no answer.  Please help.

    Cris Evan.

     


    cris
    • Changed type cris.evan Sunday, December 26, 2010 3:20 PM
    • Changed type cris.evan Sunday, December 26, 2010 3:20 PM
    • Changed type cris.evan Sunday, December 26, 2010 3:21 PM
    Sunday, December 26, 2010 8:26 AM

Answers

  • here it is, try to incorporate it into Omie's solution to get what you need:

    c#

     

    	private void button1_Click(object sender, EventArgs e)
    	{
    		MessageBox.Show("Has checked child: " + hasCheckedChildNode(treeView1.SelectedNode, true));
    	}
    	private void button2_Click(object sender, EventArgs e)
    	{
    		MessageBox.Show("Has unchecked child: " + hasCheckedChildNode(treeView1.SelectedNode, false));
    	}
    
    	private bool hasCheckedChildNode(TreeNode node, bool isChecked)
    	{
    		for (int i = 0; i < node.Nodes.Count; i++)
    		{
    			TreeNode childNode = node.Nodes[i];
    			// stop if direct child is checked 
    			if (childNode.Checked == isChecked)
    				return true;
    
    			// go deep and check down the hierarchy
    			if (childNode.Nodes.Count > 0)
    			{
    				bool result = hasCheckedChildNode(childNode, isChecked);
    				if (result == true)
    					return true;
    			}
    		}
    
    		return false;
    	}
    

     

    VB.net:

     

    Private Sub button1_Click(sender As Object, e As EventArgs)
    	MessageBox.Show("Has checked child: " & hasCheckedChildNode(treeView1.SelectedNode, True))
    End Sub
    Private Sub button2_Click(sender As Object, e As EventArgs)
    	MessageBox.Show("Has unchecked child: " & hasCheckedChildNode(treeView1.SelectedNode, False))
    End Sub
    
    Private Function hasCheckedChildNode(node As TreeNode, isChecked As Boolean) As Boolean
    	For i As Integer = 0 To node.Nodes.Count - 1
    		Dim childNode As TreeNode = node.Nodes(i)
    		' stop if direct child is checked 
    		If childNode.Checked = isChecked Then
    			Return True
    		End If
    
    		' go deep and check down the hierarchy
    		If childNode.Nodes.Count > 0 Then
    			Dim result As Boolean = hasCheckedChildNode(childNode, isChecked)
    			If result = True Then
    				Return True
    			End If
    		End If
    	Next
    
    	Return False
    End Function
    
    
    best regards,
    Vladimir

     

    • Marked as answer by cris.evan Monday, December 27, 2010 9:13 AM
    Sunday, December 26, 2010 4:07 PM

All replies

  • this is a question, not a discussion..

     

    anyway,

    Where are you doing this check ? I suppose in NodeAfterCheck event handler.

    In that case, you could use argument e which is of type System.Windows.Forms.TreeViewEventArgs

    e.Node.Parent should return parent of currently clicked node.

    if you wish to check all the parents till root, you'll have to loop through parents, something like this :

        Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) _
            Handles TreeView1.AfterCheck

            If e.Node.Parent IsNot Nothing Then
               

                Dim temp As TreeNode = e.Node

                While temp.Parent IsNot Nothing
                    temp.Parent.Checked = e.Node.Checked 'this behavior will change if you put this code in BeforeCheck
                    temp = temp.Parent
                End While
            End If

        End Sub


    Thanks

    A place for MSDN users to socialize  

    Living on Earth may be expensive, but did you know that it includes a free trip around the sun? Isn't that worth it?
    • Proposed as answer by Vladimir.Ilic Monday, December 27, 2010 9:59 AM
    Sunday, December 26, 2010 9:05 AM
  • Hi Omie,

        When we do it in this way, if I uncheck any node all of it's parents' are unchecked. If any of the parent node has a checked child node, I want to stop unchecking one level below this parent. Please help.

     

    Cris Evan

     


    cris
    Sunday, December 26, 2010 10:41 AM
  • Hello Cris,

    could you please change the thread type to question cause this is clearly not a discussion.

    As for your question, if I understood it correctly, you could implement a function which would (recursively) check if any of the node's children is checked, and use the result to exit while loop (from Omie's post). 

    Best regards,
    Vladimir

    Sunday, December 26, 2010 12:58 PM
  • Hi Vladimir,

       Could you please provide similar source code or any link.  I have to stop unchecking parent nodes when a parent node with checked child node is found.

    Cris Evan

     


    cris
    Sunday, December 26, 2010 3:37 PM
  • here it is, try to incorporate it into Omie's solution to get what you need:

    c#

     

    	private void button1_Click(object sender, EventArgs e)
    	{
    		MessageBox.Show("Has checked child: " + hasCheckedChildNode(treeView1.SelectedNode, true));
    	}
    	private void button2_Click(object sender, EventArgs e)
    	{
    		MessageBox.Show("Has unchecked child: " + hasCheckedChildNode(treeView1.SelectedNode, false));
    	}
    
    	private bool hasCheckedChildNode(TreeNode node, bool isChecked)
    	{
    		for (int i = 0; i < node.Nodes.Count; i++)
    		{
    			TreeNode childNode = node.Nodes[i];
    			// stop if direct child is checked 
    			if (childNode.Checked == isChecked)
    				return true;
    
    			// go deep and check down the hierarchy
    			if (childNode.Nodes.Count > 0)
    			{
    				bool result = hasCheckedChildNode(childNode, isChecked);
    				if (result == true)
    					return true;
    			}
    		}
    
    		return false;
    	}
    

     

    VB.net:

     

    Private Sub button1_Click(sender As Object, e As EventArgs)
    	MessageBox.Show("Has checked child: " & hasCheckedChildNode(treeView1.SelectedNode, True))
    End Sub
    Private Sub button2_Click(sender As Object, e As EventArgs)
    	MessageBox.Show("Has unchecked child: " & hasCheckedChildNode(treeView1.SelectedNode, False))
    End Sub
    
    Private Function hasCheckedChildNode(node As TreeNode, isChecked As Boolean) As Boolean
    	For i As Integer = 0 To node.Nodes.Count - 1
    		Dim childNode As TreeNode = node.Nodes(i)
    		' stop if direct child is checked 
    		If childNode.Checked = isChecked Then
    			Return True
    		End If
    
    		' go deep and check down the hierarchy
    		If childNode.Nodes.Count > 0 Then
    			Dim result As Boolean = hasCheckedChildNode(childNode, isChecked)
    			If result = True Then
    				Return True
    			End If
    		End If
    	Next
    
    	Return False
    End Function
    
    
    best regards,
    Vladimir

     

    • Marked as answer by cris.evan Monday, December 27, 2010 9:13 AM
    Sunday, December 26, 2010 4:07 PM
  • Thanks a lot Vladimir.  That has done it.

     

    Cris Evan


    cris
    Monday, December 27, 2010 9:14 AM
  • You might wanna mark Omie's post As Answer too, since it actually contains the bigger part of the solution to your problem.

    thanks,
    Vladimir

    Monday, December 27, 2010 10:01 AM