8 Replies - 7428 Views - Last Post: 10 December 2014 - 11:08 AM Rate Topic: -----

#1 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Selecting Multiple Items At Once in a ListBox

Posted 03 December 2014 - 04:22 PM

Hello,

I'm still fairly new to VB.Net (only been learning it at college for just over a year now), and I need some help with a ListBox.

My ListBox contains items in the following format:

Item Name
Item Number
Item Price
Item Description

Item Name
Item Number
Item Price
Item Description

etc.

What I'd like to happen is, when you select any item in one chunk, all other items in that chunk will be highlighted with it. So if you select the item's price, it's name, number and description will also be highlighted.

I already know that you can change the selection mode of the ListBox to be MultiSimple or MultiExtended (not sure what the difference is), but that only lets you select multiple items manually, one at a time. I need the lines to be automatically selected all at once.

Is this possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Selecting Multiple Items At Once in a ListBox

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Selecting Multiple Items At Once in a ListBox

Posted 03 December 2014 - 06:17 PM

It's possible but would require a lot of work. For one you would need to determine what the user selected and then work back from there to know what to multi-select.

Another options would be to create a class to hold these 4 items and only display the name to the user. From that you could retrieve the other items in the object from the selection of the name.
Was This Post Helpful? 1
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 05:27 AM

Hi,
If you are set on using a ListBox then CharlieMay has suggested about the best way to go about doing it. However, if you don`t mind learning a little about the ListView or DataGridView cotrols, ether one would be ideal for something like this.

With the ListView or DataGridView you would add a Column for each type of data like (Name | Number | Price | Discription) and then just add a Row or ListViewItem depending on which control you use, to add the information.

So, your information for each part is in one row but, separated in columns like below. I know the formatting of the text on here will probably screw up my example below but, hopefully you can get the idea.

Name | Number | Price | Discription
-----------------------------------
100k | x21100 | $0.05 | Resistor <- Row 1
200k | x21200 | $0.05 | Resistor <- Row 2

Then when the user selects a row there would be no hassle to figure out what other items are suppose to be selected with it because, it would all be in the selected Row or ListViewItem. :)
Was This Post Helpful? 2
  • +
  • -

#4 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 08:11 AM

CharlieMay: Yes, that's the thing I'm having trouble with. You're right about the class - in fact, I already have a class which stores this information (we've been told to do it so that the information for each product is taken from a .txt file and then displayed in the ListBox). I was thinking that, if I really couldn't get it to do exactly what I wanted, I could just have two ListBoxes, one which lists the product names and then another ListBox that shows the other details (so I could have a 'More Details' button).

IronRazer: That sounds perfect! I was going to ask how I could display it so that it looks like each property is displayed on multiple lines, when actually, each property is written across one line of code. So really, all the properties are in one row, but it looks like they're not. I know with concatenation, you can join strings together with '&' (so "Hi" & "my name is " & "Twisted Fang"). So I wondered if there was a similar method you could use to add whitespace or make one string appear immediately below the previous string. Your suggestion sounds much easier, though.
Was This Post Helpful? 0
  • +
  • -

#5 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 241
  • Joined: 13-November 13

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 08:20 AM

With some effort it can be done. Good question. Put one listbox and an exit button on a form and try this code:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim lcounter As Integer = 0
        Dim lcounter2 As Integer = 0
        'fill listbox with itms
        For lcounter = 0 To 3
            For lcounter2 = 0 To 3
                ListBox1.Items.Add(lcounter2.ToString)
            Next
            ListBox1.Items.Add("")
        Next
    End Sub
    Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick
        Dim curItem As String = ""
        Dim curItem1 As String = ""
        Dim intnum As Integer = ListBox1.SelectedIndex
        Dim intnum2 As Integer = ListBox1.SelectedIndex
        Dim newInd As Integer = 0
        Dim icount1 As Integer = 0
        curItem = CStr(ListBox1.SelectedItem)
        If curItem = "" Then
            MsgBox("Nothing")
            ListBox1.ClearSelected()
            Exit Sub
        End If
        If curItem <> "" Then
            For icount1 = 1 To 4
                ListBox1.SetSelected(intnum2, True)
                curItem1 = ListBox1.Items(intnum2).ToString
                If curItem1 = "" Then
                    Debug.Print("Original Index " & intnum.ToString)
                    Debug.Print("Counter  " & (icount1 - 1).ToString)
                    Debug.Print(ListBox1.SelectedIndex.ToString)
                    'now find the new starting index
                    newInd = intnum - (5 - icount1)
                    ListBox1.ClearSelected()
                    selNew(newInd)
                    Exit Sub
                End If
                intnum2 += 1
            Next
        End If
    End Sub
    Private Sub ListBox1_MouseEnter(sender As Object, e As EventArgs) Handles ListBox1.MouseEnter
        If ListBox1.SelectedIndices.Count > 1 Then
            ListBox1.ClearSelected()
        End If
    End Sub
    Private Sub selNew(intn As Integer)
        Dim icount As Integer = 0
        For icount = 1 To 4
            ListBox1.SetSelected(intn, True)
            intn += 1
        Next
        MsgBox("Move mouse out of listbox" & vbCrLf & " to clear selected items")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 08:45 AM

If you have to use a listbox, then I would personally use a class to create an object. You can then override the .ToString method to display the name only in the listbox when in fact, the entire contents of the object is stored.

Then when you click on an item in the listbox, you can cast the selected item back to its proper type and gain access to its other properties

Public Class car
    Public Property Make As String
    Public Property Model As String
    Public Property Year As String

    Public Overrides Function ToString() As String
        'Return MyBase.ToString    (this is what is auto generated)
        'instead you could 
        Return Make
    End Function
End Class


Now when you create a new object based off this class and add it to a listbox, only the Make of the car object will be displayed

On the click of an item in the listbox you would simply
'create a temporary variable of type Car and use the selecteditem of the listbox to cast it to its proper type
Dim CarSelected as Car = DirectCast(ListBox1.SelectedItem, Car)
'At this point you could put the Model and Year into 2 textboxes
   TextBox1.Text = CarSelected.Model
   TextBox2.Text = CarSelected.Year
'This would reside in the SelectedIndexChanged event of the listbox



Was This Post Helpful? 2
  • +
  • -

#7 mrd777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-October 14

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 01:18 PM

What I'd like to happen is, when you select any item in one chunk, all other items in that chunk will be highlighted with it. So if you select the item's price, it's name, number and description will also be highlighted.

I believe he wants FullRowSelect

Go to the properties panel for the list and set FullRowSelect to True
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: Selecting Multiple Items At Once in a ListBox

Posted 04 December 2014 - 02:17 PM

View Postmrd777, on 04 December 2014 - 08:18 PM, said:

I believe he wants FullRowSelect

Go to the properties panel for the list and set FullRowSelect to True

FullRowSelect is for a ListView, not a ListBox.
Was This Post Helpful? 0
  • +
  • -

#9 Twisted Fang   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 07-October 14

Re: Selecting Multiple Items At Once in a ListBox

Posted 10 December 2014 - 11:08 AM

Thank you everyone for such quick responses. I'm sorry for my own delay. I didn't want to reply until I'd actually made changes.

I've now changed my ListBox to a ListView and changed the code so that it can still add things to the user's 'cart' like it could before. I followed mrd777's advice and set the FullRowSelect property to True. For what I'm trying to do, it works a lot better. Thank you for the help. Luckily, I don't have to use a ListBox. So long as I'm displaying all of the class attributes at some point, I can do it however I like.

I will try out Minimalist's and CharlieMay's code at some point and get back them regarding it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1