Solved

Getting an un-selected item from a listbox

Posted on 2004-09-11
3
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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