Search code examples
c#focustabcontrolentertabpage

Changing focus from on textbox to another, when tabpages is changed in C#?


I have several tabs in a form. Each tab has one textbox. When I enter tabpage1 I have managed to set the focus on the textBox1. When I press a button in tabpage1 I jump to a random tab in the controller. What I want now is to have the focus set on textBox in the active tabpage. I have tried using tabpage_Enter event, but it does not seem to work. My code look like this :

    private void tabPage2_Enter(object sender, EventArgs e)
    {
        textBox2.Select();

    }

Any suggestions?


Solution

  • I think you need to use SelectedIndexChanged event of TabControl instead of _Enter, using Enter event, focus will change to textBox2 every time the cursor enter the tabPage control.