Results 1 to 6 of 6

Thread: Delete selected tree node...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Delete selected tree node...

    I have a treeview control that looks like this:

    - Simon
    -- Line
    -- Circle
    -- Rectangle

    If the "Cirlce" node is selected I was wondering how I would delete that node using vb.net?

    Thanks in advance

    Simon

  2. #2
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Delete selected tree node...

    This should get the job done.

    Code:
    .Nodes.Remove(.SelectedNode)

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Delete selected tree node...

    How to: Add and Remove Nodes with the Windows Forms TreeView Control
    http://msdn2.microsoft.com/en-us/library/70w4awc4.aspx

  4. #4
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Delete selected tree node...

    Simple...
    Code:
    tvTreeView.SelectedNode.Remove()
    (tvTreeView is to be replaced by your tree view)

    On what event you want to remove the node, is up to you...
    if you want it to actually delete when it's selected, then you can add this code:

    Code:
    Private Sub tvTreeView_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tvTreeView.AfterSelect
        e.Node.Remove() 'This might error if you have no nodes..
    End Sub
    Cheers
    Icyculyr

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Delete selected tree node...

    Thanks for your help however I have found a slight problem with the way that I am working.

    I have a treeView which contains a number of nodes and also have a pictureBox, what I am doing is deleting the node element from within the pictureBox using a contextMenuStrip. This therefore means that the treeView loses focus. When I use your code it gives me a nullReferenceException on code I guess this is because the node is note focued? However if I execute your code from a contextMenuStrip on the treeView it works fine.

    How can I get around this problem??

    FYI: I have set the hideSelection to false

    Thanks in advance

    Simon

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Delete selected tree node...

    Show us YOUR code and point out EXACTLY where the error occurs.

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