Solved

C# Custom Button BackColor by Event issues

Posted on 2014-01-06
10
98 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

632 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