Trying just to allow a user to edit a label, and when they're done the treeview sorts alphabetically. Sounds simple, right?

I find that a) it always begins editing the first node on the list after the sort, and b) random nodes start changing their text too

I took this code to repro it from a combination of the TreeViewNodeSorter and TreeView.AfterLabelEdit pages on msdn.

Expand both sections and start changing the names one after the other. You can click out to end the edit or push Enter, it doesn't seem to matter.


As per the image, the first node always gets put into edit mode after I finish editing, and when I changed one of the nodes to wxy, it decided to change a different parent node too.


Any thoughts on what might be going on?

Name:  treeview_weirdness.jpg
Views: 1979
Size:  19.8 KB



Code:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitializeTreeView1()
    End Sub

    Private WithEvents treeView1 As TreeView
    Private textBox1 As TextBox
    Private WithEvents button1 As Button


    Private Sub InitializeTreeView1()

        ' Create the TreeView
        treeView1 = New TreeView()
        treeView1.Size = New Size(200, 200)
        treeView1.LabelEdit = True

        ' Create the button and set some basic properties. 
        button1 = New Button()
        button1.Location = New Point(205, 138)
        button1.Text = "Set Sorter"

        ' Create the root nodes.
        Dim docNode As New TreeNode("Documents")
        Dim spreadSheetNode As New TreeNode("Spreadsheets")

        ' Add some additional nodes.
        spreadSheetNode.Nodes.Add("payroll.xls")
        spreadSheetNode.Nodes.Add("checking.xls")
        spreadSheetNode.Nodes.Add("tracking.xls")
        docNode.Nodes.Add("phoneList.doc")
        docNode.Nodes.Add("resume.doc")

        ' Add the root nodes to the TreeView.
        treeView1.Nodes.Add(spreadSheetNode)
        treeView1.Nodes.Add(docNode)

        ' Add the TreeView to the form.
        Controls.Add(treeView1)
        Controls.Add(button1)



    End Sub



    Private Sub treeview1_afterlabeledit(ByVal sender As Object, ByVal e As NodeLabelEditEventArgs) Handles treeView1.AfterLabelEdit

        If Not (e.Label Is Nothing) Then
            If e.Label.Length > 0 Then
                If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) = -1 Then
                    ' Stop editing without canceling the label change.
                    e.Node.EndEdit(False)
                    e.Node.Text = e.Label
                    treeView1.Sort()
                    'e.Node.EndEdit(False)
                Else
                    ' Cancel the label edit action, inform the user, and
                    ' place the node in edit mode again. 
                    e.CancelEdit = True
                    MessageBox.Show("Invalid tree node label." &
              Microsoft.VisualBasic.ControlChars.Cr &
              "The invalid characters are: '@','.', ',', '!'",
              "Node Label Edit")
                    'e.Node.BeginEdit()
                End If '
            Else
                ' Cancel the label edit action, inform the user, and
                ' place the node in edit mode again. 
                e.CancelEdit = True
                MessageBox.Show("Invalid tree node label." &
           Microsoft.VisualBasic.ControlChars.Cr &
           "The label cannot be blank", "Node Label Edit")
                'e.Node.BeginEdit()
            End If
        End If


    End Sub


End Class