我正在尝试在处理每个已检查项目后取消选中checkboxlist
项目,但我不确定如何解决这个问题。
代码的基本纲要
string selectedNote="";
foreach (object itemChecked in chkbxSnVisits.CheckedItems)
{
DataRowView drView = itemChecked as DataRowView;
selectedNote = drView["id"].ToString() + " -- " + drView["visit"].ToString();
//do a bunch of stuff
//uncheck itemChecked
}
答案 0 :(得分:4)
有点像,
foreach (int i in chkbxSnVisits.CheckedIndices)
{
chkbxSnVisits.SetItemCheckState(i, CheckState.Unchecked);
}
答案 1 :(得分:1)
我是怎么做的
ChechkBoxList chklist;
var chkListCheck = from ListItem item from chklist.Items where item.selected select item;
foreach(ListItem item in chkListCheck ){
item.selected = false;
}
我不知道CheckBoxList对象的CheckedItem属性,但为了保持这一点你可以做到
foreach(ListItem item in chkListCheck.CheckedItems){
item.selected = false;
}
答案 2 :(得分:0)
您应该使用async/await
让您的程序更易于编写,并且可以轻松更新用户界面而无需所有那些令人讨厌的Invoke
来电:
看看这个例子:
async void runCheckedTasks_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
checkListBox.Enabled = false;
button.Enabled = false;
button.Text = "Running...";
var items = checkListBox.CheckedIndices;
await DoCheckedTasks(items);
checkListBox.Enabled = true;
button.Enabled = true;
button.Text = "Go!";
}
async Task DoCheckedTasks(CheckedListBox.CheckedIndexCollection indicies)
{
foreach (int i in indicies)
{
// Here you cast to whatever type you are storing in the CheckListBox.
// I am only using strings like 'First Task', 'Second Task', ...
var item = checkListBox.Items[i] as string;
checkListBox.Items[i] = string.Format("Processing {0}...", item);
checkListBox.SetItemCheckState(i, CheckState.Indeterminate);
var result = await DoTask(i);
if(!result)
checkListBox.Items[i] = string.Format("{0} Failed!", item);
else
checkListBox.Items[i] = string.Format("{0} Successful!", item);
checkListBox.SetItemCheckState(i, CheckState.Unchecked);
}
}
async Task<bool> DoTask(int index)
{
var rand = new Random();
await Task.Delay(3000); // Fake Delay to simulate processing
var d20 = rand.Next(0, 20) + 1; // Roll a d20, >=10 to pass
return d20 >= 10;
}