Windows Develop Bookmark and Share   
 index > Windows Forms General > Treeview aftercheck event fires twice
 

Treeview aftercheck event fires twice

I am working on some code that checks/unchecks all the child nodes of a parent node. My problem, at the moment, is that the after check event fires twice. Here's the code that I'm using:

Private Sub tvFolders_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs)

If Me.SettingNodes = True Then
Exit Sub
End If

Try
Windows.Forms.Cursor.Current = Cursors.WaitCursor

Me.tvFolders.SelectedNode = e.Node
Me.tvFolders.BeginUpdate()

If e.Node.Checked = True Then
e.Node.ForeColor = Color.Blue
Else
e.Node.ForeColor = Color.Black
End If

Me.tvFolders.EndUpdate()

Me.SetNodes(e.Node, e.Node.Checked)

Me.tvFolders.SelectedNode = e.Node

Windows.Forms.Cursor.Current = Cursors.Default

Catch ex As Exception
Throw ex
End Try

End Sub


Private Sub SetNodes(ByVal tNode As TreeNode, ByVal boolStatus As Boolean)

Try
Debug.WriteLine("Setting Node: " & tNode.FullPath)

RemoveHandler tvFolders.AfterCheck, AddressOf tvFolders_AfterCheck

Me.SettingNodes = True

For Each childNode As TreeNode In tNode.Nodes
childNode.Checked = boolStatus
Next

Me.SettingNodes = False

Me.tvFolders.SelectedNode = tNode

AddHandler tvFolders.AfterCheck, AddressOf tvFolders_AfterCheck

Catch ex As Exception
Throw ex
End Try

End Sub

Any ideas as to why the event fires twice?

thanks

llebron  Monday, January 28, 2008 5:01 PM

Hi llebron,

I performed a test using your code, and it worked well on my machine. The AfterCheck event only was raised once when I check/uncheck the TreeNode. I guess that you may register the AfterCheck event twice in your code somewhere. As the document says, you can use the Action property of the TreeViewEventArgs to prevent the event from being raised multiple times when setting the TreeNode.Checked property from within a BeforeCheck or AfterCheck event handler. Here is the sample form document.

Code Snippet

' Updates all child tree nodes recursively.

Private Sub CheckAllChildNodes(ByVal treeNode As TreeNode, ByVal nodeChecked As Boolean)

Dim node As TreeNode

For Each node In treeNode.Nodes

node.Checked = nodeChecked

If node.Nodes.Count > 0 Then

' If the current node has child nodes, call the CheckAllChildsNodes method recursively.

Me.CheckAllChildNodes(node, nodeChecked)

End If

Next node

End Sub

' NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.

' After a tree node's Checked property is changed, all its child nodes are updated to the same value.

Private Sub node_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles treeView1.AfterCheck

' The code only executes if the user caused the checked state to change.

System.Console.WriteLine("Without action")

If e.Action <> TreeViewAction.Unknown Then

System.Console.WriteLine("With action")

If e.Node.Nodes.Count > 0 Then

' Calls the CheckAllChildNodes method, passing in the current

' Checked value of the TreeNode whose checked state changed.

Me.CheckAllChildNodes(e.Node, e.Node.Checked)

End If

End If

End Sub

More information, please visit:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck(VS.80).aspx

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, February 01, 2008 3:40 AM

Hi llebron,

I performed a test using your code, and it worked well on my machine. The AfterCheck event only was raised once when I check/uncheck the TreeNode. I guess that you may register the AfterCheck event twice in your code somewhere. As the document says, you can use the Action property of the TreeViewEventArgs to prevent the event from being raised multiple times when setting the TreeNode.Checked property from within a BeforeCheck or AfterCheck event handler. Here is the sample form document.

Code Snippet

' Updates all child tree nodes recursively.

Private Sub CheckAllChildNodes(ByVal treeNode As TreeNode, ByVal nodeChecked As Boolean)

Dim node As TreeNode

For Each node In treeNode.Nodes

node.Checked = nodeChecked

If node.Nodes.Count > 0 Then

' If the current node has child nodes, call the CheckAllChildsNodes method recursively.

Me.CheckAllChildNodes(node, nodeChecked)

End If

Next node

End Sub

' NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.

' After a tree node's Checked property is changed, all its child nodes are updated to the same value.

Private Sub node_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles treeView1.AfterCheck

' The code only executes if the user caused the checked state to change.

System.Console.WriteLine("Without action")

If e.Action <> TreeViewAction.Unknown Then

System.Console.WriteLine("With action")

If e.Node.Nodes.Count > 0 Then

' Calls the CheckAllChildNodes method, passing in the current

' Checked value of the TreeNode whose checked state changed.

Me.CheckAllChildNodes(e.Node, e.Node.Checked)

End If

End If

End Sub

More information, please visit:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck(VS.80).aspx

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, February 01, 2008 3:40 AM

You can use google to search for other answers

Custom Search

More Threads

• 2 line per row view
• Securing a Database Connection String
• Programmatically clicking a link and the >NET2 web browser control
• Treeview control horizontal scrollbar issue
• DataGridView - readonly cells
• Can List<T> be bound to DataGridView
• ListBox ClearSelected and SelectedIndex = -1 not working?
• Map ListViewSubItem key(name) to a specific ListView's column name?
• Memory game
• Missing XML comment error msg