Jump to content
  • Advertisement
Sign in to follow this  
Jsin

Help with a ListBox

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!