Thom Ash
Thom Ash

Reputation: 221

Can't get desired tabPage & form to display on startup

Revising question to ask why after finding the offending line by trial and error. Hints from answers suggested something was occurring after form load. I found a line setting tabControl1.SelectedIndex = 1; tabPage2 index is also 1 so why wasn't it selecting tabPage2? After changing the 1 to 0 everything displays correctly.

I can't find a white paper or tutorial on tabControl and use of SelectedIndex so can someone tell me how this works?

 private void Form1_Load(object sender, EventArgs e)
        {
            getRegions();
            tabControl1.SelectedIndex = 1;

I have a Windows form that had 10 tabPages on tabControl1. A change in requirement necessitated all except 2 & 8. Prior to removing any tabPages, tabPage2 and form displayed at startup. I can remove 3,4,5,6,7,9,10 without any problem (1,2,8 are left). If I remove tabPage1 the tabPage and form that displays at startup is tabPage8. It was tabPage2 and I would like it to stay that way.

I've played with this on and off for a couple of weeks and am stumped. I can't seem to find any article or question describing this. What I found were multiple suggestions to add this.tabControl1.SelectedTab = tabPage2; which doesn't seem to do anything.

What might be causing this and what do I need to do? (don't really know what code I need to paste, let me know if more is needed)

// tabControl1
            // 
            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage8);
            this.tabControl1.Location = new System.Drawing.Point(13, 27);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 1;
            this.tabControl1.Size = new System.Drawing.Size(963, 483);
            this.tabControl1.TabIndex = 1;
            this.tabControl1.SelectedTab = tabPage2;

        // tabPage1
        // 
        this.tabPage1.Location = new System.Drawing.Point(4, 22);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(955, 457);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "Region Overall Status";
        this.tabPage1.UseVisualStyleBackColor = true;
        // 


        // tabPage2
        // 
        this.tabPage2.Controls.Add(this.lstBuildList);
        this.tabPage2.Controls.Add(this.label9);
        this.tabPage2.Controls.Add(this.label8);
        this.tabPage2.Controls.Add(this.lblFileLookUp);
        this.tabPage2.Controls.Add(this.btnFileLookUp);
        this.tabPage2.Controls.Add(this.cbRegion);
        this.tabPage2.Controls.Add(this.tabControl2);
        this.tabPage2.Controls.Add(this.tvFileMan);
        this.tabPage2.Controls.Add(this.txtFileLookUp);
        this.tabPage2.Controls.Add(this.cbRegionSites);
        this.tabPage2.Location = new System.Drawing.Point(4, 22);
        this.tabPage2.Name = "tabPage2";
        this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage2.Size = new System.Drawing.Size(955, 457);
        this.tabPage2.TabIndex = 1;
        this.tabPage2.Text = "Build Attribute Maps from Vista Files";
        this.tabPage2.UseVisualStyleBackColor = true;
        this.tabPage2.Click += new System.EventHandler(this.tabPage2_Click);



        // tabPage8
        // 
        this.tabPage8.Location = new System.Drawing.Point(4, 22);
        this.tabPage8.Name = "tabPage8";
        this.tabPage8.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage8.Size = new System.Drawing.Size(955, 457);
        this.tabPage8.TabIndex = 0;
        this.tabPage8.Text = "Edit Attribute Map/ Create SQL Tables from Attribute Map";
        this.tabPage8.UseVisualStyleBackColor = true;
        this.tabPage8.Click += new System.EventHandler(this.tabPage8_Click);
        this.tabControl1.SelectedTab = tabPage2;
        // 

Upvotes: 0

Views: 697

Answers (2)

prem
prem

Reputation: 3538

You can select the tabpage after InitializeComponent or at Form_Load to display at startup.

Here's a sample code which I have tested

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Select tabpage after initialize
            tabControl1.SelectedTab = tabPage3;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //You can select it at form load or at some other action also like a button click
            //tabControl1.SelectedTab = tabPage3;
        }
    }
}

Don't write any code at your designer. Always write after the InitializeComponent call.

Upvotes: 2

HenrikJohnson
HenrikJohnson

Reputation: 159

First of all be ware of the Windows Forms Designer because it very often sneaks in changes you didn't expect when making changes. :)

One thing you could try is to move the this.tabControl1.SelectedTab outside of your InitializeComponent method (I assume you have one). Sometimes the BeginInit/EndInit calls have strange side effects.

Upvotes: 0

Related Questions