Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何使用For循环创建数百个单选按钮?_.net_Winforms_C++ Cli_Radio Button - Fatal编程技术网

.net 如何使用For循环创建数百个单选按钮?

.net 如何使用For循环创建数百个单选按钮?,.net,winforms,c++-cli,radio-button,.net,Winforms,C++ Cli,Radio Button,我正在尝试在窗口窗体上创建许多使用for循环的单选按钮。我面临的问题是为每个单选按钮生成一个变量名。最初,我计划在每个单选按钮上添加不同的数字,如0001,0002。但是,我不能这样做,因为变量名不是字符串。有什么建议吗?使用数组: RadioButton[] rb = new RadioButton[100]; for (int i = 0; i < 100; i++) { rb[i] = new RadioButton(); rb[i].Location = new P

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

使用数组:

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.
}
RadioButton[]rb=新的RadioButton[100];
对于(int i=0;i<100;i++)
{
rb[i]=新单选按钮();
rb[i]。位置=新点(0,i*20);
rb[i].Text=“此处的文本”;
groupBox1.Controls.Add(rb[i]);
//等等。
}
这是用C#编写的,因为我不知道VC++,但也许它可以帮助您。

使用数组:

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.
}
RadioButton[]rb=新的RadioButton[100];
对于(int i=0;i<100;i++)
{
rb[i]=新单选按钮();
rb[i]。位置=新点(0,i*20);
rb[i].Text=“此处的文本”;
groupBox1.Controls.Add(rb[i]);
//等等。
}
这是用C#编写的,因为我不懂VC++,但也许它可以帮助您。

试试这个:

        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);
        }
var rb=new List();
布尔满足=假;int位置=0;
而(!满意)
{
添加(new RadioButton(){Location=new Point(0,Location*20),Text=Location.ToString()});
位置++;
满足=rb.计数>100?真:假;
}
foreach(rb中的对象r)
{
此.Controls.Add((RadioButton)r);
}
试试这个:

        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);
        }
var rb=new List();
布尔满足=假;int位置=0;
而(!满意)
{
添加(new RadioButton(){Location=new Point(0,Location*20),Text=Location.ToString()});
位置++;
满足=rb.计数>100?真:假;
}
foreach(rb中的对象r)
{
此.Controls.Add((RadioButton)r);
}

如果表单上有“数百”个控件,您将遇到严重的性能问题。你的用户会非常适合你,因为这个界面根本没有“可用性”。但这很容易做到,就像你描述的那样。“变量名不是字符串”是什么意思?是的。查找单选按钮控件的
Name
属性。@CodyGray我计划将这些按钮放入带有滚动条的面板中。@CodyGray我知道有这样的名称字段->radioButton1->Name=L“radioButton1”。L“radioButton1”是字符串,但(this->radioButton1->Name)中的radioButton1不是字符串。我该怎么办?没关系。可以重用相同的变量名来创建循环中的所有对象。如果需要保留通过代码单独访问它们的能力,可以在循环中迭代时将它们放入数组(类似于
列表
)。(我只想写一些示例代码并给出答案,但我告诉自己,在iPhone上写评论更容易……如果我以后记得的话。)如果表单上有“数百”个控件,您将遇到严重的性能问题。你的用户会非常适合你,因为这个界面根本没有“可用性”。但这很容易做到,就像你描述的那样。“变量名不是字符串”是什么意思?是的。查找单选按钮控件的
Name
属性。@CodyGray我计划将这些按钮放入带有滚动条的面板中。@CodyGray我知道有这样的名称字段->radioButton1->Name=L“radioButton1”。L“radioButton1”是字符串,但(this->radioButton1->Name)中的radioButton1不是字符串。我该怎么办?没关系。可以重用相同的变量名来创建循环中的所有对象。如果需要保留通过代码单独访问它们的能力,可以在循环中迭代时将它们放入数组(类似于
列表
)。(我只想写一些示例代码并发布一个答案,但我告诉自己,在iPhone上写评论更容易……如果我以后记得的话。)我不明白的一件事是如何在groupbox上添加每个radiobutton?我很困惑应该有这样的语句:“this->groupBox1->Controls->Add(this->radioButton4);”不是吗?您可以使用for循环来创建,但它们实际上并不在要显示的内容上。我说得对吗?很抱歉,我对VC++非常陌生。@Marco我在回答中补充了这一点。还纠正了我在原始答案中没有自己创建单选按钮的错误。同样,这是在C#中。我不明白的一件事是如何在groupbox上添加每个单选按钮?我很困惑应该有这样的语句:“this->groupBox1->Controls->Add(this->radioButton4);”不是吗?您可以使用for循环来创建,但它们实际上并不在要显示的内容上。我说得对吗?很抱歉,我对VC++非常陌生。@Marco我在回答中补充了这一点。还纠正了我在原始答案中没有自己创建单选按钮的错误。同样,这是C#。