【发布时间】:2012-09-26 17:57:29
【问题描述】:
我是 C# 的新手,我的问题是如何将选中的项目从选中列表框中添加到列表框中,当我取消选中此项目时,也将其从列表框中删除.. 谢谢!
【问题讨论】:
-
到目前为止你有什么?
-
我使用的是 foreach,在添加部分效果很好,但问题开始于取消选中该项目并从列表框中删除。
标签: c# checkedlistbox
我是 C# 的新手,我的问题是如何将选中的项目从选中列表框中添加到列表框中,当我取消选中此项目时,也将其从列表框中删除.. 谢谢!
【问题讨论】:
标签: c# checkedlistbox
如果您有 checkedListBox1 作为 checkedListBox 并且您的 listBox 称为 listBox1,您应该为您的 checkedListBox 添加此 ItemCheck Event
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]);
}
【讨论】:
添加项目:
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);
}
【讨论】:
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
【讨论】: