Removing Node corrupts TreeView

3 posts, 0 answers
  1. Lev
    Lev avatar
    6 posts
    Member since:
    Jun 2010

    Posted 29 Nov 2013 Link to this post


    I am using RadControls for WinForms Q1 2011 and Visual Studio 2012, Ultimate Edition to develop a .NET4.0 app.
    The app uses RadTreeView for showing a very simple file system explorer and removes nodes in two cases:
    1. On NodeExpandedChanged the event handler removes a dummy node representing a collapsed directory with a new node.
    private void OnDirExpandedChanged(object sender, RadTreeViewEventArgs e)
        if (e.Node.Expanded)
            if (e.Node.FirstNode.Name == "____dummy____")
                RadTreeNode l_parent = e.Node.Parent;
                int l_nIx = e.Node.Index;
                RadTreeNode l_new = CreateDirNode(new DirectoryInfo(e.Node.Name), true);
                l_parent.Nodes.Insert(l_nIx, l_new);
    } // OnDirExpandedChanged
    This code works just fine.

    2. On EventArrived fired by System.Management.ManagementEventWatcher upon CD insertion/ejection the event handler removes either a node representing the empty drive or a node representing the CD file system.
    Currently I narrowed the code just to the attempt to remove a CD node
    private void OnCdInsert(object sender, EventArrivedEventArgs e)
        ManagementBaseObject l_wmiDevice = (ManagementBaseObject) e.NewEvent["TargetInstance"];
        string l_sDriveRootDir = l_wmiDevice["Name"].ToString() + Path.DirectorySeparatorChar;
        int l_nIx = m_fileExplorer.Nodes[0].Nodes.IndexOf(l_sDriveRootDir);
        m_fileExplorer.Nodes[0].Nodes[l_nIx].Remove(); // (1)
        m_fileExplorer.Nodes[0].Nodes.Remove(l_sDriveRootDir); // (2)
    } // OnCdInsert
    I tried different ways of removing the node, I tried wrapping the code with BeginUpdate()/EndUpdate(), I tried calling Invalidate() or Refresh() - the result is always the same
    a) The node itself is still shown, although the debugger shows that the nodes count has decreased;
    b) Expanding other nodes, e.g. a node representing C:\, does not work any more, i.e. the event is fired, but view remains unchanged.

    Note: The only difference between the nodes removed in the cases #1 and #2 is that OnDirExpanded() removes only grandchildren or farther of the root node, while OnCdInsert() removes a child of the root node.

    I am really lost and your help is my only hope now.

    I am looking forward to your assistance and thank you in advance for it.

    Best regards,
  2. Lev
    Lev avatar
    6 posts
    Member since:
    Jun 2010

    Posted 30 Nov 2013 Link to this post

    Found it :)

    The EventArrived handler was called on a thread other than the one, which created the RadTreeView control.
    So applying the usual InvokeRequired-Invoke() technique solved the issue (the lack of the InvalidOperationException originally confused me).

    Best regards,
  3. DevCraft R3 2016 release webinar banner
  4. Dess
    Dess avatar
    1561 posts

    Posted 04 Dec 2013 Link to this post

    Hello Lev,

    Thank you for contacting Telerik Support.

    I am glad that the issue you were facing is now resolved. Note that all UI controls are not thread safe controls in the whole Windows Forms UI platform. Here is an article on MSDN, describing how to make thread-safe Winforms UI application. This means that RadTreeView is not thread safe as well and cannot be used outside the main UI thread. You should use an Invoke to update the controls in cross threading scenario.

    Please do not hesitate to contact us if you have any additional questions.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top