Solved

C# custom button redraw after form close

Posted on 2013-10-22
3
630 Views
Last Modified: 2013-10-27
Experts:

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.

Any ideas?
0
Comment
Question by:piattnd
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39591589
Do you have a link to the source for these buttons?
0
 
LVL 12

Accepted Solution

by:
piattnd earned 0 total points
ID: 39591619
I actually just figured this out, but here is the link:

http://manixzero.blogspot.com/2012/12/create-custom-c-buttons-like-ms-office.html

Basically, he has the mouse up event set to put the color to that of the mouse over event.  A mouse up event should only happen if the user performed a full click on the control or moused down and moved off the control, this it should be set to the inactive/mouse out color.  I modified the code and it works like a charm.  For buttons with quick actions, like a refresh button, you see a flash right after clicking the control, but I'm OK to settle with that.
0
 
LVL 12

Author Closing Comment

by:piattnd
ID: 39603607
Answered my own question.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question