Windows Develop Bookmark and Share   
 index > Windows Forms Designer > MouseHover Event for Form Buttons (help please)
 

MouseHover Event for Form Buttons (help please)

This is my 1st time at using certain MouseEvents,what i'm want to do is this...The buttons i have now the Form and ToolStrip all have a Black BackColor..What i'm looking to do is when the persons mouse "RollsOver" a Button the Background color will change...

The over all Effect i'm trying to Achive is similar to the Windows Media Player Beta 11 the button is Black ,but when Rolled over it Lights up!!

Can someone help me with this cant seem to find any good example in any of my books to achive this look...

Thxs in advance

PS: here is some code i'v got to work partly..lol seems the only time the Button lights up Red is when the Mouse touches the very outer right side of the button...Not even the button itself,when i roll the mouse over the button it turns black..But move the button to almost off the button (Right side) it lights up..lol

Just Noticed the Button Lights up any time you touch the Form and not the Button itself..lmao

protected override void OnMouseHover(EventArgs e)

{

base.OnMouseHover(e);

button1.BackColor = Color.Red;

}

protected override void OnMouseLeave(EventArgs e)

{

base.OnMouseLeave(e);

button1.BackColor = Color.Black;

}

Rattlerr  Wednesday, July 19, 2006 2:17 AM
I think this may have been better asked in "Windows Forms General" rather than "Windows Forms Designer" Anyways, MouseHover events don't fire until the mouse stops moving, so you need to use the MouseEnter event instead. MouseEnter fires as soon as the mouse is over your control..

Rich.C  Wednesday, July 19, 2006 11:25 AM

I wouldnt think the Button Type would matter much but i'm using a PopUp Button,set to the Forms BackColor of Black...

But anyways that didnt work either...I tried OnMouseCaptureChange,funny thing is i could click once on the form and the button would change colors back and forth..lol Just not the button itself..

Rattlerr  Wednesday, July 19, 2006 3:27 PM
Pardon my ignorance, but what is a PopUp Button ?
Rich.C  Wednesday, July 19, 2006 3:38 PM

You have 4 Standard "Flat Style" Buttons to choose from in the Properties of the button:

Flat,PopUp,Standard & System..

PopUp button is Flat just like the Flat Button itself the Difference is the Flat button will have a small Adjustable Border Width that goes around the button itself...

If you use the PopUp Button you wont get the Border around the Button,What happens is when the user's mouse runs over the Button it "hence" PopsUp Exposing a slight beveling of the button...

Rattlerr  Wednesday, July 19, 2006 3:43 PM
Ok, sorry I thought you were talking of a different class other than Button. Re-reading your original post, it has just dawned on me that your OnMouseBlah code is in your Form's class not the Button class, so it will be tracking when the mouse enters/leaves the form.

You have 2 options:
1. Subclass Button and override OnMouseEnter and OnMouseLeave there
or
2. Attatch event handlers to your button
Rich.C  Wednesday, July 19, 2006 4:05 PM

This is what i'v tried with No Prevail..lol

private void button1_OnMouseHover(object sender, MouseEventArgs e)

{

base.OnMouseHover(e);

if (button1.BackColor == Color.Black)

button1.BackColor = Color.DarkBlue;

else

button1.BackColor = Color.Black;

}

Even tried:

private void button1_OnMouseEnter(object sender, MouseEventArgs e)

{

base.OnMouseEnter(e);

if (button1.BackColor == Color.Black)

button1.BackColor = Color.DarkBlue;

else

button1.BackColor = Color.Black;

}

Rattlerr  Wednesday, July 19, 2006 4:16 PM
Ok, with that code you are going for my option 2 above. So you need to make sure you have added both the handlers (easiest using the designer) to your button and not to your form. Check the properties of both the form, and the button in the design view, to make sure the events are wired to where you think they are.

If you go for my option 1 above, create a subclass of Button, and override OnMouseEnter and OnMouseLeave. This option is probably your better long term option as it gives you a control you can re-use in other places.

For either option, just set the color to its new value, you don't need the if else.
Rich.C  Wednesday, July 19, 2006 4:40 PM

Don't use MouseHover for this. Mouse Hover only fires ONCE on a control and only when the mouse stops moving over the control. If you want it to fire again, you must call ResetMouseEventArgs.

As Rich said, MouseEnter and MouseLeave are the way to go.

Ken

Ken_Bussell  Wednesday, July 19, 2006 4:46 PM

Form color is Black and the Button BackColor is Black...I want it like that so it Blends in with the Form itself...So on the Mouse Enter the Button PopsUp out of the Form so to Speak...

Basically the same method as in the Windows Media Player 11 Beta..

Rattlerr  Wednesday, July 19, 2006 4:52 PM

Here is the code you need.

private void button3_MouseEnter(object sender, EventArgs e)

{

button3.BackColor = Color.Black;

}

private void button3_MouseLeave(object sender, EventArgs e)

{

button3.BackColor = Color.Gray;

}

In your code you are calling base.MouseLeave etc. You don't call the base method unless you have a derived class. Make sure you tie your events to the correct event handler names and you are set.

Ken_Bussell  Wednesday, July 19, 2006 5:00 PM

MouseEnter += new EventHandler(button1_MouseEnter); 

 

From what it seems if i use the Flat instead of the PopUp the MouseOver Works fine..But as soon as i go to use the PopUp...bam stops working..

Rattlerr  Wednesday, July 19, 2006 5:19 PM
I may finally understand what you're trying to do
I thought you were making a rollover button that went from black to blue. But it seems you actually want the button to change color when the mouse enters the form. Yes?

So you need to handle the form's mouse enter and mouse leave events.
The added complication is that when the mouse enters the button, you will receive a mouse leave event from the form which would reset the button color back to black.
To overcome this, add another pair of event handlers to the button so the button doesn't revert to black when the mouse is over it. If you have other controls on your form then these will have a similar adverse effect.

I don't see how simply changing the FlatStyle value would mess this up - what version of .NET and OS are you using?

Rich.C  Wednesday, July 19, 2006 7:50 PM
I have .NET 1.1, 2.0 and 3.0 Beta Installed..As for the O/S is XP Professional with Media Center 2005
Rattlerr  Thursday, July 20, 2006 2:18 AM

You can use google to search for other answers

Custom Search

More Threads

• Change control cursor to cursor from .cur in designer
• Iextenderprovider and Collections
• Inherited controls moves
• pattern: set, get property
• create property on a form that is type: structure
• MasterPage : having trouble to control column width of the model matrix
• how print datagridview directly?
• Load problem
• Changing font of a control at design time 'breaks' the form
• Developing a Basic WinForms application - need help on design