Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET General (http://www.xtremevbtalk.com/-net-general/)
-   -   Multi-select listbox (http://www.xtremevbtalk.com/-net-general/328564-multi-select-listbox.html)

Daigon Ali 02-03-2017 07:31 AM

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?

Goggy 02-16-2017 04:35 AM

Maybe this will help you
 
Code:

    Private Sub LstParts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstParts.SelectedIndexChanged
        With CType(sender, ListBox)
            If txtComments.Text = String.Empty Then
                txtComments.Text = .SelectedItem.ToString
            Else
                txtComments.Text &= vbNewLine & .SelectedItem.ToString
            End If
        End With
    End Sub


Daigon Ali 02-16-2017 06:44 AM

Hi Goggy,

Thanks for your reply. Unfortunately the problem persists in that if multiple items are selected only the first selected one is entered into the textbox.

I'm currently looping through all the items and using instr to see if and item is in the textbox, then adding or removing the item accordingly. However this doesn't seem very efficient.

Goggy 02-16-2017 07:29 AM

Your absolutly rite, try this instead.
 
Code:

    Private Sub LstParts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstParts.SelectedIndexChanged
        txtComments.Text = String.Empty
        For Each Item As Object In CType(sender, ListBox).SelectedItems
            txtComments.Text &= Item.ToString & vbNewLine
        Next
    End Sub



All times are GMT -6. The time now is 11:16 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.