Hey Pat,
I took a look at your code and there is a problem in it. When you are
expanding your tree in the Tre_AfterSelect subroutine you create one single
new instance of a TreeNode (P). You are adding that TreeNode to the tree and
you are re-using it. That is the cause of the problem, since you have to
create new TreeNodes for each element you are adding. So, if you are
creating a new TreeNode inside your while loop your problem will be solved.
I re-arranged the code a bit, and this sample is working in VB.NET as well:
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents TRE As System.Windows.Forms.TreeView
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.TRE = New System.Windows.Forms.TreeView
'
'TRE
'
Me.TRE.ImageIndex = -1
Me.TRE.Location = New System.Drawing.Point(17, 18)
Me.TRE.SelectedImageIndex = -1
Me.TRE.Size = New System.Drawing.Size(202, 172)
'
'Form1
'
Me.Controls.Add(Me.TRE)
Me.Menu = Me.MainMenu1
Me.MinimizeBox = False
Me.Text = "Form1"
End Sub
#End Region
Private Sub Tre_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles TRE.AfterSelect
MsgBox(TRE.SelectedNode.FullPath)
If e.Node.Tag = "L" Then
TRE.BeginUpdate()
Dim N As TreeNode = e.Node
Dim D As Integer = 0
For D = 0 To 9
N.Nodes.Add(New TreeNode(D.ToString))
Next
N.Expand()
TRE.EndUpdate()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CreateTree()
End Sub
Private Sub CreateTree()
Dim X As Integer
TRE.BeginUpdate()
TRE.Nodes.Clear()
For X = 1 To 26
Dim N As New TreeNode
N.Text = Chr(X + 64).ToString
N.Tag = "L"
TRE.Nodes.Add(N)
Next
TRE.EndUpdate()
End Sub
End Class
'-----------------------------------------------------------------
--
Regards,
Maarten Struys
PTS Software bv
----