none
vb.net how to find items from listview by categories RRS feed

  • Question

  • hello all

    hello all

    I need to ask how to find items from listview by categories and copy to rich text box

    thanks 

    kind regards

    thanks 

    kind regards



    • Edited by AL SAMIR Friday, June 16, 2017 9:17 PM
    Friday, June 16, 2017 9:12 PM

Answers

  • hello all

    hello all

    I need to ask how to find items from listview by categories and copy to rich text box

    thanks 

    kind regards

    thanks 

    kind regards



    Hi

    Here is some code that does incorporate some of your points. This example allows you to Filter on the LV items and copy filtered items to a RTB.

    Alos, as it happens,allows sorting columns on click of column headers.

    ' Form1 with blank ListView named LV
    ' TextBox1 and Button1 (filter)
    ' Button2 (Copy to RTB)
    ' sorting is implemented by clicking on
    ' column header (< or >) glyphs shown
    
    ' no particular formating of items in RTB
    
    Option Strict On
    Option Explicit On
    Public Class Form1
        ' used for sorting the columns.
        Private m_SortingColumn As ColumnHeader
        Dim rand As New Random
        Dim Store As New List(Of ListViewItem)
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            LV.Items.Clear()
            LV.View = View.Details
            LV.Font = New Font(LV.Font.Name, 12)
            LV.Scrollable = True
    
            ' Make the ListView column headers.
            LVaddColumnHeader(LV, "Col1", HorizontalAlignment.Left, "Col2", HorizontalAlignment.Right, "Col3", HorizontalAlignment.Left, "Col4", HorizontalAlignment.Left, "Col5", HorizontalAlignment.Center, "Col6", HorizontalAlignment.Right)
    
            ' Add random data rows.
            For i As Integer = 0 To 10
                LVaddRow(LV, randdata(), randdata(), randdata(), randdata(), randdata(), randdata())
            Next
    
            ' Size the columns.
            For i As Integer = 0 To LV.Columns.Count - 1
                LV.Columns(i).Width = -1
            Next i
        End Sub
        ' make short random string for test data
        Private Function randdata() As String
            Dim TD As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Dim s As String = Nothing
            For i As Integer = 0 To 3 + rand.Next(3, 8)
                s &= TD(rand.Next(TD.Length))
            Next
            Return s
        End Function
    
        ' add a ListView row.
        Private Sub LVaddRow(ByVal lvw As ListView, ByVal item_title As String, ByVal ParamArray subitem_titles() As String)
            ' Make the item.
            Dim new_item As ListViewItem = lvw.Items.Add(item_title)
            ' Make the sub-items.
            For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0)
                new_item.SubItems.Add(subitem_titles(i))
            Next i
        End Sub
    
        ' add column headers.
        Private Sub LVaddColumnHeader(ByVal lvw As ListView, ByVal ParamArray header_info() As Object)
            ' Remove any existing headers.
            lvw.Columns.Clear()
    
            ' Make the column headers.
            For i As Integer = header_info.GetLowerBound(0) To header_info.GetUpperBound(0) Step 2
                lvw.Columns.Add(
    DirectCast(header_info(i), String), -1,
    DirectCast(header_info(i + 1), HorizontalAlignment))
            Next i
        End Sub
    
        ' Sort using the clicked column.
        Private Sub lvwBooks_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) Handles LV.ColumnClick
            ' Get the new sorting column.
            Dim new_sorting_column As ColumnHeader = LV.Columns(e.Column)
    
            ' Figure out the new sorting order.
            Dim sort_order As SortOrder
            If m_SortingColumn Is Nothing Then
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            Else
                ' See if this is the same column.
                If new_sorting_column.Equals(m_SortingColumn) Then
                    ' Same column. Switch the sort order.
                    If m_SortingColumn.Text.StartsWith("> ") Then
                        sort_order = SortOrder.Descending
                    Else
                        sort_order = SortOrder.Ascending
                    End If
                Else
                    ' New column. Sort ascending.
                    sort_order = SortOrder.Ascending
                End If
    
                ' Remove the old sort indicator.
                m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
            End If
    
            ' Display the new sort order.
            m_SortingColumn = new_sorting_column
            If sort_order = SortOrder.Ascending Then
                m_SortingColumn.Text = "> " & m_SortingColumn.Text
            Else
                m_SortingColumn.Text = "< " & m_SortingColumn.Text
            End If
    
            ' Create a comparer.
            LV.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
    
            ' Sort.
            LV.Sort()
        End Sub
        Dim filtered As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Select Case Button1.Text
                Case "Clear Filter"
                    LV.Items.Clear()
                    For Each item As ListViewItem In Store
                        LV.Items.Add(item)
                    Next
                    filtered = False
                    Button1.Text = "Filter"
                Case Else
                    If Not String.IsNullOrEmpty(TextBox1.Text) Then FilterLV()
            End Select
        End Sub
        Sub FilterLV()
            LV.BeginUpdate()
    
            If Not filtered Then
                        Store.Clear()
                For Each item As ListViewItem In LV.Items
                    Store.Add(item)
                Next
            End If
    
                    LV.Items.Clear()
                    For Each LVitem As ListViewItem In Store
                        If LVitem.Text.ToLower.Contains(TextBox1.Text.ToLower) Then
                            Console.WriteLine(LVitem.Text)
                            LV.Items.Add(LVitem)
                        End If
                    Next
                    filtered = True
                    Button1.Text = "Clear Filter"
            LV.EndUpdate()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If filtered Then
                RichTextBox1.Clear()
                For Each LVitem As ListViewItem In LV.Items
                    With RichTextBox1
                        For i As Integer = 0 To LVitem.SubItems.Count - 1
                            .AppendText(LVitem.SubItems(i).Text & "  ")
                        Next
                        .AppendText(vbNewLine)
                    End With
                Next
            End If
        End Sub
    End Class
    
    ' Implements a comparer for ListView columns.
    Class ListViewComparer
        Implements IComparer
    
        Private m_ColumnNumber As Integer
        Private m_SortOrder As SortOrder
    
        Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
            m_ColumnNumber = column_number
            m_SortOrder = sort_order
        End Sub
    
        ' Compare the items in the appropriate column
        ' for objects x and y.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
            Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
    
            ' Get the sub-item values.
            Dim string_x As String
            If item_x.SubItems.Count <= m_ColumnNumber Then
                string_x = ""
            Else
                string_x = item_x.SubItems(m_ColumnNumber).Text
            End If
    
            Dim string_y As String
            If item_y.SubItems.Count <= m_ColumnNumber Then
                string_y = ""
            Else
                string_y = item_y.SubItems(m_ColumnNumber).Text
            End If
    
            ' Compare them.
            If m_SortOrder = SortOrder.Ascending Then
                Return String.Compare(string_x, string_y)
            Else
                Return String.Compare(string_y, string_x)
            End If
        End Function
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by AL SAMIR Saturday, June 17, 2017 8:55 PM
    Friday, June 16, 2017 11:06 PM

