• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 120
  • Last Modified:

C# Custom Button BackColor by Event issues

Experts:

I have a custom button class I've created and I'm overriding mouse events (enter, leave, click, down, ect) and controlling the visual appearance (namely backcolor) on the events.

I noticed if I have a dialog box pop up after a click of the button, the color assigned to the button as the "mousedown" event stays that way, even if the cursor isn't over the control.  My assumption is that because the dialog prompt is on the same thread, it stop processing the location and state of the mouse at the time of dialog display.

Does anyone have any input on how to prevent this?  I really don't want to manually create new threads for message boxes to be displayed in.
0
piattnd
Asked:
piattnd
  • 5
  • 4
3 Solutions
 
SStoryCommented:
If you click a button and a dialog box pops up, perhaps it is because it is modal (takes control until finished, versus one that is non-modal and doesn't. Is this a possibility?

Check to see who the owner of MsgBOX is also.
0
 
piattndAuthor Commented:
I have a few cases in my program that I need to use a ShowDialog() instead of Show() to prevent the user from continuing in the main form before addressing the prompt being displayed.
0
 
SStoryCommented:
What if you called .Refresh
or did an
 Application.DoEvents()

during the click before you open the dialog?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
piattndAuthor Commented:
If I do that, the state right before the dialog opens is "mouseover".  I then address the dialog box, but the button doesn't realize (and there's no trigger to tell it) that I'm no longer over the button and I've left/changed focus.  I tried the "onLostFocus", but it doesn't appear to actually lose focus.
0
 
SStoryCommented:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus%28v=vs.110%29.aspx
says

The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events.

So does Leave event work?

Also, although you are no longer over the button, you are over the form.  I suppose if you had to you could set a flag when you go over that button and if mousemove on form and flag set clear flag and tell button to refresh, but this shouldn't be necessary.
0
 
piattndAuthor Commented:
The leave event does work.  If I have only enter/leave events and I enter and leave the button, the desired effect is achieved (the color changes appropriately).  I also have a mouse down and up event to set the color to a third color on mouse down (clicking) and set the color to the mouse enter color upon mouse up (assume the mouse is still over the control when the mouse comes up and the click completes).

If you perform the click on a button, say a refresh button, where the action does not require a window to pop up, everything works as expected and the button leave event triggers when you leave the control.  When you open a Dialog Message though, as previously mentioned, the leave event is never triggered because the thread is now focused on the Form or Message I've popped up.

For now I've decided to set the MouseUp color back to the leave color and I'll live with the fact that a button that is clicked and not left will assume the color of a button that was left.  That is, until I find a solution.

I appreciate your input.  This is my first project messing with custom form objects (particularly buttons), so it's very possible I'm simply missing an override event or something simple like that.  If you think of anything else, let me know!
0
 
SStoryCommented:
I guess where I am missing it is that it seems to me if you are calling the code to open the dialog box, you could fake a call to the event or sub that resets it and manually tell the button to change colors. Right? And for good measure call it's refresh or do a DoEvents if necessary to be sure it updates right before that form loads.

Does the dialog open when you click the button?  i.e. what part of the button event life cycle is related to when the dialog button opens?
0
 
piattndAuthor Commented:
You are correct, that I could call the opening form and tell it to tell the button to set itself back to the default state, but because these controls could possibly be packaged and distributed internally for other developers to use, I don't want to have any type of "special instructions" so to speak with using them.

I hope that makes sense.  :)
0
 
piattndAuthor Commented:
If you want to see what I'm referring to, you can take a look at this guys' blog article.  He's the one who's helped me get to where I'm at, though I've made a few adjustments to fix what I found to be bugs (though it could be specific to me).

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

If you read the comments there, I noted that he hadn't overridden some of the events, so some things weren't working properly.  Give it a shot if you'd like and let me know if you experience the same thing.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now