2

I've searched a bit, and with google keyword "no" or "not" it get so general so I can't really find it

I have a textbox and I don't want the textbox to have the focus on load, so I've put the tabindex to 1 but it still get focus on load.

I know I could use method like Select = false but in my opinion it should not be required since the form itself should have focus on load not a textbox

Could use some help thank you

Mokmeuh
  • 865
  • 3
  • 11
  • 25
  • Which control has TabIndex = 0? – LarsTech Nov 07 '13 at 22:31
  • No control, I guess there's the problem but I don't want to create an invisible control to do that and It seems I can't but the tabindez = to the form itself – Mokmeuh Nov 07 '13 at 22:32
  • Is this Winforms, WPF, Web? It's not very clear why "nothing" should have focus when you are presenting a form to a user. – LarsTech Nov 07 '13 at 22:36
  • Sorry for me being unprecise, it's a simple windows form – Mokmeuh Nov 07 '13 at 22:38
  • You can try an offscreen TextBox or a [Selectable Panel](http://stackoverflow.com/a/11570670/719186) maybe. – LarsTech Nov 07 '13 at 22:42
  • I know it will work, I'm just wondering about not taking memory space with a non used textbox :S I know it's not alot it's just about how it's coded I don't know if you get it – Mokmeuh Nov 07 '13 at 22:43

1 Answers1

1

You can try this code:

Protected Overrides Sub OnLoad(e As EventArgs)
  MyBase.OnLoad(e)

  Dim focusBox As New TextBox
  focusBox.Location = New Point(0, -100)
  AddHandler focusBox.Leave, Sub()
                               focusBox.Dispose()
                             End Sub
  Me.Controls.Add(focusBox)
  focusBox.Select()
End Sub

It won't pollute the designer and as soon as a control gets a focus, it will dispose of itself.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • haahhahaah, I know you don't understand why it's funny to me but yea thank you sir, I guess it will do the trick :) – Mokmeuh Nov 07 '13 at 23:57