1

我正在尝试在窗口窗体上使用 for 循环创建许多单选按钮。我面临的问题是为每个单选按钮生成一个变量名。最初,我计划为每个单选按钮添加不同的数字,例如 0001 、 0002 。但是,我不能这样做,因为变量名不是字符串。有什么建议吗?

4

2 回答 2

2

使用数组:

RadioButton[] rb = new RadioButton[100];
for (int i = 0; i < 100; i++)
{
    rb[i] = new RadioButton();
    rb[i].Location = new Point(0, i * 20);
    rb[i].Text = "Your text here";
    groupBox1.Controls.Add(rb[i]);
    //etc.
}

这是在 C# 中,因为我不知道 VC++,但也许它可以帮助你。

于 2012-04-11T20:24:46.597 回答
-1

尝试这个 :

        var rb = new List<RadioButton>();
        bool Satisfied = false; int location =0;

        while (!Satisfied)
        {
            rb.Add(new RadioButton() { Location = new Point(0, location * 20), Text = location.ToString() });
            location++;
            Satisfied = rb.Count > 100 ? true : false;
        }


        foreach ( object r in rb)
        {
            this.Controls.Add((RadioButton)r);
        }
于 2012-04-12T04:42:11.743 回答