动态创建单选按钮并将它们排列在表单内
Dynamic creating radiobuttons and arange them inside form
我正在以这种方式在表单上动态创建 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]);
}
}
在这种情况下,单选按钮全部创建在一行中,但我需要将它们排列在特定区域内的多行中。可能吗?这种问题用什么方法?
如果你想在没有评论中建议的方法的情况下修复你的代码
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]);
}
}
我正在以这种方式在表单上动态创建 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]);
}
}
在这种情况下,单选按钮全部创建在一行中,但我需要将它们排列在特定区域内的多行中。可能吗?这种问题用什么方法?
如果你想在没有评论中建议的方法的情况下修复你的代码
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]);
}
}