單選框(RadioButton
)用于提供一組互斥選項,讓用戶從中選擇一個,一般情況下同一個容器中只能有1個單選框被選中,一般要配合GroupBox容器使用。案例:添加兩個GroupBox容器,每個容器中放入3個單選框,此時每隔容器相互獨立,在各自獨立的容器中只能有1個單選框被選中。
namespace _005_單選框
{
public partial class 單選框 : Form
{
public 單選框()
{
InitializeComponent();
}
private void 單選框_Load(object sender, EventArgs e)
{
rB1.Checked= true;
rB4.Checked= true;
}
private void button1_Click(object sender, EventArgs e)
{
foreach (RadioButton item in groupBox1.Controls)
{
if (item.Checked)
{
MessageBox.Show(item.Text);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (RadioButton item in groupBox1.Controls)
{
if (item.Checked)
{
MessageBox.Show(item.Text);
}
}
}
}
}
該文章在 2025/4/17 9:21:21 編輯過