2017-04-05 1 views
0

나는 이런 식으로 윈도우 폼에서 임의의 위치에 대한 몇 가지 버튼을 넣어 시도 :for 루프에 buttonlocation을 추가하는 방법은 무엇입니까?

for (int i = 0; < shuffle.Length; i++) 
{ 
    //This line doesn't work 
    Controls["button" + (i + 1).ToString()].Location = new Point(shuffle[i], 250); 

    //But this line is OK 
    Controls["button" + (i + 1).ToString()].Text = text[i]; 
} 

내가 다음 줄을 쓸 때 잘 작동되지만 루프에 그것을 넣어 1,2를 변경하고 3하는 방법 (i +1)?

button1.Location = new Point(shuffle[0], 250); 
button2.Location = new Point(shuffle[1], 250); 
button3.Location = new Point(shuffle[2], 250); 
+1

어떤 예외가 발생했는지 확인해야합니다. – Thor

답변

1

당신은 같은 대신 숫자의 버튼 루프 수 :

var buttons = new List<Button>(){button1, button2, button3}; 
i = 0; 
foreach (var button in buttons) 
{ 
    button.Location = new Point(shuffle[i], 250); 
    button.Text = text[i]; 
    i++; 
    } 

그런 다음 당신은 그들을 통해 목록 및주기에 모든 버튼을 추가 할 수 있습니다.

관련 문제