Focusing a TextBox

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();
}
}
}
 
J

jp2msft

I set the tab index to 0 (down from 3), but the form still does not come up
with the cursor in TextBox1.
 
N

not_a_commie

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.
 
J

Jeff Johnson

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.
 
J

jp2msft

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top