View Single Post
 
Old 02-03-2017, 06:31 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 51
Question Multi-select listbox

Hi,

I'm coming from vb6 and am having an issue with the listbox.

It's a simple multi-select listbox, meaning that the user selects/deselects items with a single click, which shows a list of available parts.

The user has a textbox for comments which they can manually type in but they add/remove the parts to/from this textbox by selecting/deselecting parts in the listbox.

In VB6 it was possible to identify the item a user was clicking and it's selected status irrespective of the any other selections by referring to the listindex property which returned the index of the item the user clicked on:
Code:
Private Sub lstParts_Click()
With lstParts
    If .Selected(.ListIndex) = True Then
        If Len(txtComments.Text) = 0 Then
            'add selected item to textbox
            txtComments.Text = .List(.ListIndex)
        Else
            'append selected item to textbox
            txtComments.Text = txtComments.Text & vbNewLine & .List(.ListIndex)
        End If
    Else
'     code here for removing items from textbox if the item is deselected
    End If
End With
End Sub
I'm looking for an equivalent in VB.net and there are the selecteditem and selectedindex properties, but these always seem to return the first selected item in the listbox rather than the one the user has just clicked.

Does anyone know how the above code can be successfully achieved in vb.net?
Reply With Quote