Results 1 to 8 of 8

Thread: [RESOLVED] Removing Items with the same name in a ListBox

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    Resolved [RESOLVED] Removing Items with the same name in a ListBox

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. List1.AddItem "item"
    4. List1.AddItem "item"
    5. List1.AddItem "item"
    6.  
    7. End Sub

    How could I make the ListBox automatically remove items with the same name?
    My questions might look stupid to others. After all I am still in the process of learning Visual Basic.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Removing Items with the same name in a ListBox

    Just specify the index number instead unless that is if you are wanting to remove them all.
    VB Code:
    1. 'Remove just the selected item:
    2. List1.RemoveItem List1.ListIndex
    3.  
    4. 'Remove the second item:
    5. List1.RemoveItem 1
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Lively Member
    Join Date
    Dec 2005
    Posts
    112

    Re: Removing Items with the same name in a ListBox

    Did you mean if listbox having same items then remove it automatically
    same items??

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Removing Items with the same name in a ListBox

    Also, it would be best to prevent the duplicates from being added in the first place.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    Re: Removing Items with the same name in a ListBox

    Yes Nida u got my point.

    That's not exactly what I'm looking for. For example In List1 there are the following items:

    adf
    fdasf
    fdas
    df
    dsafd
    213dfas
    fdasdfsa
    dfsaf
    dsafd
    yr7
    658i
    gh
    87udj
    gh
    asdf
    uy
    e5er
    sdawf
    dasf
    aas
    aas
    htry
    asfd
    aas


    I want a command that would remove all items that are duplicates. Except one (because one needs to stay).
    My questions might look stupid to others. After all I am still in the process of learning Visual Basic.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Removing Items with the same name in a ListBox

    Oh, well this will do it for you that way then.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, _
    4. ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    5.  
    6. Private Const LB_FINDSTRINGEXACT As Long = &H1A2
    7. Private Const LB_FINDSTRING As Long = &H18F
    8.  
    9. Private Sub Command1_Click()
    10.     Dim i As Integer
    11.     Dim lRet As Long
    12.     For i = List1.ListCount - 1 To 0 Step -1
    13.         lRet = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, ByVal 0&, ByVal List1.List(i))
    14.         If lRet <> i Then
    15.             List1.RemoveItem lRet
    16.         End If
    17.     Next
    18. End Sub
    19.  
    20. Private Sub Form_Load()
    21.     List1.AddItem "Test1"
    22.     List1.AddItem "Test1"
    23.     List1.AddItem "Test2"
    24.     List1.AddItem "Test2"
    25.     List1.AddItem "Test2"
    26.     List1.AddItem "Test3"
    27. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: Removing Items with the same name in a ListBox

    Try this code, ofcourse this is meant for a combobox but I am sure someone can do this for listbox. I would have done it but don't have VB installed on the computer I am serfing the net from

    VB Code:
    1. Private Const CB_FINDSTRINGEXACT = &H158
    2. Private Const CB_ERR = (-1)
    3.  
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    5.             (ByVal hwnd As Long, ByVal wMsg As Long, _
    6.             ByVal wParam As Long, lParam As Long) As Long
    7.  
    8. Private Sub cmdRemoveDups()
    9.     Dim x As Integer, y As Integer
    10.  
    11.     For x = cmbSortServiceDate.ListCount - 1 To 0 Step (-1)
    12.         y = SendMessage(cmbSortServiceDate.hwnd, CB_FINDSTRINGEXACT, CB_ERR, ByVal cmbSortServiceDate.List(x))
    13.        
    14.         If y <> x And y <> CB_ERR Then
    15.             cmbSortServiceDate.RemoveItem x
    16.         End If
    17.     Next
    18. End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    Re: Removing Items with the same name in a ListBox

    Thanks you guys.
    My questions might look stupid to others. After all I am still in the process of learning Visual Basic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width