locked
How to Display listview items in listbox RRS feed

  • Question

  • i want to be able to display the lists of items in the first column of a Listview control on a listbox control.  And also be able to display the subitems or values  from the Listview control on a label when i select an item from the listbox.

    Below is a sample of my code:

    For Each lvi As ListViewItem In FrmDataServer.ListView1.Items
                    ListParameters.Items.Add(lvi.Text)
                Next


                ListParameters.DataSource = FrmDataServer.ListView1.Items.Add(0) 'ds
                ListParameters.DisplayMember = "item(0)" 'This is the firt column
                ListParameters.ValueMember = "SubItems(0)" 'This is the sond column or subitem

    • Edited by Charlion69 Thursday, May 9, 2019 9:22 PM
    Thursday, May 9, 2019 9:03 PM

All replies

  • i want to be able to display the lists of items in the first column of a Listview control on a listbox control.  And also be able to display the subitems or values  from the Listview control on a label when i select an item from the listbox.

    Below is a sample of my code:

    For Each lvi As ListViewItem In FrmDataServer.ListView1.Items
                    ListParameters.Items.Add(lvi.Text)
                Next


                ListParameters.DataSource = FrmDataServer.ListView1.Items.Add(0) 'ds
                ListParameters.DisplayMember = "item(0)" 'This is the firt column
                ListParameters.ValueMember = "SubItems(0)" 'This is the sond column or subitem

    Hi

    Here is some code that may help.

    It is a stand alone example project. If you want to try it, start a new project with a Blank Form1 and copy/replace the default code with this code.

    This example has little or no error handling and uses some dummy data built at run time.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim WithEvents listView1 As New ListView()
        Dim WithEvents listbox1 As New ListBox
        Dim Label1 As New Label
        Dim LBdatasource As New List(Of String)
        Dim rand As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            CreateMyListView()
            CreateMyListBox()
            CreateLabel()
            listbox1.DataSource = LBdatasource
            Controls.AddRange({listView1, listbox1, Label1})
        End Sub
        Sub CreateLabel()
            With Label1
                .AutoSize = False
                .Location = New Point(10, listView1.Bottom + 4)
                .Width = ClientSize.Width - 20
                .BackColor = Color.LightPink
                .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            End With
        End Sub
        Sub CreateMyListBox()
            With listbox1
                ' set loc
                .Location = New Point(ClientSize.Width \ 2 + 5, 5)
                ' set size
                .Size = New Size(ClientSize.Width \ 2 - 5, ClientSize.Height - 40)
                .IntegralHeight = False
                .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Right
            End With
        End Sub
        Private Sub CreateMyListView()
            ' Create new ListView.
            With listView1
                ' set loc
                .Location = New Point(5, 5)
                ' set size
                .Size = New Size(ClientSize.Width \ 2 - 5, ClientSize.Height - 40)
                ' Set to details.
                .View = View.Details
                ' Allow item edit.
                .LabelEdit = True
                ' Allow rearrange columns.
                .AllowColumnReorder = True
                ' check boxes.
                .CheckBoxes = True
                ' Select whole row.
                .FullRowSelect = True
                ' Show grid lines.
                .GridLines = True
                .Columns.Add("Item Column", -2, HorizontalAlignment.Left)
                .Columns.Add("Column 2", -2, HorizontalAlignment.Left)
                .Columns.Add("Column 3", -2, HorizontalAlignment.Left)
                .Columns.Add("Column 4", -2, HorizontalAlignment.Center)
                .Columns.Add("Column 5", -2, HorizontalAlignment.Center)
                .Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
                .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
    
                ' make some dummy test data
                For itm As Integer = 1 To 19
                    Dim item As New ListViewItem("item" & itm.ToString, rand.Next(0, 2))
                    Dim tf As Boolean = True
                    If rand.Next(0, 2) = 1 Then tf = False
                    item.Checked = tf
                    item.SubItems.Add((rand.NextDouble * rand.Next(100, 199)).ToString("0.00"))
                    item.SubItems.Add((rand.NextDouble * rand.Next(200, 299)).ToString("0.00"))
                    item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.00"))
                    item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.00"))
                    item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.00"))
                    LBdatasource.Add(item.Text)
                    .Items.Add(item)
                Next
            End With
        End Sub
        Private Sub listView1_MouseMove(sender As Object, e As MouseEventArgs) Handles listView1.MouseMove
            Static intLast As Integer = 0
            Dim htInfo As ListViewHitTestInfo
            If Not listView1.View = View.Details Then
                listView1.Items(intLast).BackColor = listView1.BackColor
            Else
                htInfo = listView1.HitTest(e.Location)
    
                If Not htInfo.Item Is Nothing Then
    
                    If htInfo.Item.Index <> intLast Then
                        listView1.Items(intLast).Selected = False
                        htInfo.Item.Selected = True
                        intLast = htInfo.Item.Index
                    End If
                End If
            End If
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listbox1.SelectedIndexChanged
            Label1.Text = Nothing
            Dim lb As ListBox = DirectCast(sender, ListBox)
            Dim item As ListViewItem
            item = listView1.FindItemWithText(lb.SelectedItem.ToString)
            For Each si As ListViewItem.ListViewSubItem In item.SubItems
                Label1.Text &= si.Text & Space(3)
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, May 9, 2019 10:45 PM
  • Hi,

    I made a demo,you can see it.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("column1", 80, HorizontalAlignment.Left)
            ListView1.Columns.Add("column2", 120, HorizontalAlignment.Left)
            ListView1.Columns.Add("column3", 120, HorizontalAlignment.Left)
            For i As Integer = 0 To 4
                Dim lvi As ListViewItem = New ListViewItem()
                lvi.ImageIndex = i
                lvi.Text = "subitem" & i
                lvi.SubItems.Add("column2," & i & "row")
                lvi.SubItems.Add("column3," & i & "row")
                Me.ListView1.Items.Add(lvi)
            Next
    
    
            For Each lvi As ListViewItem In ListView1.Items
                ListBox1.Items.Add(lvi.Text)
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Label1.Text = ListView1.Items(ListBox1.SelectedIndex).SubItems(1).Text
            Label2.Text = ListView1.Items(ListBox1.SelectedIndex).SubItems(2).Text
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 6:08 AM