CheckBox与CheckedBoxList结合应用
代码如下:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
checkedListBox1.Visible = true;
CheckAll(checkedListBox1);
}
else
{
checkedListBox1.Visible = false;
CheckAllEsce(checkedListBox1);
}
// end block
}
//全部选中方法,参数传控件名称name 属性值
public void CheckAll(object chckList)
{
if (chckList.GetType().ToString() == "System.Windows.Forms.CheckedListBox")
{
CheckedListBox ckl = (CheckedListBox)chckList;
for (int i = 0; i < ckl.Items.Count; i++)
{ ckl.SetItemCheckState(i, CheckState.Checked); }
}
// end block if
}
//end mehtod block
//全部取选中方法,参数传控件名称name 属性值
public void CheckAllEsce(object chckList)
{
if (chckList.GetType().ToString() == "System.Windows.Forms.CheckedListBox")
{
CheckedListBox ckl = (CheckedListBox)chckList;
for (int i = 0; i < ckl.Items.Count; i++)
{ ckl.SetItemCheckState(i, CheckState.Unchecked); }
}
// end block if
}
private void button2_Click(object sender, EventArgs e)
{
string strCkl = "选择信息如下:" + "\n";
if (checkBox1.Visible == true)
{
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
strCkl += checkedListBox1.CheckedItems[i].ToString() + "\n";
}
}
// end block if
MessageBox.Show(strCkl,"你选择的项有:");
}
运行结果:

浙公网安备 33010602011771号