Adblock breaks this site

[C#] BluNoFocusButton

Discussion in 'Programming General' started by Blupig, Sep 8, 2016.

  1. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    [C#] BluNoFocusButton

    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);
            }
    
    
        }
    }
    
     
    Ritysayo likes this.
  2. tonyhh

    tonyhh Newcomer

    Joined:
    Mar 19, 2015
    Posts:
    21
    Referrals:
    0
    Sythe Gold:
    8
    [C#] BluNoFocusButton

    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);
            }
    
    
        }
    }
    
     
  3. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    [C#] BluNoFocusButton

    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!
     
  4. 70i

    70i Forum Addict
    Banned

    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174
    [C#] BluNoFocusButton

    Why would I want my buttons not to gain focus when clicked?
     
< Formal education in programming | >


 
 
Adblock breaks this site