This documentation is archived and is not being maintained.

TreeView.AfterLabelEdit Event

Occurs after the tree node label text is edited.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Event AfterLabelEdit As NodeLabelEditEventHandler
'Usage
Dim instance As TreeView 
Dim handler As NodeLabelEditEventHandler 

AddHandler instance.AfterLabelEdit, handler

For more information about handling events, see Consuming Events.

The following code example allows the user to edit nonroot tree nodes by using a ContextMenu. When the user right clicks the mouse, the TreeNode at that position is determined and stored in a variable named mySelectedNode. If a nonroot tree node was selected, it is put into an editable state, allowing the user to edit the node label. After the user stops editing the tree node label, the new label text is evaluated and saved. For this example, several characters are considered not valid in the label text. If one of the invalid characters is in the label string, or the string is empty, the user is notified of the error and the label is returned to its previous text.

' Get the tree node under the mouse pointer and 
' save it in the mySelectedNode variable.  
Private Sub treeView1_MouseDown(sender As Object, _
  e As System.Windows.Forms.MouseEventArgs)

   mySelectedNode = treeView1.GetNodeAt(e.X, e.Y)
End Sub     

Private Sub menuItem1_Click(sender As Object, e As System.EventArgs)
   If Not (mySelectedNode Is Nothing) And _
     Not (mySelectedNode.Parent Is Nothing) Then
      treeView1.SelectedNode = mySelectedNode
      treeView1.LabelEdit = True 
      If Not mySelectedNode.IsEditing Then
         mySelectedNode.BeginEdit()
      End If 
   Else
      MessageBox.Show("No tree node selected or selected node is a root node." & _
        Microsoft.VisualBasic.ControlChars.Cr & _
        "Editing of root nodes is not allowed.", "Invalid selection")
   End If 
End Sub     

Private Sub treeView1_AfterLabelEdit(sender As Object, _
  e As System.Windows.Forms.NodeLabelEditEventArgs)
   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)
         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 
         Me.treeView1.LabelEdit = False 
   End If 
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: