如果我已經在Win形式,我填補這樣在WinForms中通過CheckedListBox迭代?
List<Tasks> tasks = db.GetAllTasks();
foreach (var t in tasks)
tasksCheckedListBox.Items.Add(t.Name);
經過列表框中如何我可以遍歷tasksCheckedListBox.Items並設置一些複選框作爲託運?
謝謝
如果我已經在Win形式,我填補這樣在WinForms中通過CheckedListBox迭代?
List<Tasks> tasks = db.GetAllTasks();
foreach (var t in tasks)
tasksCheckedListBox.Items.Add(t.Name);
經過列表框中如何我可以遍歷tasksCheckedListBox.Items並設置一些複選框作爲託運?
謝謝
如果你想這樣做已添加的項目後,還有一個例子on MSDN
複製在這裏:
private void CheckEveryOther_Click(object sender, System.EventArgs e) {
// Cycle through every item and check every other.
// Set flag to true to know when this code is being executed. Used in the ItemCheck
// event handler.
insideCheckEveryOther = true;
for (int i = 0; i < checkedListBox1.Items.Count; i++) {
// For every other item in the list, set as checked.
if ((i % 2) == 0) {
// But for each other item that is to be checked, set as being in an
// indeterminate checked state.
if ((i % 4) == 0)
checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
else
checkedListBox1.SetItemChecked(i, true);
}
}
insideCheckEveryOther = false;
}
add方法採用可選的IsChecked參數。然後,您可以將對象添加到正確狀態的選中列表框中。
List<Tasks> tasks = db.GetAllTasks();
foreach (var t in tasks)
tasksCheckedListBox.Items.Add(t.Name, isChecked);
或者你有這樣的事情之後加它,你可以改變一個項目的選中狀態:
foreach(var task in tasks)
{
tasksCheckedListBox.SetItemChecked(clb.Items.IndexOf(task), isChecked);
}
謝謝,我剛剛得到它。 – eomeroff 2010-02-02 00:32:46
是否由db.GetAllTasks返回的信息()包括該項目是否被選中或未選中?還是你確定是否應該根據其他數據源或標準創建ListBox後檢查項目? – BillW 2010-02-02 00:12:55
我只是通過它的名字來確定它的checked屬性GetAllTasks()方法不包含任何關於checked的任何信息。 – eomeroff 2010-02-02 00:27:15
因此,每個Task的.Name屬性中的內容會告訴你它是否被檢查:如果是這樣,那麼我認爲你可以在Jake Pearson的答案中適應下面的技巧。然後,當然,您將不得不添加一些代碼來解析字符串(我們必須假定.Name屬性保存)以確定檢查的狀態。在這樣的問題中,儘可能多地提供關於您使用的標準以確定如何評估數據以設置參數的信息總是很好的。祝你好運 ! – BillW 2010-02-02 00:50:21