Solved

Getting an un-selected item from a listbox

Posted on 2004-09-11
3
189 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:ba272
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 12035577
The Items collection gives you access to all the ListBox items.  It's available in both the Windows.Forms and the Web.UI... versions of ListBox.

So you'd do something like:

    listBox1.Items[n]

to access the item at index n (starting with 0).
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 12036763
Hi,

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

it may help you..
R.K
0
 
LVL 19

Expert Comment

by:Desp
ID: 12037625
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question