【发布时间】:2021-07-16 19:30:05
【问题描述】:
如果 x 我想显示特定数量的单选按钮,我在文本框中有值,如果 y 显示其他特定数量的单选按钮,那么如何添加特定数量的单选按钮并通过代码控制其位置和大小,谢谢
【问题讨论】:
-
WinForms,WPF,还有什么?
-
对于 WinForms C#
-
你有没有做过任何研究或尝试自己写任何东西?
如果 x 我想显示特定数量的单选按钮,我在文本框中有值,如果 y 显示其他特定数量的单选按钮,那么如何添加特定数量的单选按钮并通过代码控制其位置和大小,谢谢
【问题讨论】:
您可以尝试实现旧的 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);
【讨论】: