Setting focus inside a Panel

  • Thread starter Thread starter jp2express
  • Start date Start date
J

jp2express

I have several applications that use panels as screens, but I can *not* seem
to set the focus for a Textbox.

Panel1.BringToFront()
Panel1_Textbox.Focus()
' do something with a control on Panel1
Panel2.BringToFront()
Panel2_Textbox.Focus()
' do something with a control on Panel2

When the applications are running, I can almost see the Textbox controls
receiving focus for an instant, then Nothing has focus!

How do I set the Focus on the Textbox controls ... and have it STAY there?
 
I have several applications that use panels as screens, but I can *not* seem
to set the focus for a Textbox.

Panel1.BringToFront()
Panel1_Textbox.Focus()
' do something with a control on Panel1
Panel2.BringToFront()
Panel2_Textbox.Focus()
' do something with a control on Panel2

When the applications are running, I can almost see the Textbox controls
receiving focus for an instant, then Nothing has focus!

How do I set the Focus on the Textbox controls ... and have it STAY there?

Normally,
textbox.focus gives focus. But if you lose focus for some reason and
still you want your textbox has the focus then you should:

Private Sub TextBox1_lostfocus(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles_ TextBox1.LostFocus

TextBox1.Focus()

End Sub

Therefore, when you textbox loses focus, it re-gains the focus.

Hope this helps.
 
I set the focus to the correct textbox in the OnShown() override, and have
not problems. If I do the same in the constructor, I believe it reverts back
to the first tabbed index. The alternative therefore would be to set the tab
index for the desired textbox to be first. Setting focus in the OnShown
method is more appropriate in my view however.
 
Looks to me like such a "trick" would cause you to be in a loop with it
impossible to get "out of" the textbox!
 
Looks to me like such a "trick" would cause you to be in a loop with it
impossible to get "out of" the textbox!
--
Terry









- Show quoted text -

If OP use "textbox1.focu"s within a if-then conditional statement that
means textbox loses focus in certain circumstances, the loop won't
occur.
 
IMHO such coding practises lead to nightmares. Figuring out why it's losing
the focus and fixing the problem seems more straight forward. If the
textbox should always receive the focus when the panel is shown, then it's
tab index should be 0 within the panel. If conditionaly, the focus might
need to go somewhere else, then the control with tab index 0, should send the
focus to someplace else on those conditions.
 
Back
Top