2

在处理每个已检查的项目后,我正在尝试取消选中一个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
}
4

3 回答 3

4

它有点像,

   foreach (int i in chkbxSnVisits.CheckedIndices)
        {
            chkbxSnVisits.SetItemCheckState(i, CheckState.Unchecked);
        }
于 2013-01-18T20:50:50.137 回答
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;
}
于 2013-01-18T21:46:52.380 回答
0

You should use async/await to make your program easier to write and allows easy updating of the UI without all those pesky Invoke calls:

Check out this example:

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;
}
于 2013-01-18T21:07:00.837 回答