+动态创建标签页

时间:2013-11-22 09:52:46

标签: c# winforms datagridview tabcontrol tabpage

根据用户在文本框中输入的数字,我想创建包含gridview和文本框的tabpages。

现在,当用户更改要显示的数字时,我会删除并重新添加所有标签页。

如何更改代码以便我只需添加/删除tabpages中的差异?

private DataGridView[] rtb = new DataGridView[100];
        private TabPage[] tab = new TabPage[100];
        DataGridViewComboBoxColumn comp;


        public void ctp(Int32 textbox)
        {
            try
            {
                if (textbox > 10)
                {
                    MessageBox.Show("You Exceed Limit");
                }
                else
                {
                    int k = 0;
                    int s = 0;
                    k = Convert.ToInt32(textBox1.Text); 
                    for (int i = 0; i < k; i++)
                    {
                        tab[i] = new TabPage();
                        //Start Gridview
                        rtb[i] = new System.Windows.Forms.DataGridView();
                        rtb[i].Location = new System.Drawing.Point(0, 50);
                        rtb[i].Size = new System.Drawing.Size(1020, 150);
                        //ID Column
                        rtb[i].Columns.Add("tr_id", "ID");
                        rtb[i].Columns["tr_id"].ReadOnly = true;
                        rtb[i].Columns["tr_id"].Width = 1;
                        // Color Column
                         comp = new DataGridViewComboBoxColumn();
                            comp.HeaderText = "Color No";
                            //comp[i].DataPropertyName = "Color No";
                            comp.Width = 200;
                            comp.Name = "color_no";
                            comp.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                            //comp[i].ReadOnly = false;
                            rtb[i].Columns.Add(comp);
                        //Quantity Column
                        rtb[i].Columns.Add("quantity", "Quantity");
                        //Piece Weight Column
                        rtb[i].Columns.Add("piece_weight", "Piece Weight");
                        //Total Weight Carton
                        rtb[i].Columns.Add("total_weight", "Total Weight");
                        //Piece in Carton Column
                        rtb[i].Columns.Add("pcs_carton", "Pcs/Carton");
                        //No Of Carton Column
                        rtb[i].Columns.Add("no_of_carton", "Total Cartons");
                        //Unit Name Column
                        rtb[i].Columns.Add("unit_name", "Unit Name");
                        //Rate Column
                        rtb[i].Columns.Add("rate", "Rate");
                        //Amount Column
                        rtb[i].Columns.Add("amount", "Amount");
                        //Ship Qty Column
                        rtb[i].Columns.Add("ship_qty", "Ship Qty.");
                        //kdnr column
                        rtb[i].Columns.Add("kdnr", "KDNR");
                        //reference No Column
                        rtb[i].Columns.Add("ref_no", "Reference No.");

                        DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
                        checkColumn.Name = "deleterow";
                        checkColumn.HeaderText = "Delete Row";
                        checkColumn.Width = 50;
                        checkColumn.ReadOnly = false;
                        checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
                        rtb[i].Columns.Add(checkColumn);

                        tab[i].Controls.Add(rtb[i]);
                        tab[i].Location = new System.Drawing.Point(4, 22);
                        tab[i].Name = "tab" + i.ToString();
                        tab[i].Padding = new System.Windows.Forms.Padding(3);
                        tab[i].Size = new System.Drawing.Size(400, 242);
                        tab[i].Text = "Article" + i.ToString();
                        tab[i].UseVisualStyleBackColor = true;
                        tabControl2.TabPages.Add(tab[i]);

                        //MessageBox.Show(""+tab[i].Text);
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
        #region Calling Create TabPage Function
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    Int32 textbox = Convert.ToInt32(textBox1.Text);
                    ctp(textbox);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        #endregion
        }

1 个答案:

答案 0 :(得分:1)

请勿致电:

tabControl2.TabPages.Clear();

即删除所有标签页。您需要添加额外页面。计算你需要添加的数量,然后循环添加它们,如下所示:

var NewPage = new TabPage();

// Create your page here ...

// Add the page to the tabs
tabControl2.TapPages.Add(NewPage);