How to Edit TreeNode From TreeView in VB.NET

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Email

The TreeView control of VB.NET Windows Framework displays a hierarchical list of data elements. It consists of the TreeNodes. You can access a TreeNode by clicking on the TreeView button in the VB.NET Toolbox on a form or other VB.NET control in your application. It will create a pointer to the TreeView control node location. You need to set a value of the TreeView label property in order to edit a TreeNode from the TreeView in VB.NET.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Click the "Start" button in Windows. Select "Microsoft Office" and "Microsoft Visual Studio."

  2. 2

    Click "Open" and select your Visual Basic program with the TreeView you want to edit.

  3. 3

    Add the following code after the "CurrentDb.Execute strSQL" line in your program:

    Private Sub treeView1_MouseDown(sender As Object, _

    e As System.Windows.Forms.MouseEventArgs)

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

    End Sub

    Private Sub menuItem_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." & _
    
        Microsoft.VisualBasic.ControlChars.Cr & _
    
        "Editing is not allowed.", "Selection error")
    

    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
    
            e.Node.EndEdit(False)
    
         Else
    
            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
    
         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

    This will bind the TreeNode with the mouse pointer.

  4. 4

    Click "File" and "Save" to save the edited TreeNode from the TreeView in your VB.NET program.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.