2017-01-10 60 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