move the selected items from checkedlistbox to listbox C# - c#

I'm trying to move the selected items from checkedlistbox to listbox.But I can't...
for (int x = 0; x<=checkedListBox1.Items.Count;x++ )
{
if (checkedListBox1.GetItemChecked(x))
{
listBox1.Items.Add(checkedListBox1.SelectedItem + "\r\n");
}
}
Please help me

Below code will add selected items to listBox1 and remove it from checkedListBox1, but SelectedItems will be one item each time because Multi-selection is not supported on CheckedListBox you can use CheckedItems instead of SelectedItems to select more than one item.
// Add Selected Items to ListBox
listBox1.Items.AddRange(checkedListBox1.SelectedItems.OfType<object>().ToArray());
// Remove from CheckedListBox
foreach (var item in checkedListBox1.SelectedItems.OfType<object>().ToList())
{
checkedListBox1.Items.Remove(item);
}

Related

Removing items from the listbox and List <T>

How can I remove items from the listbox and the List at the same time? The User selects has the listbox click the button to remove, it removes the listbox and list and then updates the listbox.
I'm not using as datasource, I added items in the listbox with foreach
mylist.ForEach(delegate(list add)
{
listBox1.Items.Add(list.ITEM + list.VALOR [...]);
}
);
Solution:
mylist.RemoveAt(listbox.SelectedIndex);

Removing an item from a list without removing it from its datasource - c#

I'm trying to remove an item from a listBox after i have drag and dropped it, but when i do so i got a message saying i cant modify the listbox since its linked with a datasource.
The thing is that i want to remove the item only from the view of the listBox while keeping it in the datasource.
Here is what i have tried to do:
int indexSelec = listBox3.SelectedIndex;
listBox3.Items.Remove(listBox3.Items[indexSelec]);
Does anybody has a solution?
Thx
You should storage List Box items to List< string > then use that List to add or remove items from List Box. Here is example:
First add a new list: List<string> _items = new List<string>();
Now add items to list and display it on List Box:
_items.Add("One");
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items; //this will display added items to List Box
Adding items to List Box:
_items.Add("New Item " + DateTime.Now.Second);
listBox1.DataSource = null;
listBox1.DataSource = _items;
Removing items from List Box:
int selectedIndex = listBox1.SelectedIndex;
try
{
_items.RemoveAt(selectedIndex);
}
catch
{
}
listBox1.DataSource = null;
listBox1.DataSource = _items;
You cannot remove the ListBoxItem from the ListBox, because it is part of a ReadOnlyCollection.
But you can set its Visibility.
ListBoxItem c = (ListBoxItem) ListBox1.ItemContainerGenerator.ContainerFromItem(ListBox1.Items[0]);
c.Visibility = System.Windows.Visibility.Collapsed;

Delete selected items in a GridView

IList<object> itemsSelected = MyGrid.SelectedItems;
foreach (object itemSelected in itemsSelected)
{
MyGrid.SelectedItems.Remove(itemSelected);
}
I try remove selected items from a GridView but not all selected items are removed.
Could someone help me?
object[] itemsSelected = MyGrid.SelectedItems.ToArray<object>();
foreach (object item in itemsSelected)
{
MyGrid.Items.Remove(item);
}
I had the exact problem as well. I wrote something like this but it didn't delete all items too, just some of them :
foreach(var item in MyGridView.SelectedItems)
{
MyGridView.Items.Remove(item);
}
But write this which will delete all your selected items for sure :
while (YourGridView.SelectedItems.Count != 0)
{
YourGridView.Items.Remove(YouGridView.SelectedItem);
}
there isn't exception, when you use foreach with SelectedItems? When you remove item, SelectedItems array is modified and foreach throws an exception. (Though I've tried on ListBox control). Try to use for-operator and remove items from last to first by index.
Based on your answers in the comments, I assume you are working on a C# winforms application.
Is it possible that what you are actualy using is a ListBox and not a GridView?
If that is so, you should use the ClearSelected() method which unselects all items in the ListBox.

List Box control in asp.net

I have two listbox controls Listbox1 and Listbox2. I want to get the count of items of Listbox2 which are selected from Listbox1 in c#? Suppose i have total 7 items in Listbox1 and from those i have selected only 3 items in Listbox2 control. I want to get the count of items of Listbox2 in C#?
Wonder why nobody used Linq.
#Riya: I understand your requirement as, you want the Count of SelectedItems in ListBox1 that are present in ListBox2 Items. If so do this.
var filteredListCount = ListBox2.Items
.Cast<ListItem>()
.Where(li =>
ListBox1.Items
.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Text).Contains(li.Text))
.Count();
Loop thru the selected items when the selection is changed
Something like this:
int count = 0;
foreach(string itemListbox2 in listBox2.Items)
{
if (itemListbox2.Selected)
{
foreach(string itemListbox1 in listbox1.Items)
{
if (itemListbox1.Selected)
{
if(itemListbox1.Equals(itemListbox2))
{
count++;
break;
}
}
}
}
}
You can loop on all selected items in ListBox1 and inside the loop you search for an item with the same value in ListBox2 and if it's selected you increment a counter.
A Listbox in asp.net doesn't have SelectedItems. Therefor loop through the Items and check if they are selected. If so, find an item in the other list with the same value. If you find a corresponding item, count it. Like this:
int count = 0;
foreach (ListItem item in secondListBox.Items)
{
if (item.Selected)
{
ListItem itemWithSameValue = firstListBox.Items.FindByValue(item.Value);
if (itemWithSameValue != null)
{
count++;
}
}
}

Selecting ListView in C#

i have ListView with items
one ListView with items another one is empty
i need to copy from first ListView selected item to another ListView at the same time
i have to remove the selected item in first ListView in C#
Rather than removing items from the collection that you're enumerating (as per Wael's answer), which is a "bad idea", use a temporary collection, in this instance a List to store them in before removing them:
List<ListViewItem> itemsToMove = new List<ListViewItem>();
foreach (ListViewItem item in listView1.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
listView1.Items.Remove(item);
listView2.Items.Add(item);
}
Where listView1 is the list with the selected items and listView2 is the list to move them to.
this code will copy from the first listview1 to listview2 all the selected items and delete it from listview1
foreach (ListViewItem itm in ListView1.SelectedItems)
{
ListView1.Items.Remove(itm);
listView2.Items.Add(itm);
}

Categories

Resources