Isaac
Isaac

Reputation: 51

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)
{

}

Upvotes: 1

Views: 5442

Answers (3)

Nellymandela
Nellymandela

Reputation: 119

I assume u want to select a tab when a different Button is clicked. tabControl.SelectedIndex = [Index of tab to switch to];

Code should look like; tabControl.SelectedIndex = feeTabIndex;

If this is not clear enough, tell me exactly what you want to do.

Upvotes: 0

大陸北方網友
大陸北方網友

Reputation: 3767

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

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

Upvotes: 1

matt.dixon
matt.dixon

Reputation: 76

Instead of 'tabControl.SelectedTab = DestSelect;" try instead the method 'tabControl.SelectTab(DestSelect);'

I read through this article to find your (hopefully) answer: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2cf22896-c5bd-4a9b-ab61-34404b55ef01/how-to-jump-to-a-specific-tab-in-the-tabcontrol?forum=vbgeneral

Upvotes: 1

Related Questions