# Listview Questions

I am in need of solving my listview problem.

I have code for two command buttons as well as the form load:

Private Sub Command1_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Charsand"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.findItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
End Sub

Private Sub Command2_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Sandwich"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.findItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
End Sub

With LV1
.View = lvwReport
End With
End Sub

Now it adds the QTY just fine every time i push the button... but i now need it to subtract the QTY every time i double-click one in the list view.. Once the QTY is at 0, delete the item from the list view.

I also need a way to total up the PRICE everytime one of the command buttons are push (Price * QTY). As well as subtracting it when its double-clicked in the listview box.

David
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Private iX As Single
Private iY As Single

Private Sub Command1_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Charsand"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.FindItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
End Sub

Private Sub Command2_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Sandwich"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.FindItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
End Sub

With LV1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
End With
End Sub

Private Sub LV1_DblClick()

Dim ListObj As ListItem 'Set listObj as a listitem
With LV1
Set ListObj = .HitTest(iX, iY)
If Not ListObj Is Nothing Then
If Val(ListObj.Text) > 1 Then
ListObj.Text = Val(ListObj.Text) - 1
Else
LV1.ListItems.Remove ListObj.Index
End If
End If
End With

End Sub

Private Sub LV1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

iX = x
iY = y

End Sub
0
Commented:
This one includes the total:

Private iX As Single
Private iY As Single

Private Sub Command1_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Charsand"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.FindItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
ListObj.SubItems(3) = "\$" & Format(ListObj.Text * Right(ListObj.SubItems(2), Len(ListObj.SubItems(2)) - 1), "#.00")
End Sub

Private Sub Command2_Click()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "Sandwich"
Price = "\$" & "1.88"
QTY = 1
Set ListObj = LV1.FindItem(Name, lvwSubItem)
If ListObj Is Nothing Then
Set ListObj = LV1.ListItems.Add(, , QTY)
Else
ListObj.Text = Val(ListObj.Text) + QTY
End If

ListObj.SubItems(1) = Name
ListObj.SubItems(2) = Price
ListObj.SubItems(3) = "\$" & Format(ListObj.Text * Right(ListObj.SubItems(2), Len(ListObj.SubItems(2)) - 1), "#.00")
End Sub

With LV1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
End With
End Sub

Private Sub LV1_DblClick()

Dim ListObj As ListItem 'Set listObj as a listitem
With LV1
Set ListObj = .HitTest(iX, iY)
If Not ListObj Is Nothing Then
If Val(ListObj.Text) > 1 Then
ListObj.Text = Val(ListObj.Text) - 1
ListObj.SubItems(3) = "\$" & Format(ListObj.Text * Right(ListObj.SubItems(2), Len(ListObj.SubItems(2)) - 1), "#.00")
Else
LV1.ListItems.Remove ListObj.Index
End If
End If
End With

End Sub

Private Sub LV1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

iX = x
iY = y

End Sub
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
JR2003: That seems to work bring down the total, but it does not add to the Total. The total stays the same as the QTY goes up or down.
0
Commented:
Stealthrt,
I does work, There is another column called 'Total' at the end of the ListView which keeps the total amount for each listItem.
Are you using the 2nd piece of code I pasted in?
JR
0
Author Commented:
JR2003: Yes, i have used your 2nd piece of code. When i put a stop in the code of one of the command buttons to see the value of this line:
ListObj.SubItems(3) = "\$" & Format(ListObj.Text * Right(ListObj.SubItems(2), Len(ListObj.SubItems(2)) - 1), "#.00")

The value of ListObj.Subitems(3) when i hit the command button 3 times is \$3.76 BUT it never displays in the listview that... It only has the default \$1.88. Which come to think about it... 3 x 1.88 = \$5.64, not \$3.76??
0
Commented:
Stealthrt,
If you are adding an item then it will be  \$3.76 before the line in question executes, you are probably looking at the contents of SubItem(3) before it is updated.
I have added a new column to the listview to the right og all the columns called 'Total'. Thisd is created in the Form_Load. You must have this line or the additional column won't show:
JR
0
Author Commented:
JR2003: I do have that in my load form:

With LV1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
End With
End Sub

Still doesnt update the price after every push of the command button nor double-clicking it.
0
Author Commented:
Sorry about that... I did not scroll far enough to see the Total colum..

Thanks for the code, works great!

:o)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.