Thursday, August 28, 2008

C#/.NET: I Figured This Out, With A Little Help

C#/.NET: I Figured This Out, With A Little Help

I was having some problems with something that others have run into as well:
For some reason a panel is not a first class citizen when it comes to
repainting. If you paint your own graphics into a panel and expect the
panel to refresh after dialogs and menus are dismissed - you will be
disappointed. You must do some fake out of the form to make it thinks
it needs to repaint - and a grid view ( or other less burdensome
control???) that needs repainting will cause a true WM_PAINT to be
sent to this control that you can catch and then force a repaint of
your panel then.
In practice, what this meant was that when I opened a file to load data, it would plot the data in a panel--and the menu clearing would overwrite part of the panel. I could only force the data to replot in the panel by minimizing and maximizing the form.

I asked over on the MSDN forums website, and while the helpful person didn't find me the solution, he got me thinking a bit more carefully, until I did find the solution. I added an event handler for the menu's VisibleChanged property, and had that invalidate the panel. It turns out that as soon as the menu gets done messing with the form, this event happens. I then put a panel1.Invalidate() call there, which forces a refresh of the panel.

UPDATE: It also helps to include the MouseLeave, Paint, and DropDownClosed events.

No comments:

Post a Comment