VB Coding Tip: Treeview - Label Editing

Monday Nov 18th 2002 by Sam Huggill

This code allows you to simulate the label editing in Explorer.

After the success of the last three parts of the Treeview series when I found this code on Microsoft's web site, I thought it would be good to share it with you.

This code allows you to simulate the label editing in Explorer. If the user leaves it blank then a message box with tell you and you will have to enter a value. If you press Esc then it will restore the previous value.

Option Explicit
Dim sNodeText As String  ' to hold the node text
Private Sub Form_Load()
    'Add some nodes to the TreeView
    TreeView1.Nodes.Add , , , "test"
    TreeView1.Nodes.Add , , , "test 1"
    TreeView1.Nodes.Add , , , "test 2"
End Sub
Private Sub Timer1_Timer()
    ' Put the TreeView in edit mode
    Timer1.Enabled = False
End Sub
Private Sub TreeView1_AfterLabelEdit( _
         Cancel As Integer, _
         NewString As String)
 ' Make sure that we have a value in the Label
 If Len(NewString) < 1 Then
   ' The Label is empty
   MsgBox "Error! You must enter a value"
   ' enable the Timer to get us back
   '   to edit mode
   Timer1.Interval = 100
   Timer1.Enabled = True
 End If
End Sub
Private Sub TreeView1_BeforeLabelEdit( _
        Cancel As Integer)
 ' If the label is not empty store the string
 If Len(TreeView1.SelectedItem.Text) > 0 Then
   sNodeText = TreeView1.SelectedItem.Text
 End If
End Sub
Private Sub TreeView1_KeyUp(KeyCode As Integer, _
                            Shift As Integer)
 ' If the user hits the Esc key then restore 
 '   the old label
 If KeyCode = vbKeyEscape Then
   TreeView1.SelectedItem.Text = sNodeText
 End If
End Sub
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved