Common method of using tab control?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I have a tab control with seven tabs. I need to do certain things when
a user clicks each tab. I'm doing this:

private void tabControl1_Click(object sender, EventArgs e)
{
if(tabControl1.SelectedIndex == 0)
{...do something...}
else if (tabControl1.SelectedIndex == 6)
{...do something...}
.....
}

and so on for all tabs. That or switches will work but isn't very
intuitive. Is there some other common way of doing this?

Thanks,
Brett
 
Brett,

That's really the only way of doing it, really. You could perform an if
statement on the text of the tab, assuming that it is unique.

Is there some other way you were hoping to be able to do it?
 
I wasn't sure what else may be available for the tabcontrol in this
scenario. I just don't see all of those if statements or their
cousins, switches, as an elegant solution. That's all.

Thanks,
Brett
 
You can use the TabControl's SelectedIndexChanged event, or the TabPage's
Click event.
I supose you can use somthing like:

private void tabControl1_SelectedIndexChanged(
object sender,
System.EventArgs e)
{
if (this.tabControl1.SelectedTab.Equals(this.tabPage1))
{
// do somthing...
}
else if (this.tabControl1.SelectedTab.Equals(this.tabPage2))
{
// do somthing...
}
}

Mihaly
 
Back
Top