• Log in or sign up for an account here: https://www.sythe.org/login

[C#] BluNoFocusButton

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Location
Canada
Discord Unique ID
178533992981594112
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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:

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);
        }


    }
}
 
I'm actually new to c#, but I made a few changes, Idk if they work tbh lol. But it's all just practice.
Code:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace BluNoFocusButton
{
    public class BluNoFocusButton : Label
    {
        public Color HoverColour { get; set; } = Color.White;
        public Color BackColour { get; set; } = Color.Gainsboro;


        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);
        }


    }
}
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
I'm actually new to c#, but I made a few changes, Idk if they work tbh lol. But it's all just practice.
Code:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace BluNoFocusButton
{
    public class BluNoFocusButton : Label
    {
        public Color HoverColour { get; set; } = Color.White;
        public Color BackColour { get; set; } = Color.Gainsboro;


        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);
        }


    }
}

On first glance it should work the same way, I just prefer to have private variables separate from properties to ensure that they're not being toyed with. It's an extra precaution I've gotten in the habit of applying for really large projects that tend to modify things when you don't want them to. Good edits though and I hope your journey into C# is going well!
 
Why would I want my buttons not to gain focus when clicked?
 
duelarena.gg
Back
Top