WinForms Dummy Question

This has got to be one of the dumbest posts I've done but for the life of me and my co-horts we can't figure it out.

We have a TextBox sitting on a UserControl sitting on a Panel sitting in a Form.

$10 to the first person to tell me how to set the focus to the TextBox.

We've tried:

  • txtBox.Focus();
  • ActiveControl = txtBox;
  • txtBox.Select();

Nothing works. WTF? This has *got* to be a simple thing right?

Published Tuesday, June 10, 2008 2:24 PM by Bil Simser
Filed under:

Comments

# re: WinForms Dummy Question

Tuesday, June 10, 2008 4:55 PM by AndrewSeven

Can you see the hidden field named __LASTFOCUS?

If so does it have the right client ID?

To see if its a timing issue, try calling SetFocus from the page in the prerender event.

Page.SetFocus(control)

# re: WinForms Dummy Question

Tuesday, June 10, 2008 4:57 PM by AndrewSeven

Doh! Ignore that, I thought it was webforms

# re: WinForms Dummy Question

Tuesday, June 10, 2008 5:01 PM by Bil Simser

@Andrew: WinForms, not Web.

# re: WinForms Dummy Question

Tuesday, June 10, 2008 5:15 PM by MikeBosch

Form.Controls[indexOfPanel].Controls[indexOfUserControl]...etc until you get to the textbox then FOCUS

# re: WinForms Dummy Question

Tuesday, June 10, 2008 5:22 PM by None

this.userControl11.Controls["textbox2"].Focus();

# re: WinForms Dummy Question

Tuesday, June 10, 2008 5:24 PM by Alan

How about setting the tab order?

# re: WinForms Dummy Question

Tuesday, June 10, 2008 6:03 PM by Cory

Did you try to set the focus after the Panel element on the form has been created, or during?

In some languages, you have to register an event listener to ensure your element is actually created, and in some cases, the whole 'parent' element is also created in order to set focus or blur.

# re: WinForms Dummy Question

Tuesday, June 10, 2008 6:47 PM by Matt Hamilton

Couple of possibilities, Bil.

1. Controls are private by default, I think. Check the TextBox's "Modifier" in the properties box and see if you need to make it public.

2. Is the code in your form's class? If so, it'd be userControl1.textBox1.Focus();

Of course, I'm assuming that these are compile-time issues and not runtime ones. Maybe it's compiling just fine and not giving the TextBox focus when the code is called.

# re: WinForms Dummy Question

Tuesday, June 10, 2008 8:23 PM by Steve

just created a test app..had to screw around with tabindexes to have it act like you said, then tweaked them back to get it correct like you want..

set tabindex of the panel to 0

on the user control set tabindex of textbox 0

works for me..

# re: WinForms Dummy Question

Tuesday, June 10, 2008 9:15 PM by secretGeek

Hmm -- the Focus() method is working fine for me, when it happens in (for example) a button click handler.

But if it's in the form load then it doesn't have the desired effect.

Try putting it in form_activated.

(might need some other logic there so it doesn't fire every time the form gets focus)

# re: WinForms Dummy Question

Tuesday, June 10, 2008 9:55 PM by Charles

Set txtBox.TabStop to false in Form_Load then txtBox.Focus() and then txtBox.TabStop to true in Form_Activate to get the default tab behavior back. Worked for me...

# re: WinForms Dummy Question

Wednesday, June 11, 2008 10:04 AM by rajbk

It works from a button event handler but does not work in the form Load handler. Instead, use the Form Shown event.

Raj

# re: WinForms Dummy Question

Thursday, June 19, 2008 2:14 AM by Matt Hamilton

THE SUSPENSE IS KILLING ME!

# re: WinForms Dummy Question

Tuesday, July 15, 2008 3:37 PM by Khanphaphone Vongsavanthong

this.ActiveControl = txtBox.Control;

Have a nice day.

# re: WinForms Dummy Question

Wednesday, July 16, 2008 3:04 PM by Mark

Textbox is on user control is not exposed at the form level to be called. You would have to expose something on the usercontrol. If the textbox is the only control on the usercontrol then usercontrol1.setfocus otherwise expose a method on the usercontrol to set the focus.

Mark

# re: WinForms Dummy Question

Tuesday, March 29, 2011 4:19 PM by Rehana

Charles, your solution :

Set txtBox.TabStop to false in Form_Load then txtBox.Focus() and then txtBox.TabStop to true in Form_Activate to get the default tab behavior back.

worked for me :)

Thanks ton !!

Leave a Comment

(required) 
(required) 
(optional)
(required)