0

I am making a config editor form and have hit a bit of an issue, I put a lot of time into userfriendly and efficient design and therefor want the TabIndex to work perfectly to minimize use of mouse.

My problem is now when I try to tab through the controls I noticed the CheckBox was not gaining focus like if you press it with the mouse, this means I couldn't tab through and change their state directly from keyboard.

How do I make the CheckBox gain focus via TabIndex and TabStop so that I can just press Enter to change its state via the KeyUp event.

Below is a picture of my form, and next to it a picture of the TabIndex as well as a the code taken directly from the Form.Designer.cs class.

enter image description here

        // 
        // cbxDefaultPublic
        // 
        this.cbxDefaultPublic.AutoSize = true;
        this.cbxDefaultPublic.Location = new System.Drawing.Point(247, 12);
        this.cbxDefaultPublic.Name = "cbxDefaultPublic";
        this.cbxDefaultPublic.Size = new System.Drawing.Size(15, 14);
        this.cbxDefaultPublic.TabIndex = 1;
        this.cbxDefaultPublic.TabStop = true;
        this.cbxDefaultPublic.UseVisualStyleBackColor = true;

Please note that I had a hard time explaining this cause its a bit complicated and didn't know how to explain it so bare over with me if I got a few things wrong.

Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • 2
    The CheckBox control prefers to have Text in order to show the focus. – LarsTech Jan 19 '16 at 23:54
  • @GrantWinney Ok now I'm really confused, mostly cause the `SpaceBar` works, but what confuses me is that the visuals for the `CheckBox` doesn't change when it's in focus, like how controls (fx Buttons) turn slightly blue when they get focus. – Simon Jensen Jan 19 '16 at 23:56
  • @LarsTech I tested it with text, it seems the `CheckBox` stays gray while the text on it will have a dotted outline. – Simon Jensen Jan 20 '16 at 00:00
  • That's just the way the CheckBox was designed. – LarsTech Jan 20 '16 at 00:01
  • @GrantWinney What I'm trying to accomplish is to have the `CheckBox` change to the LightBlue as if you hover your mouse over it just like when buttons are in focus. – Simon Jensen Jan 20 '16 at 00:02
  • If you don't want to use text with the CheckBox, check out GotFocus event to change the appearance when it's focused, so you know it's focused. – jsuen Jan 20 '16 at 00:57

1 Answers1

0

With the help of the people commenting on my question I was able to get on the right track of what to do and what to search for.
Thanks to Grant Winney, LarsTech and JohnnyBoy for explaining to me how the CheckBox worked and what I needed to look at.

I found out that the CheckBox does not have a public highlight feature so I had to get creative.
What I did was I created a custom CheckBox and well.. might just show you the code :P

public class MyCbx : CheckBox {
    protected override void OnGotFocus(EventArgs e) {
        base.OnGotFocus(e);
        base.OnEnter(e);
        base.OnMouseEnter(e);
    }
    protected override void OnLostFocus(EventArgs e) {
        base.OnLostFocus(e);
        base.OnLeave(e);
        base.OnMouseLeave(e);
    }
    protected override void OnMouseLeave(EventArgs e) {
        if(!this.Focused) {//prevent it from losing highligh if control is in focus
            base.OnMouseLeave(e);
        }
    }
}

So I call the MouseEnter and Leave events when It gain or lose focus, this will make it change to a highlighted state.

Simon Jensen
  • 488
  • 1
  • 3
  • 19