Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Custom Button BackColor by Event issues

Posted on 2014-01-06
10
Medium Priority
?
109 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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 2000 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

Technology Partners: 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!

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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