1、命名空间与继承
命名空间:System.Windows.Forms
继承:Object→MarshalByRefObject→Component→Control→ButtonBase→RadioButton[CheckBox]
2、常用属性
Checked:是否被选中
注意,RadioButton和CheckBox一般和组件搭配使用,一般放置在GroupBox或者Panel之中
3、常用事件
private void radioButton1_CheckedChanged(object sender, EventArgs e)
4、官方示例
无官方例子
//RadioButton事件示例
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if(rb != null)
{
if (rb.Checked)
{
this.label3.Text = "radioButton选择结果:"+ rb.Text;
}
}
}
//CheckBox事件示例
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string s = "cb选择结果:";
foreach (var item in this.groupBox3.Controls)
{
CheckBox cb = item as CheckBox;
if (cb != null)
{
if(cb.Checked)s+= " " +cb.Text;
}
}
this.label4.Text = s;
}
两种状态的CheckBox
private void Form1_Load(object sender, EventArgs e)
{
//两种状态Checkbox作为按钮使用
//设置未选中状态
this.checkBox5.Checked = false;
this.checkBox5.Text = "已经停止";
this.checkBox5.ForeColor = Color.Snow;
this.checkBox5.BackColor = Color.Red;
this.checkBox5.Appearance = Appearance.Button;//Appearance外观选择Button模式
this.checkBox5.FlatStyle=FlatStyle.Flat;//FlatStyle选择Flat
this.checkBox5.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if(cb != null)
{
if(cb.Checked)
{
this.checkBox5.Text = "正在摸鱼";
this.checkBox5.ForeColor = Color.NavajoWhite;
this.checkBox5.BackColor = Color.Green;
}
else
{
this.checkBox5.Text = "已经停止";
this.checkBox5.ForeColor = Color.Snow;
this.checkBox5.BackColor = Color.Red;
}
}
}
二选一的RadioButton
private void Form1_Load(object sender, EventArgs e)
{
//两种状态的RadioButton作为按钮使用
this.label7.Text = "二选一";
//设置未选中状态
this.radioButton3.Checked = false;
this.radioButton3.Text = "未启用:发呆功能";
this.radioButton3.ForeColor = Color.Blue;
this.radioButton3.BackColor = Color.DarkGray;
this.radioButton3.Appearance = Appearance.Button;//Appearance外观选择Button模式
this.radioButton3.FlatStyle = FlatStyle.Flat;//FlatStyle选择Flat
this.radioButton3.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效
this.radioButton4.Checked = true;
this.radioButton4.Text = "已启用:吹牛功能";
this.radioButton4.ForeColor = Color.Black;
this.radioButton4.BackColor = Color.Red;
this.radioButton4.Appearance = Appearance.Button;//Appearance外观选择Button模式
this.radioButton4.FlatStyle = FlatStyle.Flat;//FlatStyle选择Flat
this.radioButton4.FlatAppearance.BorderSize = 0;//必须先设置FlatStyle,再设置FlatAppearence.BorderSize为0才有效
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
RadioButton cb = sender as RadioButton;
if (cb != null)
{
if (cb.Checked)
{
//更改状态
//cb.Checked = false;
this.radioButton3.Text = "已启用:发呆功能";
this.radioButton3.ForeColor = Color.Black;
this.radioButton3.BackColor = Color.Red;
}
else
{
this.radioButton3.Text = "未启用:发呆功能";
this.radioButton3.ForeColor = Color.Blue;
this.radioButton3.BackColor = Color.DarkGray;
}
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
RadioButton cb = sender as RadioButton;
if (cb != null)
{
if (cb.Checked)
{
//更改状态
//cb.Checked = false;
this.radioButton4.Text = "已启用:吹牛功能";
this.radioButton4.ForeColor = Color.Black;
this.radioButton4.BackColor = Color.Red;
}
else
{
this.radioButton4.Text = "未启用:吹牛功能";
this.radioButton4.ForeColor = Color.Blue;
this.radioButton4.BackColor = Color.DarkGray;
}
}
}
5、其他
- CheckBox的ThreeState->戳