Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

I have TabControl with 2 TabItems: TabItem1 and TabItem2
In TabItem1: Radio (TabIndex = 1) and TextBox(TabIndex = 2)
I try to focus textbox but when selecting tabControl1, focus radio. How do i focus into textbox

C#
void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(e.Source is TabItem))
            {
                return;
            }
            if(tabControl1.SelectedItem.Equals(tabItem1))
            {
                this.tabItem1.Focus();
                UpdateLayout(); //tabControl1.UpdateLayout();
                this.textBox1.Focus();
            }
            else
            {
                this.tabItem2.Focus();
                this.tabItem2.UpdateLayout();
                this.txtFocusTab2.Focus();
            }
        }
Posted

1 solution

This is not focusing, this is "selection". Here is how:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selectedindex(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selectedtab(v=vs.110).aspx[^].

Note that both properties are read-write. The setters have the side effect of actually selecting the tab you want.

—SA
 
Share this answer
 
Comments
minhhana 3-Sep-14 12:31pm    
Sr,I'm not good english :), but I want when selecting tab1, focus to second control (textbox) not radio, the code always focus radio
Sergey Alexandrovich Kryukov 3-Sep-14 13:25pm    
Combine SelectedIndex with Control.Focus. But better, leave the focus as is. WPF has logical focus which will put it all right. Anyway, you now know all you need. Any further questions?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900