tags:

views:

349

answers:

3

Does anyone know a way to open a 2nd form in a .NET application with a certain tab selected in a tab control on that form?

This is what my current code looks like which just opens the form:

SettingsForm TheSettingsForm = new SettingsForm(this);
TheSettingsForm.Show();

SettingsForm.cs is the name of the 2nd form to be opened.

Thanks in advance,

+2  A: 

You just need to expose a property on your form that allows the calling code to set the desired tab. You might do it just by index like this:

var form = new SettingsForm(this);
form.SelectedTab = 2;
form.Show();

The property on the form would just set the appropriate property on the tab control:

public int SelectedTab
{
    get { return _tabControl.SelectedIndex; }
    set { _tabControl.SelectedIndex = value; }
}

HTH, Kent

Kent Boogaart
It works! many thanks
timmyg
+1  A: 

Do something like this:

SettingsForm TheSettingsForm = new SettingsForm(this);
TheSettingsForm.TabPanel.SelectedIndex = SettingsFormTabIndexes.MyDesiredTab;
TheSettingsForm.Show();

where you've made a property in TheSettingsForm that exposes the tab control and SettingsFormTabIndexes is a friendly enum naming all the tabs by index.

plinth
+1  A: 

Add an event handler to the TabControl.SelectedIndexChanged event.

myTabControl.SelectedIndexChanged += myTabControl_SelectedIndexChanged;

private void myTabControl_SelectedIndexChanged(object sender, EventArgs e) {
    TabControl tc = sender as TabControl;
    if (tc.SelectedIndex == indexOfTabToShowFormOn) {
        TheSettingsForm.Show();
    }
}
Jason