Listed.selected index change variable enumeration

Hey. What is the correct way to assign a selected index value from a list to a variable? The user selects an item in the list, and then the output changes depending on their choice.

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 tell me whether it is correct to use it, as I did above, or if I have problems and why you cannot use the selectedindex method.

Thanks!

+4
source share
2 answers

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

C. If you are trying to get the value of an element associated with SelectedINdex from a list, use the index to return the object itself (a list is a list of objects that often, but not always, will 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 code!): SelectedName = ListBox1.Items(ListBox1.SelectedIndex).ToString MsgBox(SelectedName) End Sub 

THIS is a bit 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 
+4
source

Well, it depends on what you want to achieve from the selected list item.

There are several possible ways, let me try to explain some of them for your homework.

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

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

And you bind this data table to your list as

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

If the user selects the second item from the list.

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

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

OR even to get the same results.

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

And to get an element of value against the selected element, you need to do

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

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

 ListBox1.SelectionMode = SelectionMode.MultiSimple; 

OR

 ListBox1.SelectionMode = SelectionMode.MultiExtended; 

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

This way you can get the display values ​​just by going through SelectedItems

 string displayValues = string.Empty; foreach (object selection in ListBox1.SelectedItems) { displayValues += selection.ToString() + ","; } // so displayValues = Second item title, Third item title, 

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

I also look through it, I will send if I find it.

Hope your understanding is built.

Good luck

+2
source

Source: https://habr.com/ru/post/1346445/


All Articles