110,825
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
就是我编写了一段代码可以实现动态的生成groupBox(一个文本框里输入一个数字,然后点button就可以在界面中生成对应数字个数的groupBox),并且每个groupBox里面含有7个radiobutton,radioButton.Text分别是低、较低、很低、中等、高、较高、很高,我自动生成之后,我在每个groupBox中需要选择radioButton,比如说生成了5个groupBox,我分别选中了“低”“低”“较低”“高”“中等”五种选项,但是我不知道如何编写代码获取到这五个选项,因为每个radioButton背后代表着一个值,我想统计选中选项后的累计值,并对其进行计算
int o = Convert.ToInt32(textBox1.Text);
for (int u = 0; u < o; u++)
{
GroupBox groupBox = new GroupBox();
groupBox.Location = new Point(53, 12 + 60 * u);
groupBox.Size = new Size(734, 52);
groupBox.Text = "专家"+(u+1)+"意见";
string[] radioButtonLabels = { "很低", "低", "较低", "中等", "较高", "高", "很高" };
for (int i = 0; i < 7; i++)
{
RadioButton radioButton = new RadioButton();
radioButton.Text = radioButtonLabels[i];
radioButton.Location = new Point(19 + 100 * i, 26);
radioButton.Size = new Size(95, 16);
groupBox.Controls.Add(radioButton);
}
this.Controls.Add(groupBox);
}
以上是我生成任意groupBox包含7类radioButton的代码,请问后续该如何实现
通过遍历控件的子控件可以实现,但更好的开发方式应该是自定义一个groupbox控件,并在rbut变为选中状态时,更新自定义属性;如此在你需要的时候可以直接从自定义属性读取到是哪个rbut处于选中状态或者自定义属性直接记录rbut.text。
另外提个小建议,写一个Helper类,把你写死在代码里的各种常量用枚举表示,而界面需要显示枚举值的名称时只需要enum.low.tostring()即可。哪怕只是把常量都归拢到一起放一个文件呢,以后不也好改么。
用个Conrrols装起来,遍历即可