Search code examples
c#winformsbuttontabcontrol

How to switch tabs with button in a TabControl?


I've watched a few tutorials online on how to change tabs using buttons but for some reason all the code I've tried doesn't work. I am currently running Microsoft Visual Studio 2017 and am trying to write some code for a button to change tabs. I couldn't find any differences between my code and code shown in tutorials, so it may just be a Visual Studio setting that I haven't set up correctly to allow the button correctly, but I couldn't figure out if or where it may be.

Here's my current code:

//Element event handlers
public Form1()
{
  InitializeComponent();
}

private void buttonStart_Click(object sender, EventArgs e)
{
  tabControl.SelectedTab = DestSelect;
}

private void buttonGotoIntro_Click(object sender, EventArgs e)
{
  tabControl.SelectedTab = Intro;
}

//An old computer-generated segment code for the previous button.
//When I try to remove it the computer gets mad at me.
private void GotoIntro_Click(object sender, EventArgs e)
{

}

Solution

  • Please confirm you have subscribed to the Click event for the buttons.

    public Form1()
    {
        InitializeComponent();
        buttonStart.Click += buttonStart_Click;
        buttonGotoIntro.Click += buttonGotoIntro_Click;
    }