Results 1 to 7 of 7

Thread: Listbox position question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    208

    Listbox position question

    I have a listbox with:

    Steve
    John
    Kara 1
    Bridget 1
    Sam 1

    I need to add a new name between the name without the 1 and with the 1.

    how can I loop through the listbox and find that position and and add the new name in that spot.

    thanks

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

    Re: Listbox position question

    Here is one of my search functions for a combo box.

    http://vbforums.com/showpost.php?p=1820829&postcount=4

    Then use the AddItem and specify the index position at which to insert the new item at.
    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 djklocek's Avatar
    Join Date
    Aug 2006
    Posts
    107

    Re: Listbox position question

    VB Code:
    1. Dim number as Integer
    2. For j = 0 To List1.ListCount
    3.        frmtstr = Right(List1.List(j), 2)
    4.            If frmtstr = " 1" Then
    5.              Laber1.Caption = j
    6.              Exit Sub
    7.            End If
    8. Next j

    It returns position to label1. Then you just have to add item to listbox like
    List1.AddItem "newitem", number

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

    Re: Listbox position question

    If you have a large list then it will slow down because of the looping.
    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
    Lively Member djklocek's Avatar
    Join Date
    Aug 2006
    Posts
    107

    Re: Listbox position question

    Quote Originally Posted by RobDog888
    If you have a large list then it will slow down because of the looping.
    Yes, I unfortunatelly cannot deny that .

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

    Re: Listbox position question

    If you dont have any or many loops then it really wont affect your apps performance too much but if you have many of them then it will definately be visible.

    By creating a function and using the API non-looping method you are only makinging one call to directly find the item. If you place the function in a module and make it public then you can use it repeatly throughout your program. Less code, centralized, easier management.
    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

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    208

    Re: Listbox position question

    thanks djk, im going to work with it and see if it matches what im doing...any problems ill post.

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