Results 1 to 13 of 13

Thread: Quantity in a ListBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27

    Quantity in a ListBox

    I'm having trouble coding a listbox that works with quantity.

    What I am trying to do:
    I have a list box on form1 where you select what you would like to add to a listbox on form2 by pressing a commandbutton on form1. Also by pressing the commandbutton each time a quantity will appear next to the item in the listbox on form2.

    Thanks for any help coding something like that.

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Maybe have..
    VB Code:
    1. Dim num_boxes As Integer 'quantity of items
    2.  
    3. Private Sub Command1_Click()
    4. 'do we already have this item in the list?
    5. If num_boxes > 0 Then
    6.     'yes we have one already, so search for it
    7.     For i = 0 To Form2.List1.ListCount - 1
    8.         If Left(Form2.List1.List(i), 3) = "Box" Then
    9.             Form2.List1.RemoveItem i 'remove it
    10.             Form2.List1.AddItem "Box (" & num_boxes + 1 & ")" 'add it again with the new amount
    11.             num_boxes = num_boxes + 1
    12.         End If
    13.     Next i
    14. Else
    15.     'we don't have one yet, so add it
    16.     Form2.List1.AddItem "Box (1)"
    17.     num_boxes = 1
    18. End If
    19. End Sub

    Hope this helps,

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    VB Code:
    1. Private Sub Command1_Click()
    2.     With Form2.List1
    3.         .List(.ListIndex) = .List(.ListIndex) & " - " & txtQty.Text
    4.     End With
    5. End Sub

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Oh, stupid me. Too long have I been away from Visual Basic. I think this is what you wanted, but using Rhinobull's inspiration
    VB Code:
    1. Private Sub Command1_Click()
    2. If num_boxes > 0 Then
    3.     num_boxes = num_boxes + 1 'moved that
    4.     'yes we have one already, so search for it
    5.     For i = 0 To Form2.List1.ListCount - 1
    6.         If Left(Form2.List1.List(i), 3) = "Box" Then
    7.             Form2.List1.List(i) = "Box (" & num_boxes & ")" 'changed that
    8.         End If
    9.     Next i
    10. Else
    11.     'add it
    12.     Form2.List1.AddItem "Box (1)"
    13.     num_boxes = 1
    14. End If
    15. End Sub

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    it works, but it says "Box (1)" then "Box(2)" instead of the selected item from listbox1

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Oh, I didn't see that in your original post, sorry:
    VB Code:
    1. 'List1 - Form1
    2. 'List 1 - Form2
    3. 'Command1 - Form1
    4.  
    5. Dim num_boxes As Integer
    6. Private Sub Command1_Click()
    7. If num_boxes > 0 Then
    8.     num_boxes = num_boxes + 1 'moved that
    9.     'yes we have one already, so search for it
    10.     For i = 0 To Form2.List1.ListCount - 1
    11.         If Left(Form2.List1.List(i), Len(List1.List(List1.ListIndex + 1))) = List1.List(List1.ListIndex + 1) Then
    12.             Form2.List1.List(i) = Me.List1.List(Me.List1.ListIndex) & " (" & num_boxes & ")" 'changed that
    13.         End If
    14.     Next i
    15. Else
    16.     'add it
    17.     Form2.List1.AddItem List1.List(List1.ListIndex) & " (1)"
    18.     num_boxes = 1
    19. End If
    20. End Sub

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    Doesn't work. It will add the selected item from the first listbox to the second with a (1) the first time the commandbutton is clicked but every other time it is clicked nothing happens.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You'll have to post your code, Velken.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    I used the code «°°phReAk°°» supplied.

    VB Code:
    1. 'List1 - Form1
    2. 'List 1 - Form2
    3. 'Command1 - Form1
    4.  
    5. Dim num_boxes As Integer
    6. Private Sub Command1_Click()
    7. If num_boxes > 0 Then
    8.     num_boxes = num_boxes + 1 'moved that
    9.     'yes we have one already, so search for it
    10.     For i = 0 To Form2.List1.ListCount - 1
    11.         If Left(Form2.List1.List(i), Len(List1.List(List1.ListIndex + 1))) = List1.List(List1.ListIndex + 1) Then
    12.             Form2.List1.List(i) = Me.List1.List(Me.List1.ListIndex) & " (" & num_boxes & ")" 'changed that
    13.         End If
    14.     Next i
    15. Else
    16.     'add it
    17.     Form2.List1.AddItem List1.List(List1.ListIndex) & " (1)"
    18.     num_boxes = 1
    19. End If
    20. End Sub

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Here is a sample of my own. So try it out and let me know.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i%, sItem$, sQty%
    3.  
    4.     'get new entry
    5.     sItem = Trim(txtItem.Text)
    6.     sQty = Val(Trim(txtQty.Text)) 'not the best way but it works to some extent
    7.    
    8.     With Form2.List1
    9.         'check if item already exit
    10.         If .ListCount > 0 Then
    11.             For i = 0 To .ListCount - 1
    12.                 If InStr(1, .List(i), sItem) > 0 Then
    13.                     'it does - change it
    14.                     .List(i) = sItem & " (" & txtQty.Text & ")"
    15.                     Exit Sub
    16.                 End If
    17.             Next i
    18.         End If
    19.         'it doesn't - add new
    20.         .AddItem sItem & " (" & txtQty.Text & ")"
    21.     End With
    22.  
    23. End Sub
    24.  
    25. Private Sub txtItem_KeyPress(KeyAscii As Integer)
    26.     If KeyAscii = 13 Then Command1_Click
    27. End Sub
    28.  
    29. Private Sub txtQty_KeyPress(KeyAscii As Integer)
    30.     If KeyAscii = 13 Then Command1_Click
    31. End Sub

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    I copied it in, tried it, and got an Object Required Error on line :

    sItem = Trim(txtItem.Text)

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Velken,
    it's just a sample project and I used some textbox to type item name and another textbox (txtQty) to type actual qty so I can test it. In your project item and qty are something that only YOU may know where to get them from. You have to be a little more creative and learn how to read someone else's code and not just COPY/PASTE some silly sample regardles of its readyness.

    Cheers

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    my mistake ... ill mess with it and see what I can do.
    I do appreciate the help btw.

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