自定义控件 继承 RadionButton 类,My_RadionButton。
我在这个类中定义个 GroupName 属性,用来给单选框分组,同一组里面只能选一个,
不借助panel等容器。
为什么我下面的代码不行呀/// <summary>
/// 重载值改变
/// </summary>
/// <param name="e"></param>
protected override void OnCheckedChanged(EventArgs e)
{ if (this.Checked)
{
if (this.Parent == null)
{
return;
} foreach (Control control in this.Parent.Controls)
{
if (control is My_RadioButton)
{
My_RadioButton radiobuttonTemp = control as My_RadioButton;
if (radiobuttonTemp.GroupName.Trim() == this.GroupName.Trim() && radiobuttonTemp.Name != this.Name)
{
radiobuttonTemp.Checked = false;
}
}
}
}
}
我在这个类中定义个 GroupName 属性,用来给单选框分组,同一组里面只能选一个,
不借助panel等容器。
为什么我下面的代码不行呀/// <summary>
/// 重载值改变
/// </summary>
/// <param name="e"></param>
protected override void OnCheckedChanged(EventArgs e)
{ if (this.Checked)
{
if (this.Parent == null)
{
return;
} foreach (Control control in this.Parent.Controls)
{
if (control is My_RadioButton)
{
My_RadioButton radiobuttonTemp = control as My_RadioButton;
if (radiobuttonTemp.GroupName.Trim() == this.GroupName.Trim() && radiobuttonTemp.Name != this.Name)
{
radiobuttonTemp.Checked = false;
}
}
}
}
}
if (radiobuttonTemp.GroupName.Trim() == this.GroupName.Trim() && radiobuttonTemp.Name != this.Name)
看看这个条件是否符合。断点跟一下吧。