Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch from one tab to other programatically

Tags:

c#

winforms

In a Windows Form, while using tabControl1 how to switch from one tab to other(i.e tabPage1 to tabPage2) on button click in tabPage1..Tried a lot with

tabPage2.Show(); 
tabControl1.SelectedIndex = tabPage2; 

etc but not giving any o/p...Please help

like image 332
Adi0745 Avatar asked Dec 20 '25 09:12

Adi0745


1 Answers

You should try using the TabControl.SelectedTab Property:

Gets or sets the currently selected tab page.

The topic above also has the following remark:

The tab page must be in the TabPages collection to make it the current tab page.

In your case, this should work:

//somewhere in your code, you have to add first the TabPage to the TabControl
tabControl1.TabPages.Add(tabPage2);

tabControl1.SelectedTab = tabPage2;

[UPDATE]

If that doesn't work either, it's likely you haven't associated the button1_Click method as the button1's Click event handler. And it's even more likely that you just created the button1 control in another control container of your form or the form itself and then cutted and pasted it to the tabPage1 control.

That will remove the event handler.

Just reassign the button1_Click method as button1's Click event handler and make sure you use the SelectedTab property (or SelectedIndex as shown in some other answers and comments of this question).

Here's a reference to a similar problem: cut and paste controls lost event handlers.

like image 59
Alex Filipovici Avatar answered Dec 22 '25 21:12

Alex Filipovici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!