0

So I have a tree view that I extended so the user could select multiple items, but I've run into a little bug where it randomly stop painting itself.

After some digging around I actually found that it was my MouseDown and MouseUp events that were causing the problem:

    protected override void OnMouseDown(MouseEventArgs e)
    {
        BeginUpdate();
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        EndUpdate();
        base.OnMouseUp(e);
    }

So if the user clicks in the TreeView, but releases outside of it, the MouseUp event doesn't get called, and the update won't end. I understand that much, but what's strange is after that, no matter how many times they click in the box it won't start drawing again, even though the EndUpdate() is now being called. This can be resolved by adding a button with EndUpdate() in it, but obviously I don't want that.

The reason I even need this is that I manually manage the coloring of the nodes as well as the selected node so all of the normal selection events are actually canceled. But without the Begin and End update I still get a flicker of a selection box before it reaches the cancel. With these I don't get the flicker but apparently it comes at a cost.

Here's a dropbox link to an empty project that demonstrates the issue. If you break on the end update, you can see that it is getting called, but it just doesn't seem to change anything. But if you click the button that calls the same code, it resolves it.

Can anyone explain why this is happening? Am I missing something terribly obvious? Or is this just a bad way to go about it in general?

Regards.

Answer: As no answer has been submitted, here's the solution brought to my attention by Hans Passant. Didn't want to post and mark as answer myself as I didn't come up with this. Added this to my extended TreeView class and all issues seem to be resolved. (Albeit it's probably going to make some bad calls, but besides being wasteful it doesn't appear to hurt anything).

    protected override void OnMouseCaptureChanged(EventArgs e)
    {
        EndUpdate();
        base.OnMouseCaptureChanged(e);
    }
JWrightII
  • 942
  • 11
  • 26
  • When you do this, you *must* set the Capture property to ensure you get the MouseUp event. And subscribe the MouseCaptureChanged event to know when other code captured the mouse. Enable double-buffering on the TreeView control instead. – Hans Passant Jan 24 '13 at 20:06
  • possible duplicate of [Treeview flickering?](http://stackoverflow.com/questions/10362988/treeview-flickering) – Hans Passant Jan 24 '13 at 20:07
  • I've been using Hans Passant's double buffer code (Same as in the question he just linked to) for the length of this project, but it wasn't able to help with this since the selection isn't exactly something you can easily stop from being drawn between events. I have even in the past tried using extension methods to disable drawing on the parent obeject. But the begin and end update were the only things that seemed to work best, until this issue. – JWrightII Jan 24 '13 at 20:17

0 Answers0