ba272
asked on
Getting an un-selected item from a listbox
Hi,
I have no problem getting a selected item from a listbox. But I'd like to get an item (by item number) which is not selected. For example, I'd like the item at position zero and each item thereafter. But I'd like to do this without having to select each item before I can access the item.
I looked in the C# help but it's not obvious to me.
Thanks for the help,
RA
I have no problem getting a selected item from a listbox. But I'd like to get an item (by item number) which is not selected. For example, I'd like the item at position zero and each item thereafter. But I'd like to do this without having to select each item before I can access the item.
I looked in the C# help but it's not obvious to me.
Thanks for the help,
RA
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To get a single unselected item you can use the by-default 'Items' collection of listBox object:
listBox1.Items[n]
but you MUST be sure that Item at Items[n] is NOT SELECTED because it may be possible that the Items[n] is selected as the Items Collection contains all the items (Selected + Unselected)
The following Code will make an arraylist of All UN SELECTED items in a listbox:
ArrayList UnSelectedItems = new ArrayList();
for (int i =0 ; i < listBox1.Items .Count ; i++)
if (!listBox1.SelectedItems.C ontains(li stBox1.Ite ms[i]))
UnSelectedItems.Add(listBo x1.Items[i ]);
Later you can print the values to see that all the printed values are NOT SELECTED
for (int i =0 ;j<UnSelectedItems.Count ;j++)
Console.WriteLine(UnSelect edItems[j] .ToString( ));
hth,
Fahad Mukhtar
listBox1.Items[n]
but you MUST be sure that Item at Items[n] is NOT SELECTED because it may be possible that the Items[n] is selected as the Items Collection contains all the items (Selected + Unselected)
The following Code will make an arraylist of All UN SELECTED items in a listbox:
ArrayList UnSelectedItems = new ArrayList();
for (int i =0 ; i < listBox1.Items .Count ; i++)
if (!listBox1.SelectedItems.C
UnSelectedItems.Add(listBo
Later you can print the values to see that all the printed values are NOT SELECTED
for (int i =0 ;j<UnSelectedItems.Count ;j++)
Console.WriteLine(UnSelect
hth,
Fahad Mukhtar
Look at this example...
http://www.thecodeproject.com/cs/media/SystemPainter.asp
it may help you..
R.K