4

Hi there What is the correct way of assigning the selected index's value from a listbox to a variable? The user selects an item in a listbox and then the output changes depending on their selection.

I use:

variablename = listbox.text

in the listBox_SelectedIndexChanged event and this works.

When I use the button_click event I use:

variablename = listbox.selectedindex 

But this does not work in the listbox_selectedindexchanged event.

Please could you let me know if it is okay to use it like I did above or if I will run into problems and why you cannot use the selectedindex method.

Thanks!

Wannabe
  • 493
  • 3
  • 10
  • 18

2 Answers2

4

A. It sounds like your variable is a string, and yet you are trying to assign to it the value returned by the SelectedIndex Property, which is an integer.

B. If you are trying to retrieve the value of the item associated with the SelectedINdex of the Listbox, use the Index to return the Object itself (the listbox is a list of Objects, which are often, but not always, going to be strings).

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    'THIS retrieves the Object referenced by the SelectedIndex Property (Note that you can populate
    'the list with types other than String, so it is not a guarantee that you will get a string
    'return when using someone else's code!):
    SelectedName = ListBox1.Items(ListBox1.SelectedIndex).ToString
    MsgBox(SelectedName)
End Sub

THIS is a little more direct, using the SelectedItem Property:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    'This returns the SelectedItem more directly, by using the SelectedItem Property
    'in the event handler for SelectedIndexChanged:
    SelectedName = ListBox1.SelectedItem.ToString
    MsgBox(SelectedName)

End Sub
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
XIVSolutions
  • 4,442
  • 19
  • 24
2

Well it depends what you want to achieve from the listbox's selected item.

There are a couple of possible ways, let me try to explain some of these for your homework.

Suppose you have a data table with two columns, and their rows...

ID    Title
_________________________
1     First item's title
2     Second item's title
3     Third item's title

And you bind this data table to your list box as,

ListBox1.DisplayMember = "ID";
ListBox1.ValueMember = "Title";

If user selects second item from the list box.

Now if you want to get the display value (Title) of the selected item, then you can do

string displayValue = ListBox1.Text;   // displayValue = Second item's title

OR even this to get same results.

// displayValue = Second item's title
string displayValue = ListBox1.SelectedItem.ToString();

And to get the value member against the selected item, you need to do

string selectedValue = ListBox1.SelectedValue;    // selectedValue = 2

Now there are situations when you want to allow user to select more than one item from the list box, so you then set

ListBox1.SelectionMode = SelectionMode.MultiSimple;

OR

ListBox1.SelectionMode = SelectionMode.MultiExtended;

Now suppose if user selects two items; second and third.

So you can get the display values by simply iterating through the SelectedItems

string displayValues = string.Empty;
foreach (object selection in ListBox1.SelectedItems)
{
    displayValues += selection.ToString() + ",";
}

// so displayValues = Second item's title, Third item's title,

And if you want to get ID's instead of Title's then...

I am also looking through it, I will post if found.

I hope your understandings build.

Good Luck!

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38