Common method of using tab control?

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
B

Brett Romero

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top