Focusing a TextBox

  • Thread starter Thread starter jp2msft
  • Start date Start date
J

jp2msft

I'm displaying a form for our employees that requires them to log in before
they use it.

After they have been logged in and authenticated, the login form closes and
I tell the application to focus the text box that should accept input next.

Unfortunately, this never happens.

I can press the Tab key once, and focus goes immediately to the text box,
but it is not there by default.

Is there a trick to getting the cursor to default to a text box control
without requiring a Tab press?

If not, is there a way that I can capture any key that is pressed and copy
that to the text box?

If it makes a difference, this is an MDI Child form, but I don't see how
that matters.

My quasi code is as follows:

void LogYouIn()
{
using (LoginForm login = new LoginForm())
{
if (login.ShowDialog(this) == DialogResult.OK)
{
TextBox1.Focus();
}
}
}
 
I set the tab index to 0 (down from 3), but the form still does not come up
with the cursor in TextBox1.
 
At the time you call the focus method, has the textbox been created?
Loaded? Shown? I would think that focus call would go in the owning
form's Load override.
 
At the time you call the focus method, has the textbox been created?
Loaded? Shown? I would think that focus call would go in the owning
form's Load override.

Shown is the most important part. Even if the text box has been created, if
it's not visible it can't receive focus.
 
The form has already been created, and everything is displayed within the MDI
parent.

It looks like I get this problem a lot with MDI forms.

BTW: Glad you aren't a commie.
 
Back
Top