Re: Solved: Remove a Node from TreeView?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alex Feinman [MVP] (public_news_at_alexfeinman.com)
Date: 10/24/04


Date: Sun, 24 Oct 2004 00:21:16 -0700

Why do you expect to be able to remove a non-root node by calling Remove on
the treeview itself?
When you add a node, you add it to TreeView.Nodes(0).Nodes
When you remove it, you should be removing it by calling
TreeView.Nodes(0).Nodes.Remove and not TreeView.Nodes.Remove.
You get OutOfRangeException because the node does not belong to a collection
from which you are trying to remove it.
Alternatively you can use
if TreeView.SelectedNode.ParentNode Is Nothing then
    TreeView.Nodes.Remove(TreeView.SelectedNode)
else
    TreeView.SelectedNode.ParentNode.Remove(TreeView.SelectedNode)
end if

-- 
Alex Feinman
---
Visit http://www.opennetcf.org
"Nicky Smith" <nixdk@yahoo.dk> wrote in message 
news:fb5ln0hg8t95avnuqjcgd7h2mu5ghmne9e@4ax.com...
>
> Hello,
>
> I've found a workaround for this bug (and it is a bug!)
>
> You can't call the remove method of the parent node, but you can call
> the .Remove method of the reference node:
>
> This raises the error:
>
>  Dim TN As TreeNode = TV1.SelectedNode
>  TV1.Nodes.Remove(TN)
>
> This works as expected:
>
> Dim TN As TreeNode = TV1.SelectedNode
> TN.Remove()
>
> I've tried this on the emulator without SP1 or 2 and on a device with
> SP2 and the bug exists in both scenarios.
>
> The .Remove method is documented in the .Net msdn help files using the
> first version with a reference as an argument for the remove method.
> (.remove(tn))
>
> -N
>
>
>
>
> On Sat, 23 Oct 2004 13:03:30 +0200, Nicky Smith <nixdk@yahoo.dk>
> wrote:
>
>>Hello,
>>
>>I've got a problem with a TreeView. I get a starnge error when trying
>>to call the .Remove method.
>>
>>The method is called from a ContextMenu, when holding the stylus down
>>on a childnode.
>>
>>I then call the Remove method like this:
>>
>>If MessageBox.Show("Are you sure?", "Delete Item?",
>>MessageBoxButtons.YesNoCancel, _
>>            MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
>>= DialogResult.Yes Then
>>                Dim TN As TreeNode = TV1.SelectedNode
>>
>>                TV1.Nodes.Remove(TN)
>>
>>End If
>>
>>This throws an Arguement Out of Range exception, on the call to
>>Remove(). I don't undeerstand that, because the TN object does become
>>a valid tree node, and the Remove method accepts a TreeNode as an
>>argument!
>>
>>What am I doing wrong?
> 


Relevant Pages

  • RE: How to create a TreeView node with no image?
    ... each treenode in the treeview is 0. ... missing treeline was prepared for showing the image. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Improve Performance of UI
    ... for my TreeView though, so I know exactly how many levels deep it needs to ... I'm also playing with the idea of creating a new subclass or two of TreeNode ... > you cache the data in a dataset or other bucket and add the child nodes to ... > the expanded node when the user expands a node. ...
    (microsoft.public.dotnet.languages.csharp)
  • Populate TreeView From XML
    ... method 'TreeNode' takes '1' arguments. ... private void Page_Load ... Initialize the TreeView control. ... TreeNode tNode = new TreeNode; ...
    (microsoft.public.dotnet.languages.csharp)
  • Populate TreeView From XML
    ... method 'TreeNode' takes '1' arguments. ... private void Page_Load ... Initialize the TreeView control. ... TreeNode tNode = new TreeNode; ...
    (microsoft.public.dotnet.general)
  • TreeView duplicating nodes and also blowing up IE
    ... I'm having some serious problems with the TreeView control. ... firstly create a TreeNode then does some recursive procedure to add all ... add the root node, so I know the problems not that... ...
    (microsoft.public.dotnet.framework.windowsforms.controls)