记录代码备用,
增加CheckListbox 勾选全选项代码
//------------------获取全选项值-----------------------------
if (checkedListBox1.SelectedIndex==0)
{
for (int i = 1; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, checkedListBox1.GetItemChecked(0));
if (checkedListBox1.GetItemChecked(0))
{
txtSelectConent.Text += checkedListBox1.GetItemText(checkedListBox1.Items[i]) + ",";
DataRowView dv = ((DataRowView)checkedListBox1.CheckedItems[i]);
selectedCode.Append(dv[valueMember] + ",");
checkState = CheckState.Checked;
}
}
if (checkState != CheckState.Checked)
{
checkState = CheckState.Unchecked;
}
txtSelectConent.Text = txtSelectConent.Text.TrimEnd(',');
selectedText = txtSelectConent.Text.Split(',');
if (selectedCode.Length>1)
selectedCode = selectedCode.Remove(selectedCode.Length - 1, 1);
return;
}
操作单选项值,让单选自动完成多选单选,
//----------------------单选项值-------------------------------------
checkState = CheckState.Checked;
for (int i = 1; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
txtSelectConent.Text += checkedListBox1.GetItemText(checkedListBox1.Items[i]) + ",";
checkState1 = CheckState.Checked;
}
else {
// checkedListBox1.SetItemChecked(0, false);
checkState = CheckState.Unchecked;
checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate);
}
}
if (checkState == CheckState.Unchecked && checkState1 == CheckState.Unchecked)
{
checkedListBox1.SetItemCheckState(0, CheckState.Unchecked);
}
else if (checkState == CheckState.Checked && checkState1 == CheckState.Checked)
{
checkedListBox1.SetItemCheckState(0, CheckState.Checked);
}
txtSelectConent.Text= txtSelectConent.Text.TrimEnd(',');
selectedText = txtSelectConent.Text.Split(',');
for (int k = 1; k < checkedListBox1.CheckedItems.Count; k++)
{
DataRowView dv = ((DataRowView)checkedListBox1.CheckedItems[k]);
selectedCode.Append(dv[valueMember] + ",");
}
if (selectedCode.Length > 1)
selectedCode = selectedCode.Remove(selectedCode.Length - 1, 1);
标签:Checked,txtSelectConent,selectedCode,CheckListbox,勾选,CheckState,Text,随笔,checkedL From: https://www.cnblogs.com/xychen/p/18005247