[C#] BluNoFocusButton

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

[C#] BluNoFocusButton
  1. Unread #1 - Sep 8, 2016 at 8:01 PM
  2. Blupig
    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

    Blupig BEEF TOILET
    $5 USD Donor

    [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.
  3. Unread #2 - Feb 20, 2017 at 11:15 PM
  4. tonyhh
    Joined:
    Mar 19, 2015
    Posts:
    21
    Referrals:
    0
    Sythe Gold:
    8

    tonyhh Newcomer

    [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);
            }
    
    
        }
    }
    
     
  5. Unread #3 - Feb 20, 2017 at 11:21 PM
  6. Blupig
    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

    Blupig BEEF TOILET
    $5 USD Donor

    [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!
     
  7. Unread #4 - Feb 22, 2017 at 8:19 PM
  8. 70i
    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174

    70i Forum Addict
    Banned

    [C#] BluNoFocusButton

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

Users viewing this thread
1 guest


 
 
Adblock breaks this site