我在C#中的新種類,我的問題是如何將checked列表框中的選中項添加到列表框中,並且當我取消選中此項時,也將它從列表框中刪除.. !添加或刪除checkedlistbox列表框中的已檢查的項目
回答
如果你有checkedListBox1
爲checkedListBox
和你listBox
叫listBox1
,你應該增加這個ItemCheck Event
您checkedListBox
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
if (e.NewValue == CheckState.Unchecked)
listBox1.Items.Remove(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
}
檢查checkedListBox的事件選項卡,ItemCheck事件應指向此方法 –
這很好實施。 –
添加項目:
YourListbox.Items.Add("");
鏈接:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection.add.aspx
刪除項目:
YourListbox.Items.Remove("");
鏈接:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection.remove.aspx
var items = new System.Collections.ArrayList(listboxFiles.SelectedItems);
foreach (var item in items) {
listbox.Items.remove(item);
}
如何在這種情況下使用itemcheck事件? –
您使用selectedItems爲了選擇 –
我使用此代碼,但似乎調試器不執行項目檢查事件。 公共無效checkedListBox1_ItemCheck(對象發件人,ItemCheckEventArgs E) { 如果(e.NewValue == CheckState.Checked) { listBox1.Items.Add(checkedListBox1.SelectedItem.ToString()); } } 私人無效checkedListBox1_SelectedIndexChanged(對象發件人,EventArgs的) { listBox1.Text = checkedListBox1.SelectedItem.ToString(); } –
ASPX
<asp:CheckBoxList ID="_CheckBoxList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:CheckBoxList>
<asp:ListBox ID="_ListBox" runat="server"></asp:ListBox>
CS
protected void CheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList cbx = (CheckBoxList)sender;
_ListBox.Items.Clear();
foreach (ListItem item in cbx.Items)
{
if(item.Selected)
_ListBox.Items.Add(new ListItem(item.Text, item.Value));
}
}
包裹它的更新面板中使用AJAX
- 1. checkedlistbox項目檢查到列表框
- 2. 添加/刪除列表中的項目
- 3. Visual Studio 2012 C#:刪除CheckedListBox中的所有*檢查項目
- 4. 列表框添加項目刪除但文本框項目不刪除
- 5. 無法添加選中項目checkedlistbox form1到列表框2
- 6. 檢查列表是否可以添加/刪除項目?
- 7. WP7 - 動畫添加/刪除列表框中的項目
- 8. Matlab指南:添加/刪除列表框中的項目
- 9. jquery添加和刪除列表框中的項目
- 10. 灌裝與檢查的項目列表中CheckedListBox
- 11. 刪除OnDraw中的列表框項目?
- 12. 刪除列表框項目
- 13. 刪除列表框項目
- 14. 需要將項目從checkedlistbox添加到列表並刪除一些
- 15. 添加和刪除列表框之間的項目
- 16. 從數據綁定列表框中添加/刪除項目
- 17. 如何在checkedlistbox中獲取當前已檢查的項目
- 18. 添加/刪除列表<>在列表框中選擇的項目
- 19. 清除或將項目添加到列表框中
- 20. 檢查項目是否在列表中並刪除舊項目
- 21. CheckedListBox並與它們的索引檢查項目列表
- 22. 刪除類似的列表框項目
- 23. 檢查列表框 - 檢查項目
- 24. 列表框添加項目
- 25. 多個線程添加和刪除項目到列表框
- 26. 刪除啓用了複選框的列表框中的項目。通過檢查你想刪除的文件
- 27. 從列表框中刪除項目
- 28. 從列表框中刪除項目?
- 29. WPF從列表框中刪除項目
- 30. 從列表框中刪除項目
什麼是你到目前爲止? –
我正在使用foreach,在添加部分很好,但問題始於取消選中項目並從列表框中刪除。 –