2017-01-10 88 views
0

我創建動態N個無線按鈕上的形式在這條路上單選按鈕和人氣指數它們:動態創建內部形狀

private void CreateRadioButton() 
    { 
     int rbCount = 40; 

     System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[rbCount]; 

     for (int i = 0; i < rbCount; ++i) 
     { 
      radioButtons[i] = new RadioButton(); 
      radioButtons[i].Text = Convert.ToString(i); 
      int x = 514 + i*37; 
      int y = 20; 
      radioButtons[i].Location = new System.Drawing.Point(x,y); 
      radioButtons[i].Size = new Size(37, 17); 
      this.Controls.Add(radioButtons[i]); 
     } 
    } 

在這種情況下,單選按鈕都在一行中創建,但我需要他們安排特定區域內有多行。可能嗎?用什麼方法來解決這類問題?

+1

TableLayoutPanel中 – Steve

+0

或者FlowLayoutPanel的 –

+0

或[單選按鈕列表(http://stackoverflow.com/a/41355419/3110834)。 –

回答

1

如果你想解決您的代碼,而建議的方式在評論

private void CreateRadioButton() 
{ 
    int rbCount = 40; 
     int numberOfColumns = 8; 
     var radioButtons = new RadioButton[rbCount]; 
     int y = 20; 
     for (int i = 0; i < rbCount; ++i) 
     { 
      radioButtons[i] = new RadioButton(); 
      radioButtons[i].Text = Convert.ToString(i); 
      if (i%numberOfColumns==0) y += 20; 
      var x = 514 + i%numberOfColumns * 37; 
      radioButtons[i].Location = new Point(x, y); 
      radioButtons[i].Size = new Size(37, 17); 
      this.Controls.Add(radioButtons[i]); 
     } 
} 
+0

這只是我需要的。很簡單。 Thnx很多。 – Josef