本帖最后由 Broadm 于 2023-11-30 10:33 编辑
[C#] 纯文本查看 复制代码 using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
private readonly GroupBox groupBox = new GroupBox();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
var rb = new RadioButton()
{
Text = "名称" + i,
Margin = new Padding(8, 30 * i, 0, 0),
Top = 15 + 30 * i,
Left = 15,
};
groupBox.Controls.Add(rb);
}
var button = new Button() { Text = "检测选中项", Left = 300 };
button.Click += Button_Click;
this.Controls.Add(groupBox);
this.Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
var checkedRadioButton = (from RadioButton r in groupBox.Controls where r.Checked select r).FirstOrDefault();
if (checkedRadioButton == null)
{
MessageBox.Show("请选择一个");
return;
}
MessageBox.Show("您选择的是:" + checkedRadioButton.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
|