locked
How to select duplicates in a listbox ? RRS feed

  • Question

  • Hello everyone,

    i want to select all duplicates items in my listbox but its select all expect 1  i want to select all duplicates  items in my list

      For Row As Int16 = 0 To Listrate.Items.Count - 2
                For RowAgain As Int16 = Listrate.Items.Count - 1 To Row + 1 Step -1
                    If Listrate.Items(Row).ToString = Listrate.Items(RowAgain).ToString AndAlso Listrate.Items(Row).ToString.Length = Listrate.Items(RowAgain).ToString.Length Then
                                          Listrate.SetSelected(RowAgain, True)
                    End If
                Next
            Next
    Thanks in advance


    Friday, January 31, 2020 2:43 PM

Answers

  • Hello,

    This can be done using GroupBy in a lambda statement which first gets each item and it's index in the ListBox the does a grouping on the text and returns an anonymous type of string/integer where a item is duplicated.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim duplicates = Listrate.Items.OfType(Of String).
                Select(Function(currentItem, index) New With {
                          Key .Index = index,
                          Key .Text = currentItem}).
                GroupBy(Function(g) g.Text).Where(Function(g) g.Count() > 1)
    
    
        For Each topItem In duplicates
            For Each value In topItem
                Listrate.SetSelected(value.Index, True)
            Next
        Next
    
    End Sub

     


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 31, 2020 3:18 PM

All replies

  • Hello,

    This can be done using GroupBy in a lambda statement which first gets each item and it's index in the ListBox the does a grouping on the text and returns an anonymous type of string/integer where a item is duplicated.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim duplicates = Listrate.Items.OfType(Of String).
                Select(Function(currentItem, index) New With {
                          Key .Index = index,
                          Key .Text = currentItem}).
                GroupBy(Function(g) g.Text).Where(Function(g) g.Count() > 1)
    
    
        For Each topItem In duplicates
            For Each value In topItem
                Listrate.SetSelected(value.Index, True)
            Next
        Next
    
    End Sub

     


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 31, 2020 3:18 PM
  • Thank u so much.
    Friday, January 31, 2020 3:55 PM
  • Thank u so much.
    Your welcome.

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 31, 2020 4:09 PM