[英]Add many radio buttons to groupbox C#
如果 x 我想显示特定数量的单选按钮,我在文本框中有值,如果 y 显示其他特定数量的单选按钮,那么如何添加特定数量的单选按钮并通过代码控制其 position 和大小,谢谢
您可以尝试实现良好的旧for
循环。 如果要创建垂直堆叠的单选按钮:
//TODO: provide the desired value here
int numberOfRadioButtons = 7;
//TODO: Put desired values here
int left = 15;
int top = 25;
int height = 50;
for (int i = 0; i < numberOfRadioButtons; ++i) {
RadioButton button = new RadioButton() {
Parent = myGroupBox,
Location = new Point(left, top + height * i),
Text = $"RadioButton # {i}",
// If you insist on setting size manually, uncomment the lines below
//AutoSize = false,
//Size = new Size(200, height),
};
// button is created; you can use it, say, to assign event handler:
//button.Click += MyRadioButtonClick;
}
编辑:您可以使用Linq来检查RadioButton
:
using System.Linq;
...
// Either checked rudio button or null if none of radio button is checked
RadioButton chosen = myGroupBox
.Controls
.OfType<RadioButton>()
.FirstOrDefault(button => button.Checked);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.