Results 1 to 4 of 4

Thread: [RESOLVED] Treeview - Add new node and select text for editing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    15

    Resolved [RESOLVED] Treeview - Add new node and select text for editing

    I want to add nodes that behave in a similar fashion as when you add new folders in windows explorer. i.e. right click > new folder > then the new folder is added and highlighted as New Folder so that as soon as the user types they are overwriting the folder name.

    I can add a new node as a child node of the node I have selected and label it "New Node" but I can't figure out how to select the new node and have it highlighted so that when the user types, they are overwriting the "New Node" text.

    I have tried this but it doesn't work.
    Code:
    Private Sub AddProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProjectToolStripMenuItem.Click
            Me.trvProjects.SelectedNode.Nodes.Add("New Node")
            Me.trvProjects.SelectedNode = trvProjects.Nodes("New Node")
            Me.trvProjects.Focus()
        End Sub

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Treeview - Add new node and select text for editing

    You can call the BeginEdit method of the node. See here:
    http://msdn.microsoft.com/en-us/libr...beginedit.aspx

  3. #3
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    Re: Treeview - Add new node and select text for editing

    This works:
    nmad beat me to it though.
    Code:
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tvw As TreeView
            Dim nd As TreeNode
    
            tvw = Me.TreeView1
            tvw.LabelEdit = True
            nd = tvw.Nodes.Add("New Item")
    
            tvw.SelectedNode = nd
    
            'THIS is the key line:        
            nd.BeginEdit()
            
        End Sub
    Last edited by RunsWithScissors; Jun 27th, 2009 at 09:59 AM. Reason: Give credit where due to nmad. I had to figure it out the hard way.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    15

    Re: Treeview - Add new node and select text for editing

    Thanks guys, that worked. I have a lot more work to do as I am only just getting started with VB, so it really is very much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width