- Joined
- Nov 23, 2006
- Sythe Gold
- 1,609
- Messages
- 7,145
- Location
- Canada
- Discord Unique ID
- 178533992981594112
Hey this is just a simple control I had to make myself for a project at work. The problem was that the regular Winforms buttons gain focus when you click them. I realized that labels don't gain focus so I made a label with some button behaviour.
If you use this please link back to this thread or give me credit.
Here's the code, it might be useful to someone:
If you use this please link back to this thread or give me credit.
Here's the code, it might be useful to someone:
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BluNoFocusButton
{
public class BluNoFocusButton : Label
{
private Color hoverColour;
private Color backColour;
public BluNoFocusButton()
{
this.BackColor = Color.Gainsboro;
this.HoverColour = Color.White;
backColour = this.BackColor;
}
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
backColour = value;
}
}
public Color HoverColour
{
get { return hoverColour; }
set { hoverColour = value; }
}
protected override void CreateHandle()
{
base.CreateHandle();
this.AutoSize = false;
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
base.BackColor = hoverColour;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
base.BackColor = backColour;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.BackColor = backColour;
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.BackColor = hoverColour;
base.OnMouseUp(e);
}
}
}













