locked
Listbox paste (prevent Duplicates) RRS feed

  • Question

  • i want to prevent duplicate items in the list box.  The code i am working with.

    ListBox1.Items.AddRange(Clipboard.GetText.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries))
    ListBox1.Sorted = True


    Sunday, April 26, 2020 4:51 PM

Answers

  • Use this

    Dim items = Clipboard.GetText.Split(
        New String() {vbCrLf},
        StringSplitOptions.RemoveEmptyEntries).Distinct()
    
    ListBox1.Items.AddRange(items.ToArray())
    ListBox1.Sorted = True


    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

    Sunday, April 26, 2020 5:25 PM

All replies

  • Hi

    Maybe, paste into a new listbox (not displayed) first and then find non duplicates to put into the actual listbox. Then dispose of temp listbox.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, April 26, 2020 5:36 PM
    Sunday, April 26, 2020 4:54 PM
  • Thank you.  How do i find non duplicates in the not displayed listbox....

    i'm very new to programming. 


    NB.com

    Sunday, April 26, 2020 5:04 PM
  • Use this

    Dim items = Clipboard.GetText.Split(
        New String() {vbCrLf},
        StringSplitOptions.RemoveEmptyEntries).Distinct()
    
    ListBox1.Items.AddRange(items.ToArray())
    ListBox1.Sorted = True


    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

    Sunday, April 26, 2020 5:25 PM
  • Thank you for your help. i ended up using 

      Dim Item1 As String()
            Item1 = (Clipboard.GetText.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries))
            Static c As Integer = 0
            For Each b In Item1
                If Not ListBox1.Items.Contains(b.Trim) Then
                    ListBox1.Items.Add(b.Trim)
                    ListBox1.Sorted = True
                    c = c + 1
                End If
            Next
            Label1.Text = c


    NB.com

    Sunday, April 26, 2020 6:50 PM
  • You mentioned you are very new to programming, your solution shows it and here are my comments.

    Your code used may get the job done for you but curious why take the long route and with that inefficient code used along with bad naming for variables then there is the static variable, can you provide a reasonable reason for using a static variable, zero sense to use a static local variable.




    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

    Sunday, April 26, 2020 7:16 PM
  • Hi Danny Macwan,

    I’m glad to hear that you solve the problem.

    As Kareninstructor mentioned, your code is not efficient.

    Here's the result of my test which use Stopwatch to measure the execution time of your code and Kareninstructor's.

    My code:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim watch = System.Diagnostics.Stopwatch.StartNew()
    
            Dim Item1 As String()
            Item1 = (Clipboard.GetText.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries))
            Static c As Integer = 0
            For Each b In Item1
                If Not ListBox1.Items.Contains(b.Trim) Then
                    ListBox1.Items.Add(b.Trim)
                    ListBox1.Sorted = True
                    c = c + 1
                End If
            Next
            Label1.Text = c
    
            watch.Stop()
            Dim elapsedMs = watch.ElapsedMilliseconds
    
            TextBox1.Text = elapsedMs
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ListBox1.Items.Clear()
    
            Dim watch = System.Diagnostics.Stopwatch.StartNew()
    
            Dim items = Clipboard.GetText.Split(
        New String() {vbCrLf},
        StringSplitOptions.RemoveEmptyEntries).Distinct()
    
            ListBox1.Items.AddRange(items.ToArray())
            ListBox1.Sorted = True
    
            watch.Stop()
            Dim elapsedMs = watch.ElapsedMilliseconds
    
            TextBox1.Text = elapsedMs
        End Sub
    Hope it can help you find a better solution.

    Besides, I hope you can click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Thank you and have a nice day.

    Best Regards,

    Xingyu Zhao



    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.


    Monday, April 27, 2020 3:29 AM
  • i must be doing something wrong...i am getting similar results first time i try to paste into the list box.

    If i do the same numbers a 2nd time button 2 is a lot faster..but the first time around i copied 3500 items and it took similar times. 

    i removed the static and am just using listbox1.items.count to at the end to get the count.


    NB.com

    Monday, April 27, 2020 4:26 AM
  • Hi Danny Macwan,

    Thanks for your feedback.

    I copy almost 3000 items and make a test again.

    Code:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim watch = System.Diagnostics.Stopwatch.StartNew()
    
            Dim Item1 As String()
            Item1 = (Clipboard.GetText.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries))
            For Each b In Item1
                If Not ListBox1.Items.Contains(b.Trim) Then
                    ListBox1.Items.Add(b.Trim)
                    ListBox1.Sorted = True
                End If
            Next
            Label1.Text = ListBox1.Items.Count
    
            watch.Stop()
            Dim elapsedMs = watch.ElapsedMilliseconds
            TextBox1.Text = elapsedMs
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ListBox1.Items.Clear()
            Dim watch = System.Diagnostics.Stopwatch.StartNew()
    
            Dim items = Clipboard.GetText.Split(
        New String() {vbCrLf},
        StringSplitOptions.RemoveEmptyEntries).[Select](Function(x) x.Trim()).Distinct()
    
            ListBox1.Items.AddRange(items.ToArray)
            ListBox1.Sorted = True
            Label1.Text = ListBox1.Items.Count
    
            watch.Stop()
            Dim elapsedMs = watch.ElapsedMilliseconds
            TextBox1.Text = elapsedMs
        End Sub

    Code in Button2 still perform better than the code in Button1, so I suggest you use the code in Button2.

    Best Regards,

    Xingyu Zhao


    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.

    Monday, April 27, 2020 6:42 AM
  • i did use the second equation.  but i'm still getting similar results with time.  Give this a try.  Launch the application.  press button 2 before you press button 1.  then relaunch the program and press button1 first.

    anyway...thank you everyone.  


    NB.com

    Monday, April 27, 2020 11:23 AM