C# 从checkedlistbox 到listbox 添加或删除选中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12607639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add or remove checked items from checkedlistbox to listbox
提问by Carl_Honcho
I kind of new in C#, my problem is how to add checked items from a checkedlistbox to a listbox, and when I uncheck this item remove it from the listbox also.. Thanks!
我是 C# 的新手,我的问题是如何将选中的项目从选中的列表框中添加到列表框中,当我取消选中此项时,也将其从列表框中删除..谢谢!
采纳答案by Pavel Shageev
If you have checkedListBox1as checkedListBoxand your listBoxcalled listBox1, you should add this ItemCheck Eventfor your checkedListBox
如果你有checkedListBox1ascheckedListBox和你的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]);
}
回答by Aghilas Yakoub
Add Items :
添加项目:
YourListbox.Items.Add("");
Link : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection.add.aspx
链接:http: //msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection.add.aspx
Delete Items :
删除项目:
YourListbox.Items.Remove("");
Link : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection.remove.aspx
链接: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);
}
回答by arunlalam
ASPX
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
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));
}
}
Wrap it in an Update Panel to use AJAX
将其包装在更新面板中以使用 AJAX

