[ACCEPTED]-Custom button in C#: How to remove hover background?-button

Accepted answer
Score: 44
btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;


Score: 11

The grey background is due to the setting 15 of "System.Windows.Forms.FlatStyle.Flat", it's 14 the default behaviour, since it need to 13 highlight the button when you hover. To 12 eliminate that, you might have to write 11 a custom button class, inherit from the 10 original button and do some custom painting 9 to achieve that.

Btw, instead of setting 8 "enabled" in MouseHover, you should do it 7 in MouseEnter. MouseEnter and MouseLeave 6 is a pair which indicate whether is the 5 mouse is within the button or not, and it's 4 fired once per entry/exit. Where as MouseHover 3 is fire whenever the mouse moved within 2 the button, which create unnessecery repeated 1 setting of "enabled".

Score: 2

I've solved this using a label instead of 3 a button.

// imageListButtons
this.imageListButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListButtons.ImageStream")));
this.imageListButtons.TransparentColor = System.Drawing.Color.Transparent;
this.imageListButtons.Images.SetKeyName(0, "close_normal");
this.imageListButtons.Images.SetKeyName(1, "close_hover");
// lblClose
this.lblClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblClose.BackColor = System.Drawing.Color.Transparent;
this.lblClose.ImageKey = "close_normal";
this.lblClose.ImageList = this.imageListButtons;
this.lblClose.Location = new System.Drawing.Point(381, 7);
this.lblClose.Margin = new System.Windows.Forms.Padding(0);
this.lblClose.Name = "lblClose";
this.lblClose.Size = new System.Drawing.Size(12, 12);
this.lblClose.TabIndex = 0;
this.lblClose.MouseLeave += new System.EventHandler(this.lblClose_MouseLeave);
this.lblClose.MouseClick += new System.Windows.Forms.MouseEventHandler(this.lblClose_MouseClick);
this.lblClose.MouseEnter += new System.EventHandler(this.lblClose_MouseEnter);

private void lblClose_MouseEnter(object sender, EventArgs e)
    lblClose.ImageKey = "close_hover";

private void lblClose_MouseLeave(object sender, EventArgs e)
    lblClose.ImageKey = "close_normal";

private void lblClose_MouseClick(object sender, MouseEventArgs e)

PS: notice that I'm using now a 2 two state button, instead of three. It is 1 intended (I know that I still can use three).

Score: 2

create Mouse Enter event which is given 2 below.

private void forAllButtons_MouseEnter(object sender, EventArgs e)
    Button b = (Button)sender;
    b.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;

then assign this event to all the 1 buttons.

Happy programming :)

Score: 2

I have got one suggestion.Create your own 6 button class deriving from Button.Then override 5 the MouseEnter event in that.Just remove 4 the code for calling the base implementaion.


PS: You 3 won't be able to use the MouseEnter event 2 outside the derived class (e.g. a project 1 using this control)

Score: 1

Hi you simply can apply these changes to 4 your button easily using these two lines 3 of codes.

  1. Set the button's FlatStyle to Flat

    this.btnClose.FlatStyle = FlatStyle.Flat;
  2. Set 2 the button's MouseOverBackColor to Transparent

    this.btnClose.FlatAppearance.MouseOverBackColor = Color.Transparent;

Hope 1 this will help. Thanks

Score: 0

You can also stop changing color of button 3 by deselecting IsHitTestVisible option in 2 Button Properties>common> IsHitTestVisible Maybe 1 this can also help ...

More Related questions