Link to home
Start Free TrialLog in
Avatar of ba272
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
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Look at this example...
http://www.thecodeproject.com/cs/media/SystemPainter.asp

it may help you..
R.K
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.Contains(listBox1.Items[i]))
           UnSelectedItems.Add(listBox1.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(UnSelectedItems[j].ToString());


hth,
Fahad Mukhtar