Pat Ryb
Pat Ryb

Reputation: 41

How To Set TabPage on start?

I have a TabControl with three TabPages. The initial start of the app opens always the first TabPage on the left side. For me it's necessary to set the starting TabPage (for example the second one).

Of course, I know about possibilities to change the tab on start like these:

tabControl.SelectedTab = tabPage;
tabControl.SelectTab(tabPage);
...

But this code would also activate additional events to fire like TabControl.Selecting, TabControl.Deselecting, TabControl.SelectedIndexChanged etc. — I would really like to prevent this in advance.

What I am looking for is some kind of property in the TabControl like "StartingTabPageIndex" - setting it to 1 would open the second TabPage on start without invoking any unnecessary events.

Upvotes: 2

Views: 6134

Answers (3)

LarsTech
LarsTech

Reputation: 81675

Another option. Go into the Form Designer, change the SelectedIndex property from 0 to 1:

// 
// tabControl1
// 
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Location = new System.Drawing.Point(223, 21);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 1;      //  <--   This Line
this.tabControl1.Size = new System.Drawing.Size(300, 143);
this.tabControl1.TabIndex = 3;

The event handlers aren't connected yet, and making any modifications to the TabControl in the designer doesn't seem to affect that property. It seems safe to change it this way.

Upvotes: 6

Jcl
Jcl

Reputation: 28272

Update

I just made a simple test, and SelectedTab does not work because it expects the handle to be created on set.

However this seems to work:

public class MyTabControl : TabControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Browsable(true)]
    public new int SelectedIndex
    {
        get { return base.SelectedIndex; }
        set { base.SelectedIndex = value; }
    }
}

You'll now be able to see SelectedIndex in the designer and can set it. It won't change the visible tab in the designer, but it will store the "initial tab index" (zero-based).

It does change SelectedIndex, but it does not call the events since events are assigned last in the designer's serialization, so they are never assigned before the change.

Old

One option would be having SelectedTab serialized. You'd only need to derive your own custom TabControl from TabControl and have something like this:

public class MyTabControl : TabControl
{
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  public new TabPage SelectedTab {
    get { return base.SelectedTab;  }
    set { base.SelectedTab = value; }
  }
}

That way you'll get your designer selected SelectedTab as initial.

I haven't tested this, but theory says it should work :-)

Upvotes: 0

Steve
Steve

Reputation: 216363

You should remove the binding with the event handlers from the designer and add them after you have set the initial tabpage

After removing them in the designer (this doesn't delete the event handler code) rebind the event handler in the form load event after setting the required tabpage

 tabControl.SelectedTab = tabPage;
 tabControl.Selected += tabControl_Selected;
 .... and so on for the other events to handle....

Upvotes: 0

Related Questions