All replies

  • I need to ask how to find items from listview by categories and copy to rich text box

    If GroupList is the form that contains the text box that you want to populate, and if the group your want to display in the text box is group 1 and the text you want to display is the default text for the listview items, then this code will do it:

        Dim Groups As New GroupList
        Dim ThisGroup As ListViewGroup = ListView1.Groups(1)
        For Each obj As Object In ThisGroup.items
            Groups.RichTextBox1.AppendText(obj.ToString & vbCrLf)
        Next
        Groups.Show()

    If your code doesn't match that pattern then you will need to adjust.



    • Edited by Acamar Friday, June 16, 2017 10:33 PM fmt
    Friday, June 16, 2017 10:24 PM
  • Hello,

    In the future, please close out prior questions before moving on to a new question.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 16, 2017 10:51 PM
    Moderator
  • hello all

    hello all

    I need to ask how to find items from listview by categories and copy to rich text box

    thanks 

    kind regards

    thanks 

    kind regards



    Hi

    Here is some code that does incorporate some of your points. This example allows you to Filter on the LV items and copy filtered items to a RTB.

    Alos, as it happens,allows sorting columns on click of column headers.

    ' Form1 with blank ListView named LV
    ' TextBox1 and Button1 (filter)
    ' Button2 (Copy to RTB)
    ' sorting is implemented by clicking on
    ' column header (< or >) glyphs shown
    
    ' no particular formating of items in RTB
    
    Option Strict On
    Option Explicit On
    Public Class Form1
        ' used for sorting the columns.
        Private m_SortingColumn As ColumnHeader
        Dim rand As New Random
        Dim Store As New List(Of ListViewItem)
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            LV.Items.Clear()
            LV.View = View.Details
            LV.Font = New Font(LV.Font.Name, 12)
            LV.Scrollable = True
    
            ' Make the ListView column headers.
            LVaddColumnHeader(LV, "Col1", HorizontalAlignment.Left, "Col2", HorizontalAlignment.Right, "Col3", HorizontalAlignment.Left, "Col4", HorizontalAlignment.Left, "Col5", HorizontalAlignment.Center, "Col6", HorizontalAlignment.Right)
    
            ' Add random data rows.
            For i As Integer = 0 To 10
                LVaddRow(LV, randdata(), randdata(), randdata(), randdata(), randdata(), randdata())
            Next
    
            ' Size the columns.
            For i As Integer = 0 To LV.Columns.Count - 1
                LV.Columns(i).Width = -1
            Next i
        End Sub
        ' make short random string for test data
        Private Function randdata() As String
            Dim TD As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Dim s As String = Nothing
            For i As Integer = 0 To 3 + rand.Next(3, 8)
                s &= TD(rand.Next(TD.Length))
            Next
            Return s
        End Function
    
        ' add a ListView row.
        Private Sub LVaddRow(ByVal lvw As ListView, ByVal item_title As String, ByVal ParamArray subitem_titles() As String)
            ' Make the item.
            Dim new_item As ListViewItem = lvw.Items.Add(item_title)
            ' Make the sub-items.
            For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0)
                new_item.SubItems.Add(subitem_titles(i))
            Next i
        End Sub
    
        ' add column headers.
        Private Sub LVaddColumnHeader(ByVal lvw As ListView, ByVal ParamArray header_info() As Object)
            ' Remove any existing headers.
            lvw.Columns.Clear()
    
            ' Make the column headers.
            For i As Integer = header_info.GetLowerBound(0) To header_info.GetUpperBound(0) Step 2
                lvw.Columns.Add(
    DirectCast(header_info(i), String), -1,
    DirectCast(header_info(i + 1), HorizontalAlignment))
            Next i
        End Sub
    
        ' Sort using the clicked column.
        Private Sub lvwBooks_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) Handles LV.ColumnClick
            ' Get the new sorting column.
            Dim new_sorting_column As ColumnHeader = LV.Columns(e.Column)
    
            ' Figure out the new sorting order.
            Dim sort_order As SortOrder
            If m_SortingColumn Is Nothing Then
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            Else
                ' See if this is the same column.
                If new_sorting_column.Equals(m_SortingColumn) Then
                    ' Same column. Switch the sort order.
                    If m_SortingColumn.Text.StartsWith("> ") Then
                        sort_order = SortOrder.Descending
                    Else
                        sort_order = SortOrder.Ascending
                    End If
                Else
                    ' New column. Sort ascending.
                    sort_order = SortOrder.Ascending
                End If
    
                ' Remove the old sort indicator.
                m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
            End If
    
            ' Display the new sort order.
            m_SortingColumn = new_sorting_column
            If sort_order = SortOrder.Ascending Then
                m_SortingColumn.Text = "> " & m_SortingColumn.Text
            Else
                m_SortingColumn.Text = "< " & m_SortingColumn.Text
            End If
    
            ' Create a comparer.
            LV.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
    
            ' Sort.
            LV.Sort()
        End Sub
        Dim filtered As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Select Case Button1.Text
                Case "Clear Filter"
                    LV.Items.Clear()
                    For Each item As ListViewItem In Store
                        LV.Items.Add(item)
                    Next
                    filtered = False
                    Button1.Text = "Filter"
                Case Else
                    If Not String.IsNullOrEmpty(TextBox1.Text) Then FilterLV()
            End Select
        End Sub
        Sub FilterLV()
            LV.BeginUpdate()
    
            If Not filtered Then
                        Store.Clear()
                For Each item As ListViewItem In LV.Items
                    Store.Add(item)
                Next
            End If
    
                    LV.Items.Clear()
                    For Each LVitem As ListViewItem In Store
                        If LVitem.Text.ToLower.Contains(TextBox1.Text.ToLower) Then
                            Console.WriteLine(LVitem.Text)
                            LV.Items.Add(LVitem)
                        End If
                    Next
                    filtered = True
                    Button1.Text = "Clear Filter"
            LV.EndUpdate()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If filtered Then
                RichTextBox1.Clear()
                For Each LVitem As ListViewItem In LV.Items
                    With RichTextBox1
                        For i As Integer = 0 To LVitem.SubItems.Count - 1
                            .AppendText(LVitem.SubItems(i).Text & "  ")
                        Next
                        .AppendText(vbNewLine)
                    End With
                Next
            End If
        End Sub
    End Class
    
    ' Implements a comparer for ListView columns.
    Class ListViewComparer
        Implements IComparer
    
        Private m_ColumnNumber As Integer
        Private m_SortOrder As SortOrder
    
        Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
            m_ColumnNumber = column_number
            m_SortOrder = sort_order
        End Sub
    
        ' Compare the items in the appropriate column
        ' for objects x and y.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
            Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
    
            ' Get the sub-item values.
            Dim string_x As String
            If item_x.SubItems.Count <= m_ColumnNumber Then
                string_x = ""
            Else
                string_x = item_x.SubItems(m_ColumnNumber).Text
            End If
    
            Dim string_y As String
            If item_y.SubItems.Count <= m_ColumnNumber Then
                string_y = ""
            Else
                string_y = item_y.SubItems(m_ColumnNumber).Text
            End If
    
            ' Compare them.
            If m_SortOrder = SortOrder.Ascending Then
                Return String.Compare(string_x, string_y)
            Else
                Return String.Compare(string_y, string_x)
            End If
        End Function
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by AL SAMIR Saturday, June 17, 2017 8:55 PM
    Friday, June 16, 2017 11:06 PM
  • Sorry because I work on a project I have two day to finish

    Friday, June 16, 2017 11:18 PM
  • Sorry because I work on a project I have two day to finish

    Hi

    Nothing like a dead line to make you sweat - hope you sweat loads.


    Regards Les, Livingston, Scotland

    Friday, June 16, 2017 11:55 PM
  • Sorry because I work on a project I have two day to finish

    Hi

    Nothing like a dead line to make you sweat - hope you sweat loads.


    Regards Les, Livingston, Scotland

    sorry my replay not for you for "karen payne" 

    thanks 

    Saturday, June 17, 2017 11:21 AM