Search code examples
c#selecteditemselectedindex

selecteditems and selectedindex c#


I'm new to C# and I'm starting to learn how to program I'm learning to program into the Visual Studio Microsoft Edition where I use the WindowsApplication instead of the Console. While trying to make this code, I encountered this command: Selected Index and Selected Item and I would like to know the difference between the two. I'm quite confused now with my code. The code I'm trying to do is adding and deleting text in the listbox.

Thanks for your help.

Additional question: in my code I have this line:

int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);

I would like to understand this part: The first line, has a variable called " listBoxSelectedItem" with a type "int". The position of the item you selected will be store to the variable called "listBoxSelectedItem". Is that correct?

The second line is, the "listBox1.SelectedIndex" is the information that is being pass through to the method, "RemoveAt" Is my understanding here correct?

Thanks


Solution

  • Selected item will return the object that is selected. Selected index returns the location in the list as an int.

    For example you may have a list of strings:

    Cat
    Dog
    Hamster
    Horse
    

    If you select "Dog" from this list them the SelectedItem property is the string "Dog" while the SelectedIndex is 1 (indexes are zero based, so the first item is 0, second 1 etc.)