Search code examples
c#winformstabcontroltabpage

How to access tab control pages outside the tab control


i have developed a windows form application using c#.

it has a tab control and a menu bar. i want to access a control in tab page when a menu item is clicked.

for example my tab control has 5 tab pages and menu bar has 5 menu items. when menu item 1 is clicked i want to access the text box within tab page 1.

how to do that?


Solution

  • Code:

    tabControlName.SelectedIndex = theIndexOfTheTabPage; //switch to the tab page
    tabControl1.TabPages[theIndexOfTheTabPage].Controls.Find("textBoxName", true)[0].Select(); //find the TextBox and select it
    

    The first line changes to the desired tab page by changing the SelectedIndex property of the tabControl. The second Line searches for the TextBox using the Find(string name, bool searchAllChildren) method. Then the TextBox is focused by using the Select() method.

    To click a Button inside a tab use this code:

    tabControlName.SelectedIndex = theIndexOfTheTabPage; //switch to the tab page
    Button b = tabControlName.TabPages[theIndexOfTheTabPage].Controls.Find("buttonName", true)[0] as Button;
    b.PerformClick();
    

    First get the Button the same way as the TextBox. Then use PerformClick() to click the Button