Forcing the dotted focus box on a button?
-
Hi, Before I go into detail, is there any way to force a button to get that dotted box on it that shows it's in focus? Just saying
btn1.Focus();
isn't working for me. Btn1 is definitely the thing being focused on, but you wouldn't know by just looking at it. And it seems that the only way to get a button to show highlight is to Tab into it. So, the weird details... :doh: Say I put a custom button on my form (and gave it tabstop 1). I then add a textbox with tabstop 2. And finally another button (tabstop 3). If I click in the textbox and press the Tab key, the 2nd button gets that dotted line, no problem. Same goes if I press Shift-Tab (the 1st button shows focus). I made the textbox so that it recognizes if the up or down keys are hit, so when I'm in the textbox and press the up key, I say:if (e.KeyCode == Keys.Up){SendKeys.Send("+{Tab}");}
and have no problem here either - the first button shows focus. But if down is pressed: if(e.KeyCode == Keys.Down){SendKeys.Send("{Tab}");}
it freaks out and everything freezes so that I have to open task manager to close it down. One other thing - if a button has been highlighted (tabbed into) once, saying justbtn1.Focus();
is enough. But, of course, I can't be sure that tab will always be pressed. Hope this makes sense. Thanks for any help on this! Mel -
Hi, Before I go into detail, is there any way to force a button to get that dotted box on it that shows it's in focus? Just saying
btn1.Focus();
isn't working for me. Btn1 is definitely the thing being focused on, but you wouldn't know by just looking at it. And it seems that the only way to get a button to show highlight is to Tab into it. So, the weird details... :doh: Say I put a custom button on my form (and gave it tabstop 1). I then add a textbox with tabstop 2. And finally another button (tabstop 3). If I click in the textbox and press the Tab key, the 2nd button gets that dotted line, no problem. Same goes if I press Shift-Tab (the 1st button shows focus). I made the textbox so that it recognizes if the up or down keys are hit, so when I'm in the textbox and press the up key, I say:if (e.KeyCode == Keys.Up){SendKeys.Send("+{Tab}");}
and have no problem here either - the first button shows focus. But if down is pressed: if(e.KeyCode == Keys.Down){SendKeys.Send("{Tab}");}
it freaks out and everything freezes so that I have to open task manager to close it down. One other thing - if a button has been highlighted (tabbed into) once, saying justbtn1.Focus();
is enough. But, of course, I can't be sure that tab will always be pressed. Hope this makes sense. Thanks for any help on this! Mel -
It just hit me that the buttons I'm using are custom and descend from the checkbox class. Hopefully this will make more sense out of things, but I'd still like to know how to make it look in focus. Thanks! Mel
hi! :) you can try this one:
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if(this.Focused) { Rectangle rec = new Rectangle(1, 1, Width - 2 , Height - 2); } ControlPaint.DrawFocusRectangle(e.Graphics, rec, ForeColor, BackColor); } }
hope that helps! :)