List Box control in asp.net - c#

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++;
}
}
}

Related

move the selected items from checkedlistbox to listbox 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);
}

How to check if any items in listbox match another listbox

So, I need to check if any names listed in listbox1 are listed in listbox2 or not.
If there is a name in listbox1 that is NOT in listbox2, then perform an action. How to do this?
One solution would be to select all the items in both the List boxes and insert them into their own lists respectively, then you can use the Except extension method to subtract the elements from the second list from the first list to yield the names in listbox1 that are NOT in listbox2.
var listBox1 = ListBox1.Items.Cast<String>().ToList();
var listBox2 = ListBox2.Items.Cast<String>().ToList();
var resultSet = listBox1.Except(listBox2);
foreach (var item in resultSet)
{
// do something
}
First you need to get the selected value in each listbox then pass them to a string variable, then use if and else statements to compare
Example
If(a ==b)
{
//do nothing
}
else
{
//do something
}
Or you could always try it this way
If(a != b)
{
//do something
}
You can do a foreach loop to compare if the item on Listbox1 is not on Listbox2:
foreach (var item in listBox1.Items)
{
if (!listBox2.Items.Contains(item))
{
//TODO: Do your logic here
}
}
To answer:
check if any names listed in listbox1 are listed in listbox2 or not
foreach (var list1Item in listBox1.Items)
foreach(var list2Item in lisBox2.Items)
if (list1Item == list2Item)
{
// Identical items found. Handle with your code
}
And to:
If there is a name in listbox1 that is NOT in listbox2, then perform an action. How to do this?
foreach (string item in listBox1.Items)
if(!listBox2.Items.Contains(item))
{
// 'item' is present in listBox1, not in listBox2. Handle yourself.
}

ListBox multiple Selection get all selected values

I'm having a problem since a while now an just can't find any solution that works for me. I have a ListBox which is filled up with a DataTable like
listbox.DataSource = table;
listbox.Displaymember = "Name";
listbox.ValueMember = "ID";
If I now select an item in my listbox I can get it out like:
listbox.SelectedValue.toString();
My Problem:
What can I do if I would like to have ALL selected Values from a ListBox where multiple selection is enabled and save them all in an array or something like that?!
I can't use SelectedItems cause that is not giving me the information I need.
Try this:
var lst = listBox1.SelectedItems.Cast<DataRowView>();
foreach (var item in lst)
{
MessageBox.Show(item.Row[0].ToString());// Or Row[1]...
}
Or if you want only iterate over the selected items you can use SelectedIndices property:
foreach (int i in listbox.SelectedIndices)
{
// listbox.Items[i].ToString() ...
}
Or:
foreach (var item in listbox.SelectedItems)
{
MessageBox.Show(item.ToString());
}

Checked items in a CheckedListBox

In C#, how can I determine if an item in a CheckedListBox is checked or not if I have the text of the CheckListBoxItem?
I am needing to loop through all CheckedListBoxItems, and retrieve the text and the checked state.
Here is what I have so far:
CheckedListBox.ObjectCollection items = checkedListBoxFileNames.Items;
foreach (var item in items)
{
}
I am not sure on how to determine if an item is checked or not.
Thanks in advance.
You don't need this foreach loop.
Try this:
if(this.m_CheckedListbox.CheckedItems.Contains("Item1")
{
//make an action, if it's checked.
}
if(this.m_CheckedListbox.CheckedItems.Contains("Item2")
{
//make an action, if it's checked.
}
// etc...
// this.m_CheckedListbox should be the name of your checked list box.
You can use like
IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>()
where item.Selected
select int.Parse(item.Value));
more Details click here
Try like this:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
//your code
}
Also check CheckedListBox.CheckedItems Property
Collection of checked items in this CheckedListBox.

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources