2013-05-13 3 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

을 완료한다

} 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); } } 

everbody 주셔서 감사합니다. 배열 대신에'List names = new List ()' – Sachin

+0

@ Sachine gor 안내를 부탁드립니다. 목록을 새로 사용할 수 있도록 목록을 사용할 수 있도록 몇 가지 주요 사항을 알려주십시오. Furthur이 이름을 다른 형식으로 전달해야합니다. – user2241865

+0

@ user2241865 목록은 기본적으로 크기 (용량)가 동적으로 조정될 수있는 배열입니다. 대부분의 시나리오에서 작업하는 것이 더 쉽습니다. –

답변

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

+0

@ user2241865 죄송합니다. 'c.IsChecked' 대신'c.Checked'를 사용해보십시오. 내 대답 –

+0

@ Jens 나는 이미 시도한이 오류를 발견했습니다 - 암시 적으로 형식 'System.Collections.Generic.IEnumerable '을 'System.Collections.Generic.List '으로 변환 할 수 없습니다. 명시 적 변환이 존재합니다 (캐스트가 누락 되었습니까?) – user2241865

0

63,210 방금 ​​배열의 체크 박스의 이름을 저장하려는 경우가

관련 문제