|
-
Jul 8th, 2004, 02:59 PM
#1
Thread Starter
Junior Member
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.
-
Jul 8th, 2004, 03:08 PM
#2
Maybe have..
VB Code:
Dim num_boxes As Integer 'quantity of items
Private Sub Command1_Click()
'do we already have this item in the list?
If num_boxes > 0 Then
'yes we have one already, so search for it
For i = 0 To Form2.List1.ListCount - 1
If Left(Form2.List1.List(i), 3) = "Box" Then
Form2.List1.RemoveItem i 'remove it
Form2.List1.AddItem "Box (" & num_boxes + 1 & ")" 'add it again with the new amount
num_boxes = num_boxes + 1
End If
Next i
Else
'we don't have one yet, so add it
Form2.List1.AddItem "Box (1)"
num_boxes = 1
End If
End Sub
Hope this helps,
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 8th, 2004, 03:10 PM
#3
VB Code:
Private Sub Command1_Click()
With Form2.List1
.List(.ListIndex) = .List(.ListIndex) & " - " & txtQty.Text
End With
End Sub
-
Jul 8th, 2004, 03:14 PM
#4
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:
Private Sub Command1_Click()
If num_boxes > 0 Then
num_boxes = num_boxes + 1 'moved that
'yes we have one already, so search for it
For i = 0 To Form2.List1.ListCount - 1
If Left(Form2.List1.List(i), 3) = "Box" Then
Form2.List1.List(i) = "Box (" & num_boxes & ")" 'changed that
End If
Next i
Else
'add it
Form2.List1.AddItem "Box (1)"
num_boxes = 1
End If
End Sub
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 8th, 2004, 03:14 PM
#5
Thread Starter
Junior Member
it works, but it says "Box (1)" then "Box(2)" instead of the selected item from listbox1
-
Jul 8th, 2004, 03:36 PM
#6
Oh, I didn't see that in your original post, sorry:
VB Code:
'List1 - Form1
'List 1 - Form2
'Command1 - Form1
Dim num_boxes As Integer
Private Sub Command1_Click()
If num_boxes > 0 Then
num_boxes = num_boxes + 1 'moved that
'yes we have one already, so search for it
For i = 0 To Form2.List1.ListCount - 1
If Left(Form2.List1.List(i), Len(List1.List(List1.ListIndex + 1))) = List1.List(List1.ListIndex + 1) Then
Form2.List1.List(i) = Me.List1.List(Me.List1.ListIndex) & " (" & num_boxes & ")" 'changed that
End If
Next i
Else
'add it
Form2.List1.AddItem List1.List(List1.ListIndex) & " (1)"
num_boxes = 1
End If
End Sub
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 9th, 2004, 10:46 AM
#7
Thread Starter
Junior Member
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.
-
Jul 9th, 2004, 12:13 PM
#8
You'll have to post your code, Velken.
-
Jul 9th, 2004, 01:08 PM
#9
Thread Starter
Junior Member
I used the code «°°phReAk°°» supplied.
VB Code:
'List1 - Form1
'List 1 - Form2
'Command1 - Form1
Dim num_boxes As Integer
Private Sub Command1_Click()
If num_boxes > 0 Then
num_boxes = num_boxes + 1 'moved that
'yes we have one already, so search for it
For i = 0 To Form2.List1.ListCount - 1
If Left(Form2.List1.List(i), Len(List1.List(List1.ListIndex + 1))) = List1.List(List1.ListIndex + 1) Then
Form2.List1.List(i) = Me.List1.List(Me.List1.ListIndex) & " (" & num_boxes & ")" 'changed that
End If
Next i
Else
'add it
Form2.List1.AddItem List1.List(List1.ListIndex) & " (1)"
num_boxes = 1
End If
End Sub
-
Jul 9th, 2004, 02:40 PM
#10
Here is a sample of my own. So try it out and let me know.
VB Code:
Private Sub Command1_Click()
Dim i%, sItem$, sQty%
'get new entry
sItem = Trim(txtItem.Text)
sQty = Val(Trim(txtQty.Text)) 'not the best way but it works to some extent
With Form2.List1
'check if item already exit
If .ListCount > 0 Then
For i = 0 To .ListCount - 1
If InStr(1, .List(i), sItem) > 0 Then
'it does - change it
.List(i) = sItem & " (" & txtQty.Text & ")"
Exit Sub
End If
Next i
End If
'it doesn't - add new
.AddItem sItem & " (" & txtQty.Text & ")"
End With
End Sub
Private Sub txtItem_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
End Sub
Private Sub txtQty_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
End Sub
-
Jul 9th, 2004, 02:55 PM
#11
Thread Starter
Junior Member
I copied it in, tried it, and got an Object Required Error on line :
sItem = Trim(txtItem.Text)
-
Jul 9th, 2004, 03:16 PM
#12
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
-
Jul 9th, 2004, 03:19 PM
#13
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|