Results 1 to 15 of 15

Thread: add and remove items in a listbox

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Post add and remove items in a listbox

    I have been trying to make a listbox in which, everytime clicking a button a new item is added. And also, there is another button which removes selected items from the listbox. I've been unable to do both....
    First. I tried to add 4 items in a row for 4 colums. The problem is that .List reset everytime the listbox.
    Second. In order to remove the items I've been using .Selected() but when I run the user form it won't recognize the function...

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

    Re: add and remove items in a listbox

    What about using .AddItem to add and .RemoveItem? You can do the same thing with the .ListIndex property to determine which is selected.
    VB Code:
    1. Me.List1.List(Me.List1.ListIndex)
    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

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    In fact I'm using .AddItem, but I can't find out how to specify that the item should be displayed in four columns. At the moment, I'm able only to insert the item just in one row and one column.

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

    Re: add and remove items in a listbox

    You need to specify that its a multicolumn listbox.
    VB Code:
    1. ListBox1.ColumnCount = 3
    2. ListBox1.List() = MyArray 'Populate it with your multidimential array
    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
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    I still find two errors with this code:

    VB Code:
    1. Dim Name() As Variant
    2. ReDim Name(0, 3)
    3.  
    4. Name(0, 0) = item1
    5. Name(0, 1) = item2
    6. Name(0, 2) = item3
    7. Name(0, 3) = item4
    8.  
    9. lboList.ColumnWidths = 75
    10. lboList.ColumnCount = 4
    11. lboList.List() = Name()

    1. Everytime I run it, it adds other items in the same row
    2. The .ColumnWidths instruction works only for the first column
    Last edited by Fonty; Jun 24th, 2006 at 11:05 AM.

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

    Re: add and remove items in a listbox

    Your using a munildimensial array with only one column populated. Also dont use Name as a variable as its a reserved word.
    VB Code:
    1. Dim MyName() As Variant
    2. ReDim MyName(3, 3)
    3.  
    4. MyName(0, 0) = item1
    5. MyName(0, 1) = item1
    6. MyName(0, 2) = item1
    7. MyName(0, 3) = item1
    8.  
    9. MyName(1, 0) = item2
    10. MyName(1, 1) = item2
    11. MyName(1, 2) = item2
    12. MyName(1, 3) = item2
    13.  
    14. MyName(2, 0) = item3
    15. MyName(2, 1) = item3
    16. MyName(2, 2) = item3
    17. MyName(2, 3) = item3
    18.  
    19. lboList.ColumnCount = 3
    20. lboList.List() = MyName()
    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 Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    Every row has 4 items. But what if I'd like to add to select the value of each of the four items and then by clicking some button you can add this row. Then you compose another choice of 4 items and you add it to the next row. And so on.

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

    Re: add and remove items in a listbox

    You will be selecting items from where? and adding them to the listbox?
    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

  9. #9

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    For each item I have some option buttons to select their value. Then, once the 4 items values have been selected you, have the 1 row vector MyName(). You add your choice to the listbox. You compose another vector and you add it. So now you have two rows in the listbox.

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

    Re: add and remove items in a listbox

    Sorry but I dont quite follow the operat. Do your items get added vertically in a particular column or do they get added horizontally?

    You can also additems two dimentially too. This may be easier for your scererio

    VB Code:
    1. ListBox1.AddItem Text, Index
    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

  11. #11

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    I can't find out how to attach a file, but this is basically the code I wrote. Each Add button does something different. cmdAdd1 adds with .List and cmdAdd2 with .AddItem. With cmdAdd2 you can progressively add new items to the list box and with cmdAdd1 you can only one row.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim i As Long
    4. Dim Num As String
    5. Dim Lett As String
    6. Dim DLett As String
    7. Dim Vector() As Variant
    8. Dim MyName As String
    9. Private Sub cmdAdd2_Click()
    10.  
    11. If optProp1.Value = True Then
    12.     Num = "1"
    13. ElseIf optProp2.Value = True Then
    14.     Num = "2"
    15. End If
    16.  
    17. If optPropA.Value = True Then
    18.     Lett = "A"
    19. ElseIf optPropB.Value = True Then
    20.     Lett = "B"
    21. End If
    22.  
    23. If optPropAA.Value = True Then
    24.     DLett = "AA"
    25. ElseIf optPropAB.Value = True Then
    26.     DLett = "AB"
    27. End If
    28.  
    29. MyName = Num & vbTab & Lett & vbTab & DLett
    30.    
    31. With lboList
    32.     .ColumnCount = 1
    33.     .AddItem MyName
    34. End With
    35.  
    36. End Sub
    37. Private Sub cmdAdd1_Click()
    38.  
    39. If optProp1.Value = True Then
    40.     Num = "1"
    41. ElseIf optProp2.Value = True Then
    42.     Num = "2"
    43. End If
    44.  
    45. If optPropA.Value = True Then
    46.     Lett = "A"
    47. ElseIf optPropB.Value = True Then
    48.     Lett = "B"
    49. End If
    50.  
    51. If optPropAA.Value = True Then
    52.     DLett = "AA"
    53. ElseIf optPropAB.Value = True Then
    54.     DLett = "AB"
    55. End If
    56.  
    57. ReDim Vector(1, 3)
    58.  
    59. Vector(0, 0) = Num
    60. Vector(0, 1) = Lett
    61. Vector(0, 2) = DLett
    62.    
    63. With lboList
    64.     .ColumnCount = 3
    65.     .List() = Vector()
    66.     .ColumnWidths = 20
    67. End With
    68.  
    69. End Sub
    70.  
    71. Private Sub cmdRemove_Click()
    72.  
    73. With lboList
    74.     For i = .ListCount - 1 To 0 Step -1
    75.       If .Selected(i) Then .RemoveItem i
    76.     Next i
    77. End With
    78.  
    79. End Sub

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

    Re: add and remove items in a listbox

    What do you mean "attach a file"?
    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

  13. #13

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    To add a file to the thread.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: add and remove items in a listbox

    To attach a file, go to Additional Options in the Post Reply screen (towards the bottom of the screen, under where you enter your post). There's a section for attachments there.
    Tengo mas preguntas que contestas

  15. #15

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: add and remove items in a listbox

    Here's the code.
    Attached Files Attached Files

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