1

I have this problem... When my form opens, a button is focused. I don't want it bo me focused. I put on the form a little panel, that is not visible and set it as starting focus (also set tabstop = false). But still... the button is focused.

What could be the problem ?

I tried :

private void Form_Load(object sender, EventArgs e)
{
   panel.Focus();
}

But still nothing happens, the button is still focused.

Etrit
  • 865
  • 2
  • 14
  • 25
  • Remove focus from the button on load perhaps? – christopher May 29 '13 at 11:27
  • 2
    You may set [`AcceptButton`](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx) to `null` (`AcceptButton = null; //or any other button` instead of `panel.Focus();`). – Leri May 29 '13 at 11:28
  • No it's not a duplicate, because I tried many methods but still... the button is focused. Before writing this question i used the TabIndex to set the order of the controls but still... button is focused. – Etrit May 29 '13 at 11:41

5 Answers5

3

The Panel control derives from ContainerControl. Which provides common behavior for controls that act as containers for other controls. Which includes not ever wanting to take the focus. If you try to give them the focus they'll immediately pass it off to a child control.

There's a good reason for that, the user needs to be able to see which control has the focus. So he knows where the keyboard strokes go. And container controls don't have a way to display that. Nor do they do anything reasonable with a keystroke. You can certainly doctor a Panel control to make it a focusable control, this answer shows how.

But focus a bit on why you want to make it difficult for the user. Maybe you are a bit to focused on the "blemish" of a focus rectangle? If you really want to hide it then you can, it is pretty simple to do. Just add a dummy button and give it a negative Location property and a TabIndex of 0. Which moves it off the window, no longer visible to the user. But still capable of taking the focus. Solves the "blemish" problem, but at a risk of seriously confusing the user.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You can change the TabIndex of your controls so it will be focused in your wished order

WhileTrueSleep
  • 1,524
  • 1
  • 19
  • 32
0

I think your panel cannot be focussed. Try a control inside your panel.

ikwillem
  • 1,044
  • 1
  • 12
  • 24
0

If you want that the button should not have focus when you open the form, then you need to correct the TabIndex property. The TabIndex property has an integer as value which specifies the order in which the controls get focus when tab key is pressed. If the control has TabIndex set to 0 then change it to some other value.

Or

you can also set control TabStop property to false.

documentation for TabIndex and TabStop property.

Rahul
  • 5,603
  • 6
  • 34
  • 57
0

Property Named AcceptButtons of Form set it to (none)

Rahul
  • 5,603
  • 6
  • 34
  • 57
Bhavin
  • 240
  • 7
  • 19