Help with a ListBox

Started by
1 comment, last by DarrenHorton 12 years, 11 months ago
Hey guys I'm just needing a little direction here. I know how to add items to the list box. What I want to know is when I click on an item in the list box and want it to display its text in a label, do I need to create an array or is there a method in listBox that can handle this? I'm doing this in C#. Thanks for the help..
Advertisement
Hi Jsin,

Check out this, from Microsoft's C# docuentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items.aspx

Essentially, you can access the currently selected item in a ListBox with listBox1.SelectedItem. If multiple items can be selected, look into listBox1.SelectedItems, which you can access like listBox1.SelectedItems[0].


ListBox already provides you with an array of the items it contains (actually it's an ObjectCollection, but you can access elements by index with []) - listBox1.Items[2].

I believe listBox.SelectedItem is essentially equivalent to listBox.Items[ listBox.selectedIndex ]. Be careful though - the documentation suggests that if multiple items are selected, these can return any of them randomly.
Here's a good article about the list box.It should give you some pointers on how to capture the text and then display it in a label

http://www.c-sharpcorner.com/UploadFile/mahesh/3071/

This topic is closed to new replies.

Advertisement