Reputation: 73
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!
Upvotes: 2
Views: 27035
Reputation: 1838
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));
}
}
Wrap it in an Update Panel to use AJAX
Upvotes: 0
Reputation: 56
If you have checkedListBox1
as checkedListBox
and your listBox
called listBox1
, you should add this ItemCheck Event
for your 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]);
}
Upvotes: 3
Reputation: 28970
Add Items :
YourListbox.Items.Add("");
Link : 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
var items = new System.Collections.ArrayList(listboxFiles.SelectedItems);
foreach (var item in items) {
listbox.Items.remove(item);
}
Upvotes: 1