Solved

C# Custom Button BackColor by Event issues

Posted on 2014-01-06
10
66 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
Comment Utility
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
Comment Utility
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
Comment Utility
What if you called .Refresh
or did an
 Application.DoEvents()

during the click before you open the dialog?
0
 
LVL 12

Author Comment

by:piattnd
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now