≡ Menu
Deep in the Code

Differences between the VB6 TreeNode and .NET System.Windows.Forms.TreeNode

In working on the conversion of a VB6 Forms application to VB.NET (as mentioned in my last post), I have discovered many instances where Microsoft decided to keep the name (and sometimes even the associated syntax) of an element, while changing the functionality of the methods related to that element.
One such example is that of the TreeView and its TreeNodes.

In the VB6 application, the code associated with the NodeCheck event was written like this:


Private Sub tvwDataCategory_NodeCheck(ByVal Node As MSComctlLib.Node)
Dim n As Integer

If Left(Node.Key, cTagLength) = cDataTypeTag Then
Node.ForeColor = DefaultForeColor
If Node.Children <> 0 Then
n = Node.Child.Index
While n <> Node.Child.LastSibling.Index
If Node.Checked Then
tvwDataCategory.Nodes(n).Checked = True
Else
tvwDataCategory.Nodes(n).Checked = False
End If
n = tvwDataCategory.Nodes(n).Next.Index
Wend
If Node.Checked Then
tvwDataCategory.Nodes(n).Checked = True
Else
tvwDataCategory.Nodes(n).Checked = False
End If
End If

Else
Call FormatChecks
End If

End Sub

Even after running the VB6 code through the Visual Studio upgrade wizard, the resulting code did not work as it did before.  The NodeCheck event in VB6 has become the AfterCheck event in .NET, and the new code should read something like this:


Private Sub tvwDataCategory_AfterCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeViewEventArgs) Handles tvwDataCategory.AfterCheck
Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node
Dim n As Integer

If VB.Left(Node.Name, cTagLength) = cDataTypeTag Then
Node.ForeColor = DefaultForeColor

Dim nNodes As TreeNodeCollection = eventArgs.Node.Nodes

For Each nNode As TreeNode In nNodes
If nNodes.Count = 0 Then
If nNode.Checked Then
tvwDataCategory.Nodes.Item(n).Checked = True
Else
tvwDataCategory.Nodes.Item(n).Checked = False
End If
End If
Next
Else
Call FormatChecks()
End If
End Sub

I am still working to verify that this will traverse the entire node and all child branches.  As the FirstSibling and LastSibling functions have been made obsolete, new code must be written to duplicate that functionality.  The adventure continues…