|
-
Jul 12th, 2018, 02:43 PM
#1
[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?

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
-
Jul 12th, 2018, 07:32 PM
#2
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:
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
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.
-
Jul 13th, 2018, 12:46 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|