2

我是 C# 的新手,我的问题是如何将选中的项目从选中的列表框中添加到列表框中,当我取消选中此项目时,也将其从列表框中删除.. 谢谢!

4

3 回答 3

3

如果你有checkedListBox1ascheckedListBox和你的listBoxcall 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]);
}
于 2012-09-26T18:38:48.453 回答
1

添加项目:

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

}
于 2012-09-26T17:59:23.300 回答
0

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

于 2012-09-26T18:19:01.133 回答