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);
}