• C#中 选项卡(Tabcontrol)动态添加TabPage


    1、动态添加选项卡,并加载DataGridVIew,绑定数据

    private void MainForm_Load(object sender, EventArgs e)
            {
                int newsId = 0;
                dtType = News_Release.News_BLL.BLL.Cmb_Type(newsId);
                for (int i = 0; i < dtType.Rows.Count; i++)
                {
                    TabPage Page = new TabPage();
                    Page.Name = dtType.Rows[i]["Classification_ID"].ToString();
                    Page.Text = dtType.Rows[i]["Name"].ToString();
                    this.tcHomePage.Controls.Add(Page);
                    if(i==0)
                    {
                        Page.Controls.Add(dataGridView1);
                        this.dataGridView1.Dock = DockStyle.Fill;
                    }
                }
                this.dataGridView1.AllowUserToAddRows = false;
                this.dataGridView1.ReadOnly = true;
          this.dataGridView1.DataSource = Login.BLL_Login.MainForm.selectedNews(Convert.ToInt32(dtType.Rows[0]["Classification_ID"].ToString()));
            }
    
            private void tcHomePage_SelectedIndexChanged(object sender, EventArgs e)
            {
                int newsClassification = Convert.ToInt32(tcHomePage.SelectedTab.Name);
                //string typeName = tcHomePage.SelectedTab.Text;
                tcHomePage.SelectedTab.Controls.Add(dataGridView1);
                this.dataGridView1.Dock = DockStyle.Fill;
                this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.dataGridView1.AllowUserToAddRows = false;   
    
                this.dataGridView1.DataSource = Login.BLL_Login.MainForm.selectedNews(newsClassification);
            }

    2、新建一个WinForm程序,拖一个TabControl(在tabPages属性里面将默认的两个TabPage删除)和三个Button(增加、删除、修改) 

    1. public partial class Form1 : Form  
    2.     {  
    3.         private int index = 0;  
    4.         public Form1()  
    5.         {  
    6.             InitializeComponent();  
    7.         }  
    8.   
    9.         private void Form1_Load(object sender, EventArgs e)  
    10.         {  
    11.             //this.tabControl1.SelectedIndex = 1;  
    12.         }  
    13.   
    14.         private void btnAdd_Click(object sender, EventArgs e)  
    15.         {  
    16.             TabPage Page = new TabPage();  
    17.             Page.Name = "Page" + index.ToString();  
    18.             Page.Text = "tabPage" + index.ToString();  
    19.             Page.TabIndex = index;  
    20.             this.tabControl1.Controls.Add(Page);  
    21.  
    22.             #region 三种设置某个选项卡为当前选项卡的方法  
    23.             //this.tabControl1.SelectedIndex = index;  
    24.             this.tabControl1.SelectedTab = Page;  
    25.             //this.tabControl1.SelectTab("Page" + index.ToString());  
    26.             #endregion   
    27.   
    28.             index++;  
    29.         }  
    30.   
    31.         private void btnRemove_Click(object sender, EventArgs e)  
    32.         {  
    33.             bool first = true;  
    34.             if (index > 0)  
    35.             {  
    36.                 #region 两种删除某个选项卡的方法  
    37.                 this.tabControl1.Controls.RemoveAt(this.tabControl1.SelectedIndex);  
    38.                 //this.tabControl1.Controls.Remove(this.tabControl1.TabPages[this.tabControl1.TabPages.Count-1]);  
    39.                 #endregion   
    40.             }  
    41.             else  
    42.             {  
    43.                 return;  
    44.             }  
    45.  
    46.             #region 用于设置删除最后一个TabPage后,将倒数第二个设置为当前选项卡  
    47.             if (first)  
    48.             {  
    49.                 this.tabControl1.SelectedIndex = --index - 1;  
    50.                 first = false;  
    51.             }  
    52.             else  
    53.             {  
    54.                 this.tabControl1.SelectedIndex = index--;  
    55.             }  
    56.             #endregion   
    57.         }  
    58.   
    59.         private void btnUpdate_Click(object sender, EventArgs e)  
    60.         {  
    61.             this.tabControl1.SelectedTab.Text = "xyt";//修改当前选项卡的属性  
    62.             //this.tabControl1.SelectedTab.Name = "";  
    63.             //this.tabControl1.SelectedTab.Tag = "";  
    64.             //this.tabControl1.SelectedTab.Select();  
    65.         }  
    66.     }  
  • 相关阅读:
    [转载]Centos7.x下环境搭建(一)--yum方式安装mysql5.7
    树上分治
    [SPOJ2666]QTREE4
    [SPOJ375]QTREE
    [SPOJ1825]FTOUR2
    [POJ1741]Tree
    [LG-P5350]序列
    [COCI 2014/2015 #3]KAMIONI
    [SHOI2014]神奇化合物
    [GXOI/GZOI2019]旧词
  • 原文地址:https://www.cnblogs.com/zhangyonglvdaomei/p/3837846.html
Copyright © 2020-2023  润新知