0

So I have an arraylist that will display data to a listbox. Then, the user can select an entry in the listbox. From there, I need to be able to grab whatever entry they select. I'm using VS 2010.

I have been trying

tempArray.Insert(0, myarray.IndexOf(mylistbox.SelectedIndex);

All this is doing is giving me the actual index number and not the contents of the index. I'm not sure how to index the arraylist to get the object that is contained at that index.

And yes, I know that I should be using List objects, but it is for class and we have yet to be taught list objects.

4

3 回答 3

0

If these are actual arrays you are working with, shouldn't you be able to to access what's in the array by using the [ ] syntax? i.e.

myArray[myListBox.SelectedIndex]
于 2013-11-11T17:23:05.750 回答
0

1) Dont use ArrayList... that's an old class. Use List<T> instead.

2) Have you tried ListBox.SelectedItem ? That seems a bit simpler...

于 2013-11-11T17:24:01.850 回答
0

As the name suggests .IndexOf() will return an index from the array.

Array.IndexOf Method

This is obviously not what you want.

What you should look at is using the SelectedItem instead of the SelectedIndex.

That way, you can access the object directly and insert it into your list. One thing to remember is that SelectedItem will return an Object. This means that it will have to be cast to the type that you expect to be using.

Also, are you wanting to constantly insert items to the top of the list or does it not matter. If you can append it to the end of the list, try using .Add(yourObject).

于 2013-11-11T17:31:16.940 回答