This is a migrated thread and some comments may be shown as answers.

Removing Node corrupts TreeView

2 Answers 83 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Lev
Top achievements
Rank 1
Lev asked on 29 Nov 2013, 04:43 PM
Hi,

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____")
        {
            m_fileExplorer.BeginUpdate();
 
            RadTreeNode l_parent = e.Node.Parent;
            int l_nIx = e.Node.Index;
 
            e.Node.Remove();
 
            RadTreeNode l_new = CreateDirNode(new DirectoryInfo(e.Node.Name), true);
            l_parent.Nodes.Insert(l_nIx, l_new);
            l_new.Expand();
 
            m_fileExplorer.EndUpdate();
        }
    }
} // 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 Answers, 1 is accepted

Sort by
0
Lev
Top achievements
Rank 1
answered on 30 Nov 2013, 11:29 PM
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,
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Dec 2013, 12:49 PM
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.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
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 >>
Tags
Treeview
Asked by
Lev
Top achievements
Rank 1
Answers by
Lev
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or