Visual Basic: Windows Controls

LabelEdit Property Example

This example initiates label editing when you click the Command button. It allows a Node object to be edited unless it is a root Node. The LabelEdit property must be set to Manual. To try the example, place a TreeView control and a CommandButton on a form. Paste the code into the form's Declarations section. Run the example, select a node to edit, and press the CommandButton.

  Private Sub Form_Load()
   Dim nodX As Node
   Dim i As Integer
   TreeView1.LabelEdit = tvwManual   ' Set property to manual.
   Set nodX = TreeView1.Nodes.Add(,,," Node 1")   ' Add first node.

   For i = 1 to 5   ' Add 5 nodes.
      Set nodX = TreeView1.Nodes.Add(i,tvwChild,,"Node " & CStr(i + 1))
   Next I

   nodX.EnsureVisible   ' Show all nodes.
End Sub

Private Sub Command1_Click()
   ' Invoke the StartLabelEdit method on the selected node,
   ' which triggers the BeforeLabelEdit event.
   TreeView1.StartLabelEdit 
End Sub

Private Sub TreeView_BeforeLabelEdit (Cancel as Integer)
   ' If the selected item is the root, then cancel the edit.
   If TreeView1.SelectedItem Is TreeView1.SelectedItem.Root Then
      Cancel = True
   End If
End Sub

Private Sub TreeView_AfterLabelEdit _
(Cancel As Integer, NewString As String)
   ' Assume user has entered some text and pressed the ENTER key.
   ' Any nonempty string will be valid.
   If Len(NewString) = 0 Then
      Cancel = True
   End If
End Sub