C# custom button redraw after form close
Posted on 2013-10-22
I recently came across some custom buttons made by someone in a blog post. Simply put, the custom button is a custom drawn graphic (rectangle) with color schemes on events, such as MouseOver, MouseClick, and none.
I needed to allow my form to display another form and keep focus of that form until it was addressed (similar to a message box). To do this, I use the ShowDialog() method of the form. The problem this creates is the button in the parent form still thinks the mouse is over the control after the child form is closed, so it shows the graphic it should when the mouse is over the control. If I do a form.Show() instead of ShowDialog(), I do not get this problem, because the parent form does not sit and wait to process further events until after the child window is closed, but it also allows the parent window to be focused, which I do not want. I modified their solution to add a "lostfocus" event handler, so I can now reset the button's color formatting by clicking another object in the form.
So what's causing the problem? I believe it's the way the button is programmed to communicate it's current state (mouse over, mouse down, mouse up, etc). The person who posted a blog about using these buttons uses a public enum called "mouse state" (button.mstate). They then create eventhandlers and set the mouse state equal to a particular state, then invalidate the control to force a redraw. It is in the redraw process where they detect the mouse state and change the colors as appropriate to the matching mouse state.
Having thrown all that out there, I know this doesn't happen with classic C# buttons, so I'm curious your thoughts on how to resolve this issue.