2012-05-27 4 views
1

안녕하세요, 루프에서이 작업을 수행해 드리겠습니다.하지만 어떻게 할 생각이 없습니다. 나는 단순히 그것을 증가시킴으로써 이것을 할 수 없다.for 루프에 배열 목록을 쓰는 방법은 무엇입니까?

CheckBox[] checkboxarray; 

checkboxarray = new CheckBox[] { 
    txtChckBx0, txtChckBx1, txtChckBx2, txtChckBx3, txtChckBx4, txtChckBx5, 
    txtChckBx6, txtChckBx7, txtChckBx8, txtChckBx9, txtChckBx10, txtChckBx11, 
    txtChckBx12, txtChckBx13, txtChckBx14, txtChckBx15, txtChckBx16, txtChckBx17, 
    txtChckBx18, txtChckBx19, txtChckBx20, txtChckBx21, txtChckBx22, txtChckBx23, 
    txtChckBx24, txtChckBx25, txtChckBx26, txtChckBx27, txtChckBx28, txtChckBx29, 
    txtChckBx30, txtChckBx31, txtChckBx32, txtChckBx33, txtChckBx34, txtChckBx35, 
    txtChckBx36, txtChckBx37, txtChckBx38, txtChckBx39, txtChckBx40, txtChckBx41, 
    txtChckBx42, txtChckBx43, txtChckBx44, txtChckBx45, txtChckBx46, txtChckBx47, 
    txtChckBx48, txtChckBx49, txtChckBx50, txtChckBx51, txtChckBx52, txtChckBx53, 
    txtChckBx54, txtChckBx55, txtChckBx56, txtChckBx57, txtChckBx58, txtChckBx59, 
    txtChckBx60, txtChckBx61, txtChckBx62, txtChckBx63, txtChckBx64, txtChckBx65, 
    txtChckBx66, txtChckBx67, txtChckBx68, txtChckBx69, txtChckBx70, txtChckBx71, 
    txtChckBx72, txtChckBx73, txtChckBx74, txtChckBx75, txtChckBx76, txtChckBx77, 
    txtChckBx78, txtChckBx79, txtChckBx80 
}; 

답변

0

당신은 새를 할 수 없어 다음

checkboxarray = new CheckBox[] { txtChckBx0, ....} 

는 배열을 정의하는 두 가지 방법을합니다. 다음을 수행해야합니다.

CheckBox[] checkboxarray = { txtChckBx0, ....}; 

원하는 경우 작동하도록 설정하십시오.

행운을 빈다.

4

당신은 체크 박스가 모든 폼에 있다는 것을 알고있는 경우 :

var list = new List<CheckBox>(); 
foreach(var control in this.Controls) 
{ 
    var checkBox = control as CheckBox; 
    if(checkBox != null) 
    { 
     list.Add(checkBox); 
    } 
} 

var checkBoxArray = list.ToArray(); 

컨트롤이 당신이 그들을 위해 검색해야합니다 어디에 당신이 모르는 경우

.

BTW : 위의 코드는 WinForms를 사용합니다. WPF, Silverlight, Metro, ... 컨테이너의 이름이 다르게 지정됩니다. 의 WinForm에서

0

List<CheckBox> checkBox = new List<CheckBox>(); 
// Adding checkboxes for testing... 
for (int i = 0; i <= 80; i++) 
{ 
    var cbox = new CheckBox(); 
    cbox.Name = "txtChckBx"+ i.ToString(); 
    checkBox.Add(cbox); 
    Controls.Add(cbox); 

} 

List<CheckBox> checkBoxfound = new List<CheckBox>(); 
// loop though all the controls 
foreach (var item in Controls) 
{ 
    // filter for checkboxes and name should start with "txtChckBx" 
    if (item is CheckBox && ((CheckBox)item).Name.StartsWith("txtChckBx", StringComparison.OrdinalIgnoreCase)) 
    { 
     checkBoxfound.Add((CheckBox)item); 
    } 
} 
관련 문제