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?

15 Comments

  • 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)

  • Doh! Ignore that, I thought it was webforms

  • @Andrew: WinForms, not Web.

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

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

  • How about setting the tab order?

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

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

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

  • 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)

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

    Raj

  • THE SUSPENSE IS KILLING ME!

  • this.ActiveControl = txtBox.Control;

    Have a nice day.

  • 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

  • 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 !!

Comments have been disabled for this content.