Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# TabControl Selected event seems to not work

Tags:

c#

tabcontrol

I am trying to access the event handler for selecting a tab, basically I have 3 tab pages inside of tabControl1. I want to be able to manipulate what is displaying in a listbox based on what tab is selected at the moment as a new tab is selected. This does not work, anytime a tab page is selected it fails to show the message box, (when that line is uncommented)

private void tabControl1_Selected(Object sender, EventArgs e)
{
    //MessageBox.Show(tabControl1.SelectedIndex.ToString());3

    if (tabControl1.SelectedIndex == 0)
    {
        //do something
    }
}
like image 336
Windex Avatar asked Dec 31 '11 14:12

Windex


1 Answers

That is not the right assignment. Your second parameter is wrong.

Try this:

private void tabControl1_Selected(object sender, TabControlEventArgs e) {
  if (e.TabPage.Name == tabPage1.Name)
    MessageBox.Show("First Tab!");
}

And make sure you have it wired up correctly (it sounds like you don't have the event actually handled):

public Form1() {
  InitializeComponent();

  tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
}
like image 56
LarsTech Avatar answered Oct 21 '22 12:10

LarsTech