I want to show a form, but I don't want any of the controls focused. For example, right now when the form is shown, the button with tabindex 0 is focused. I want the form itself, not a button, to be focused, so that if the user accidentally hits a key it won't do anything. Is this possible? Thanks!
8 Answers
You should likely place focus somewhere relatively harmless initially, such as the "Cancel" button if your form includes such a thing.
A workaround that I have testet;
private void Form1_Shown(object sender, EventArgs e)
{
textBox1.TabStop = false;
textBox1.Focus();
textBox1.Left = -300;
}
This "hides" the textbox with focus by moving it out of the visible area. By doing it in this hackish manner, the textbox retains the ability to have focus.
[edit] This (obviously) requires you to have a textbox named textBox1 on your form that is not used for anything else.

- 722
- 3
- 7
Try this: Add a Panel
control to your form and keep the default settings. The panel can have any size and be positioned anywhere. I'll call this member m_panel
. In your constructor, set your form's ActiveControl
property to m_panel
. Lastly, make sure that the panel's TabStop
property is set to false
(which it is by default).
When the form loads, m_panel
will get the focus. However, since the panel has no border and has the same color as the form background, there is no indication that it exists, so you can effectively say that the form itself has focus, as you wanted. When the user first hits Tab or clicks in a control, the panel will be out of the equation (since TabStop
is false
) and things will work as normal.
Note: you can also use an empty Label
rather than a Panel
, whatever suits you. You can even use one of your existing labels. Remember to use ActiveControl
to specify the control of interest, or focus it explicitly by calling Focus
, since it will not get focus automatically.

- 44,214
- 4
- 43
- 69
You can set the TabStop property to false for each control but you won't be able to tab through the controls then. That may or may not be a good solution for you but it should keep any of the controls from receiving input until the user clicks on it.

- 7,913
- 1
- 23
- 28
You can MyInvisibleLabel.Select();
in _Load
or, if you have one, just focus the Exit/Cancel/Close button.

- 171,639
- 30
- 264
- 288
no you can't. There's a Workaround BTW. Add an hidden control to the form (a textbox for example) and do
TextBox1.Focus();
in the Form_Shown or Form_Load event.

- 666
- 3
- 15
-
Yep, but be aware that this can be a little confusing to users who move around with the tab key a lot since the focus will sometimes jump to somewhere he can't see. And make sure that you set the tabindex of the hidden control properly. – Hans Olsson Jun 04 '10 at 13:52
-
obviously, it's better to set TextBox1.TabStop = false. – vaitrafra Jun 04 '10 at 14:00
-
I tried this workaround out, and it does not seem to work. Focus seems to default back to the first visible control in the taborder – LaustN Jun 04 '10 at 14:02
-
I don't think a hidden (`Visible` = false) control can receive focus, based on the docs for `Control.CanFocus`. – Charlie Jun 04 '10 at 14:06
-
omg, i have to remember that things that sound obvious to me are not that obv. to others. That "hidden" was not meant as "visible=false" but "out of sight" relatively to the form position and max extension... something like TextBox1.Left=-1000. – vaitrafra Jun 04 '10 at 14:51
vaitrafra's answer is sufficient, but if you happen to have a label on your form, you could just set the focus to that as well. The accepted answer here provides a bit more insight into why you can't ensure that every control on the form does not have the focus.

- 1
- 1

- 106
- 1
- 3
The simplest solution is:
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
ActiveControl = null;
}
Alternatively, if all the controls are inside a panel docked to fill, you can focus the panel:
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
pnlMain.Focus();
}
With either approach, pressing the tab key will then focus the first control in the tab order.

- 688
- 9
- 11