?
Solved

TreeView  - Rename Node

Posted on 2000-04-22
3
Medium Priority
?
1,953 Views
Last Modified: 2008-03-17
I want a routine like in Exporer where it renames a Node
using a little box around the Node name. Can this be done in VB6?

You know what I mean, if your in Exporer and choose a directory
you can right-click, get a menu, choose Rename and it will
put a little box around the directory name and let you enter new text!

I can bring up the right-click menu but what now!
How do I put a little box around TreeView Node and retype name?
0
Comment
Question by:jgore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 800 total points
ID: 2740443
You normally have nothing to do!

Just ensure that the LabelEdit property of the TreeView is set to tvwAutomatic. You can now click an item, wait a second and click on it again and you will be in edit mode.

You can also display a popup menu. See this code:
Private Sub Form_Load()
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(, , "R", "Root")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C1", "Child 1")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C2", "Child 2")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C3", "Child 3")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C4", "Child 4")
   nodX.EnsureVisible
   TreeView1.LabelEdit = tvwManual   ' Set property to manual.
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

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

Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbRightButton Then
    Me.PopupMenu mnuTest
End If
End Sub


0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2740456
take a look at http://support.microsoft.com/support/kb/articles/q192/1/70.asp

------------------------------
The following procedure can be used to edit the node name on the TreeView side of the Windows Explorer:

Click on a node with focus to place it in edit mode.

Remove all the text from the Node label.

Move focus to a different Node.

You receive the following error message:  You must type in a file name.

Focus returns to the Node you were editing and you remain in edit mode.

If you hit the Esc Key, the original text is placed back in the Node label.

MORE INFORMATION
This sample demonstrates how to achieve the same effect using the TreeView control in Visual Basic. It also shows how to verify that when a user edits a Node label that it is not left blank.

Step-by-Step Example
Create a new standard EXE project. Form1 is created by default.

From the Project menu, select Components, check "Microsoft Windows Common Controls 6.0," and then click OK.

Add a TreeView and Timer control to Form1.

Add the following to the code window of Form1:
      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
        TreeView1.StartLabelEdit
        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
 

Save and run the project. Click on a Node to select it, then click on it again to place it in edit mode.

Follow steps 1-7 as described in the Summary above. When you hit the ESC key, you will see that the old value for the Node label has been restored.
0
 

Author Comment

by:jgore
ID: 2740786
Thanks to both of you!
You fixed me right up.
Cya'z.............
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question