Multi-select listbox
Multi-select listbox
Multi-select listbox
Multi-select listbox
Multi-select listbox
Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox
Multi-select listbox Multi-select listbox
Multi-select listbox
Go Back  Xtreme Visual Basic Talk > > > Multi-select listbox


Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2017, 07:31 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
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
  #2  
Old 02-16-2017, 04:35 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default 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
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #3  
Old 02-16-2017, 06:44 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
Default

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.
Reply With Quote
  #4  
Old 02-16-2017, 07:29 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default 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
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
Reply

Tags
item, textbox, user, listbox, selected, txtcomments.text, clicked, code, multi-select, .list.listindex, vb.net, vb6, items, vbnewline, removing, lentxtcomments.text, achieved, add, successfully, deselected, append, properties, return, selectedindex, selecteditem


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Multi-select listbox
Multi-select listbox
Multi-select listbox Multi-select listbox
Multi-select listbox
Multi-select listbox
Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox Multi-select listbox
Multi-select listbox
Multi-select listbox
 
Multi-select listbox
Multi-select listbox
 
-->