简体   繁体   English

从选中列表框添加到列表框或从列表框中删除选中的项

[英]Add or remove checked items from checkedlistbox to listbox

我是C#的新手,我的问题是如何将选中的项目从“ checkedlistbox”添加到列表框中,并且当我取消选中该项目时,也要从列表框中删除它。

If you have checkedListBox1 as checkedListBox and your listBox called listBox1 , you should add this ItemCheck Event for your 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]);
}

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);

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM