I’m having a seriously annoying problem on which I’ve been spending a ridiculous amount of time on trying to resolved and still to no avail!!
Over the years, I’ve never used the standard treeview that comes with .NET as I’ve always used a third-party component but talking about a bad experience so far!!!
In short, I’m loading my drives and then when expanding a drive, I list folders, and so on… Nothing new here, but I’m having 2 major problems:
-
When all branches are collapsed, it triggers the before and after expand event twice.. BeforeExpand x 2, then AfterExpand x 2.
-
When multiple branches are expanded, it will trigger the event 2 for each expanded branches… It’s like I’m calling some sort of recursive code when I’m not… So for example,
Expand C -> Triggers BeforeExpand x 2, then AfterEvent x 2 – Leave it expanded
Expand E -> Triggers BeforeExpand x 2 for E, then AfterEvent x 2, then does it again for C – Leave E expanded.
Expand F -> Triggers BeforeExpand x 2 for F, then AfterEvent x 2, then does it again for E then C.
It doesn’t make sense!!
Code wise:
-
Initialize the BeforeExpand and AfterExpand when my form initializes i.e.
treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand); treeView1.AfterExpand += new TreeViewEventHandler(treeView1_AfterExpand); -
When I expand a node, a background thread is called and either loads the drives or folders. From the background worker thread, I call a function, which handles the cross threading issue by calling a delegate so again, nothing special
I’m out of ideas as to what’s causing this bar the fact that it could be a bug on MS side, but I doubt it… It feels like a problem that just far too obvious.
I’m happy to post my code if needed as this was just a dummy test app I started and maybe you’ll spot something I’ve done wrong but I just can’t see what.
Hope someone has a clue as to what’s causing this.
Thanks.
Thierry.
I figured out what the problem was so I thought I’d share it in case this happens to anyone else!!
This is insane but in the background worker thread, I called the treeview1.Sort() in the RunWorkerCompleted and this is what is causing the beforeExpand and afterExpand events to be called multiple times! Once removed, it behaved accordingly.
I guess I will sort the folders first and then raised the delegate from the thread to update the UI with the sorted list.
That’s it!