<p>Say I have a <code>Textbox</code> nested within a <code>TabControl</code>.  </p>

<p>When the form loads, I would like to focus on that <code>Textbox</code> (by default the focus is set to the <code>TabControl</code>).</p>

<p>Simply calling <code>textbox1.focus()</code> in the <code>Load</code> event of the form does not appear to work.  </p>

<p>I have been able to focus it by doing the following:</p>

<pre><code> private void frmMainLoad(object sender, EventArgs e)
 {
     foreach (TabPage tab in this.tabControl1.TabPages) 
     {
         this.tabControl1.SelectedTab = tab;
     }
 }
</code></pre>

<p><strong>My question is:</strong></p>

<p>Is there a more elegant way to do this?</p>

[{'answer_id': 48719, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 7, 'selected': True, 'text': "<p>The following is the solution:</p>\n\n<pre><code>private void frmMainLoad(object sender, EventArgs e)\n{\n    ActiveControl = textBox1;\n}\n</code></pre>\n\n<p>The better question would however be why... I'm not entirely sure what the answer to that one is.</p>\n\n<p>Edit: I suspect it is something to do with the fact that both the form, and the TabControl are containers, but I'm not sure.</p>\n"}, {'answer_id': 634852, 'author': 'Korby', 'author_id': 76687, 'author_profile': 'https://Stackoverflow.com/users/76687', 'pm_score': 4, 'selected': False, 'text': "<p>Try putting it in the <code>Form_Shown()</code> event. Because it's in a container, putting in the Form_Load or even the Form() constructor won't work.</p>\n"}, {'answer_id': 986845, 'author': 'bdwakefield', 'author_id': 55053, 'author_profile': 'https://Stackoverflow.com/users/55053', 'pm_score': 1, 'selected': False, 'text': '<p>You just need to add the Control.Select() for your control to this code. I have used this to set focus on controls during validation when there are errors.</p>\n\n<pre><code>private void ShowControlTab(Control ControlToShow)\n    {\n        if (!TabSelected)\n        {\n            if (ControlToShow.Parent != null)\n            {\n                if (ControlToShow.Parent.GetType() == typeof(TabPage))\n                {\n                    TabPage Tab = (TabPage)ControlToShow.Parent;\n                    if (WOTabs.TabPages.Contains(Tab))\n                    {\n                        WOTabs.SelectedTab = Tab;\n                        TabSelected = true;\n                        return;\n                    }\n                }\n\n                ShowControlTab(ControlToShow.Parent);\n            }\n        }\n    }\n</code></pre>\n'}, {'answer_id': 2355348, 'author': 'Mikhail G', 'author_id': 283508, 'author_profile': 'https://Stackoverflow.com/users/283508', 'pm_score': 2, 'selected': False, 'text': '<p>Try to use <code>textbox1.Select()</code> instead of <code>textbox1.Focus()</code>. This helped me few times.</p>\n'}, {'answer_id': 5487429, 'author': 'Reh', 'author_id': 684068, 'author_profile': 'https://Stackoverflow.com/users/684068', 'pm_score': 1, 'selected': False, 'text': '<p>I had a user control within another user control. textbox1.Select() worked for me but textbox1.Focus() did not work.</p>\n\n<p>You can also try setting Tabstop to false, textbox1.Focus(), TabStop true.</p>\n'}, {'answer_id': 23888913, 'author': 'osama9988', 'author_id': 3511799, 'author_profile': 'https://Stackoverflow.com/users/3511799', 'pm_score': 0, 'selected': False, 'text': '<pre><code>  private void ChildForm1_Load(object sender, EventArgs e)\n        {\n            ActiveControl = txt_fname;\n\n        }\n</code></pre>\n\n<p>i use this code it works fine on win tab control or dotnetbar supertab contrl</p>\n'}]
['https://Stackoverflow.com/questions/48680', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1736/']