日期:2014-05-20  浏览次数:21132 次

怎样选中CheckBox同时全部选中CheckedListBox?
我是用一个CheckBox和一个CheckedListBox,想选中CheckBox同时全部选中CheckedListBox

并且我的CheckBox与CheckedListBox都是代码生成的!
求助各位

------解决方案--------------------
生成checkbox時,在其下面加入
this.checkbox.CheckedChanged += new System.EventHandler(this.All_CheckedChanged);

private void All_CheckedChanged(object sender, System.EventArgs e)
{
try
{
int count;
for( count = 0 ; count < CheckedListbox.Items.Count ; count++ )
{
CheckedListbox.SetItemChecked( count , checkbox.Checked);
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
即可
------解决方案--------------------
生成checkbox時,在其下面加入
CheckBox checkbox = new CheckBox();
...
CheckBoxList checkboxList = new CheckBoxList();
...
checkbox.CheckedChanged += new System.EventHandler(this.All_CheckedChanged);
checkbox.Tag = checkboxList;

private void All_CheckedChanged(object sender, System.EventArgs e)
{
try
{
CheckBoxList CheckedListbox = (sender as CheckBox).Tag as CheckBoxList;
if (CheckedListbox == null)
return;
int count;
for( count = 0 ; count < CheckedListbox.Items.Count ; count++ )
{
CheckedListbox.SetItemChecked( count , checkbox.Checked);
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}