Search code examples
c#user-interfacevisual-studio-2019tabcontrolvb.net-2010

Dynamic Button Click on Tab Control - Tab Pages C#


I have an application that needs to click on button located on tab pages. So for example:

Tabcontrol has Tabpage1, Tabpage2, .... TabpageN

and each tabpage has a buttonX that performs a taskX.

Now supposing, I'm on TabPage1 and I want to Click ButtonX on Tabpage3 without changing tabindex/selectedindex. How can I do that?


Solution

  • Generally one would use PerformClick but I've noticed it failing on TabControl. So if not dependent on EventArgs, create a sub method for the click event e.g.

    private void button1_Click(object sender, EventArgs e)
    {
        Button1PerformClick();
    }
    
    private void Button1PerformClick()
    {
        MessageBox.Show("Button1");
    }
    

    Than calling

    private void button2_Click(object sender, EventArgs e)
    {
        Button1PerformClick();
    }
    

    Doesn't matter in regards to which tab the button is on in this case.