Results 1 to 3 of 3

Thread: [RESOLVED] Treeview AfterLabelEdit and Sort

  1. #1

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Resolved [RESOLVED] Treeview AfterLabelEdit and Sort

    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: 1580
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
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Treeview AfterLabelEdit and Sort

    It appears that the issue occurs because the sorting causes labels to be edited and thus raises the AfterLabelEdit event multiple times, so the event handler interferes with itself. I changed this:
    vb.net Code:
    1. Private Sub treeview1_afterlabeledit(ByVal sender As Object, ByVal e As NodeLabelEditEventArgs) Handles treeView1.AfterLabelEdit
    2.     If Not ignoreLabelEdit Then
    3.         If Not (e.Label Is Nothing) Then
    4.             If e.Label.Length > 0 Then
    5.                 If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) = -1 Then
    6.                     ' Stop editing without canceling the label change.
    7.                     e.Node.EndEdit(False)
    8.                     e.Node.Text = e.Label
    9.                     ignoreLabelEdit = True
    10.                     treeView1.Sort()
    11.                     ignoreLabelEdit = False
    12.                 Else
    13.                     ' Cancel the label edit action, inform the user, and
    14.                     ' place the node in edit mode again.
    15.                     e.CancelEdit = True
    16.                     MessageBox.Show("Invalid tree node label." &
    17.                                     Microsoft.VisualBasic.ControlChars.Cr &
    18.                                     "The invalid characters are: '@','.', ',', '!'",
    19.                                     "Node Label Edit")
    20.                 End If '
    21.             Else
    22.                 ' Cancel the label edit action, inform the user, and
    23.                 ' place the node in edit mode again.
    24.                 e.CancelEdit = True
    25.                 MessageBox.Show("Invalid tree node label." &
    26.                                 Microsoft.VisualBasic.ControlChars.Cr &
    27.                                 "The label cannot be blank", "Node Label Edit")
    28.             End If
    29.         End If
    30.     End If
    31. End Sub
    to this:
    Code:
    Private ignoreLabelEdit As Boolean = False
    
    Private Sub treeview1_afterlabeledit(ByVal sender As Object, ByVal e As NodeLabelEditEventArgs) Handles treeView1.AfterLabelEdit
        If Not ignoreLabelEdit Then
            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
                        ignoreLabelEdit = True
                        treeView1.Sort()
                        ignoreLabelEdit = 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")
                    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")
                End If
            End If
        End If
    End Sub
    and it appeared to resolve the issue.

  3. #3

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Treeview AfterLabelEdit and Sort

    Thanks jmc. There's also some funny business when you change the node.name too. Wrapping the whole thing with the ignore does seem to avoid the random re-labelling issue.
    I still get the begin-edit problem on the first available node, but I'll punt that down the road for now.

    Thanks.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width