Solved

C# Custom Button BackColor by Event issues

Posted on 2014-01-06
10
80 Views
Last Modified: 2016-06-13
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
Comment
Question by:piattnd
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39760424
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
 
LVL 12

Author Comment

by:piattnd
ID: 39760429
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
 
LVL 25

Expert Comment

by:SStory
ID: 39760512
What if you called .Refresh
or did an
 Application.DoEvents()

during the click before you open the dialog?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 12

Author Comment

by:piattnd
ID: 39760834
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
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
ID: 39761960
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
 
LVL 12

Author Comment

by:piattnd
ID: 39762324
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
 
LVL 25

Assisted Solution

by:SStory
SStory earned 500 total points
ID: 39763695
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
 
LVL 12

Author Comment

by:piattnd
ID: 39763816
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
 
LVL 12

Assisted Solution

by:piattnd
piattnd earned 0 total points
ID: 39763828
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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