Sign in to follow this  
Jsin

Help with a ListBox

Recommended Posts

Jsin    100
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..

Share this post


Link to post
Share on other sites
WavyVirus    884
Hi Jsin,

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

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


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 []) - [b]listBox1.Items[/b][b][2][/b].

I believe [b]listBox.SelectedItem[/b] is essentially equivalent to [b]listBox.Items[ listBox.selectedIndex ][/b]. Be careful though - the documentation suggests that if multiple items are selected, these can return any of them randomly.

Share this post


Link to post
Share on other sites
DarrenHorton    110
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

[url="http://www.c-sharpcorner.com/UploadFile/mahesh/3071/"]http://www.c-sharpcorner.com/UploadFile/mahesh/3071/[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this