C#TabControl选项卡根据需要替换的方法

1、项目需求:
TabControl有2个固定选项卡,1个选项卡根据用户选择加载不同的选项
2、设计思路
根据需要设计各个选项卡,将动态选项卡的内容暂存静态TabControl控件中,然后根据需要加载选项卡内容。
3、程序实现
(1)、定义选项卡

   //选项卡页面1( tabPage1) 
        this.tabPage1.Controls.Add(this.tabControl2);
        this.tabPage1.Location = new System.Drawing.Point(22, 4);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(1709, 792);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "固定选项卡一";
        this.tabPage1.UseVisualStyleBackColor = true;

        // 选项卡页面2( tabPage2)
        this.tabPage2.Controls.Add(this.groupBox41);
        this.tabPage2.Controls.Add(this.groupBox38);
        this.tabPage2.Location = new System.Drawing.Point(22, 4);
        this.tabPage2.Name = "tabPage2";
        this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage2.Size = new System.Drawing.Size(1709, 792);
        this.tabPage2.TabIndex = 1;
        this.tabPage2.Text = "固定选项卡二";
        this.tabPage2.UseVisualStyleBackColor = true;

        // 选项卡页面3( tabPage3) 
        this.tabPage3.Controls.Add(this.tabControl3);
        this.tabPage3.Location = new System.Drawing.Point(22, 4);
        this.tabPage3.Name = "tabPage3";
        this.tabPage3.Size = new System.Drawing.Size(1709, 792);
        this.tabPage3.TabIndex = 2;
        this.tabPage3.Text = "动态选项卡一";
        this.tabPage3.UseVisualStyleBackColor = true;

         // 选项卡页面4( tabPage4) 
        this.tabPage4.Controls.Add(this.groupBox43);
        this.tabPage4.Controls.Add(this.groupBox42);
        this.tabPage4.Location = new System.Drawing.Point(22, 4);
        this.tabPage4.Name = "tabPage4";
        this.tabPage4.Size = new System.Drawing.Size(1709, 792);
        this.tabPage4.TabIndex = 3;
        this.tabPage4.Text = "动态选项卡二";
        this.tabPage4.UseVisualStyleBackColor = true;

        //  选项卡控件
        this.tabControl1.Alignment = System.Windows.Forms.TabAlignment.Left;
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Controls.Add(this.tabPage2);
        this.tabControl1.Controls.Add(this.tabPage3);
        this.tabControl1.Controls.Add(this.tabPage4);
        this.tabControl1.Location = new System.Drawing.Point(0, 28);
        this.tabControl1.Multiline = true;
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(1735, 800);
        this.tabControl1.TabIndex = 1;

(2)、将动态选项卡存入控件

static TabControl tb = new TabControl();
        	TabControl.TabPageCollection ad10200 = new TabControl.TabPageCollection(tb);
        	private void TabCtrlUpdate()
        	{
            if (tabControl1.TabPages.Count > 2)
            {
                int len = tabControl1.TabPages.Count;
                for (int i=2;i< len; i++)
                {
                    ad10200.Add(tabControl1.TabPages[2]);   
                }
            }
            tabControl1.TabPages.Add(ad10200[0]);
        }

(3)、动态加载

private void SelectOne_Click(object sender, EventArgs e)
        	{
            if (tabControl1.TabPages[2].Text != "SelectOne")
            {
                ad10200.Add(tabControl1.TabPages[2]);
                tabControl1.TabPages.Add(ad10200[0]);
            }
        	}

        private void SelectTwo_Click(object sender, EventArgs e)
        {
            if(tabControl1.TabPages[2].Text!= "SelectTwo")
            {
                ad10200.Add(tabControl1.TabPages[2]);
                tabControl1.TabPages.Add(ad10200[0]);
            }
        }

4、调试发现
将一个选项卡控件内容加入另一个选项卡控件,原选项卡内容自动消失。

你可能感兴趣的:(C#,c#,开发语言)