2013-11-22 185 views
-1

根據用戶在文本框中輸入的數字,我想創建包含gridview和textboxes的標籤頁。+動態創建標籤頁

現在,當用戶更改要顯示的號碼時,我將刪除並重新添加所有標籤頁。

如何更改代碼以便我只需添加/刪除標籤頁中的區別?

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 
     } 
+0

嗨!你的問題很難理解。我是否正確,當我想你想輸入要顯示在文本框中的標籤數量? – wonko79

+0

是因爲這個輸入在db – user2491383

+0

但他想要3而他把2放到下一次他想要另外1個所以從來沒有刪除以前的數據再加上可是這樣輸入第一次2秒他就這樣放3 – user2491383

回答

1

不要叫:

tabControl2.TabPages.Clear(); 

即去除所有標籤頁。您需要添加多餘的頁面。計算出需要添加的數量,然後通過像這樣添加它們來循環:

var NewPage = new TabPage(); 

// Create your page here ... 

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

我編輯我的問題我也一樣 – user2491383

+0

呃?如果這回答它,那麼不需要編輯你的問題! - 這會讓讀者感到困惑。 – noelicus

+0

我的自定義事件不工作,因爲事件閱讀文本框的值 – user2491383