2013-05-13 72 views
0

如果我不知道運行時用戶選擇多少個複選框,如何將動態創建的複選框的名稱存儲在字符串數組中。 說我有10個動態複選框,10個用戶中的任意一個現在選擇6複選框如何獲取這些選中複選框的名稱並將它們存儲在一個字符串數組中。如何將動態複選框的名稱存儲在字符串數組中

我知道如何在動態複選框上使用事件處理函數,但是我不知道如何聲明Straing數組,當我不知道數組的大小時。

在這裏我做了什麼至今 -

private void CheckBoxCheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox c = (CheckBox)sender; 
     //Label myLabel; 
     String str = null; 
     if (c.Checked == true) 
     { 
      str = c.Text; 
      gpBox[gpcount] = new GroupBox(); 
      gpBox[gpcount].Name = "gpBox" + Convert.ToString(count); 
      gpBox[gpcount].Text = str; 
      gpBox[gpcount].Location = new Point(5, gpposition); 
      gpBox[gpcount].AutoSize = true; 
      this.Controls.Add(gpBox[gpcount]); 

      aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection); 
      aAdapter3 = new OleDbDataAdapter(aCommand3); 
      ds3 = new DataSet(); 
      aAdapter3.Fill(ds3, "app_info"); 
      ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true); 
      int batch_count = ds3.Tables[0].Rows.Count; 
      batchCheckBox = new CheckBox[batch_count]; 
      //filling the groupbox with batch code by generating dynamic checkboxes 
      for (int j=0; j < batch_count; ++j) 
      { 
       batchCheckBox[j] = new CheckBox(); 
       batchCheckBox[j].Name = "batch" + Convert.ToString(k); 
       batchCheckBox[j].Text = ds3.Tables[0].Rows[j][1].ToString(); 
       Console.WriteLine(batchCheckBox[j].Text); 
       batchCheckBox[j].Location = new System.Drawing.Point(104 * position, 30); 
       gpBox[gpcount].Controls.Add(batchCheckBox[j]); 
       batchCheckBox[j].CheckStateChanged += new System.EventHandler(BatchBoxCheckedChanged); 
       position++; 
       count++; 
       Console.WriteLine(batchCheckBox[j].Name); 
       k++; 
      } 
      position = 1; 
      gpposition += 100; 
     } 
     else 
     { 
      count--; 
      this.Controls.RemoveByKey("lbl" + c.Name); 
      this.Update(); 
     } 
    } 
    int total_batch = 1; 
    string[] batchname; 
    private void BatchBoxCheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox batchBox = (CheckBox)sender; 
     //Here I want to store name of checkbox in array 
     if (batchBox.Checked == true) 
     { 
      batchname = new String[total_batch]; 
      total_batch++; 

     } 
     else 
     { 
     } 
    } 
+2

如果你只是想保存在陣列的複選框名稱。然後,而不是數組,使用'列表名稱=新列表()' – Sachin 2013-05-13 06:36:21

+0

@ Sachine謝謝你的指導,請給我一些亮點,所以我可以使用列表,因爲我是新的列表。 Furthur我必須將這些名稱傳遞給另一個表單 – user2241865 2013-05-13 06:47:17

+0

@ user2241865一個列表基本上是一個可以動態調整大小(容量)的數組。在大多數Senario中工作起來更容易:) – 2013-05-13 06:55:56

回答

0

你可以試試這個:

//Gets all checkbox's on the form 
    List<CheckBox> chks = Controls.OfType<CheckBox>().ToList(); 

    //take only those who is checked, and select only their name property 
    List<string> names = chks.Where(c => c.Checked).Select(c => c.Name).ToList(); 

UPDATE

測試你可以打印所選擇的名稱的列表:

string txt = ""; 
foreach(string name in names) 
{ 
    txt += name+" \n\r"; 
} 
MessageBox.Show(txt); 
+0

@ Jens我試過你的代碼片段,但顯示錯誤 - System.Windows.Forms.CheckBox不包含IsChecked – user2241865 2013-05-13 07:19:27

+0

@ user2241865的定義對不起。嘗試使用'c.Checked'而不是'c.IsChecked'。我已經更新了我的回答 – 2013-05-13 07:29:20

+0

@ Jens我已經試過,並且發現這個錯誤 - 無法隱式地將類型'System.Collections.Generic.IEnumerable '轉換爲'System.Collections.Generic.List '。一個明確的轉換存在(你是否缺少演員?) – user2241865 2013-05-13 07:30:37

0

謝謝你感到沉淪

} 
     list = new List<string>(); 
    } 

    private void BatchBoxCheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox batchBox = (CheckBox)sender; 
     //Here I want to store name of checkbox in array 

     if (batchBox.Checked == true) 
     { 
      list.Add(batchBox.Text); 
     } 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     foreach(string prime in list) // Loop through List with foreach 
     { 
      Console.WriteLine(prime); 
     } 
    }  

這樣做是

相關問題