Link to home
Start Free TrialLog in
Avatar of Stealthrt
StealthrtFlag for United States of America

asked on

Listview Question

Hello, i am in need of some help working with a listview in VB.

I need to be able to add to a QTY within the list view... This is my code right now:

Private Sub Form_Load()
With LV1
        .ColumnHeaders.Add , , "QTY", 550
        .ColumnHeaders.Add , , "Item Name", 2500
        .ColumnHeaders.Add , , "Price"
        .View = lvwReport
End With
End Sub

Private Sub Command3_Click()
Dim ListObj As ListItem
Dim QTY, Name As String
Dim Price As String

Name = "Sandwich"
QTY = "1"
Price = "$" & "1.22"
        Set ListObj = LV1.ListItems.Add(, , QTY)
        ListObj.SubItems(1) = Name
        ListObj.SubItems(2) = Price
End Sub

This is what it looks like when i push the command button to test it out..
QTY  |   Item      |   Price
 1       Sandwich    $1.22
 1       Sandwich    $1.22

But i need it to do this...

QTY  |   Item      |   Price
 2       Sandwich    $2.44

So this is what i need:
 - The QTY to add if the button is hit more than just once
 - Add the Price if the button is hit more than just once

Thats it!

Thanks for your time,
David
Avatar of Dabas
Dabas
Flag of Australia image

Hi Stealthrt:
Does your listview have to be Qty Item Price, or can it be Item Qty Price
Reason I am asking is that you probably will need to use the ListView's FindItem function, and it will be easier if your ListItem's text property is the one to search

Dabas
Avatar of Stealthrt

ASKER

I would like it to be first so it could be read as 1 item price.... But if it wont work like that then i guess i could have it as item qty price.... But i really would like it more qty item price...

David
ASKER CERTIFIED SOLUTION
Avatar of Dabas
Dabas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked just fine Dabas...

Another question though... if i wanted to double click on that in the listview to subtract one from the quanity, how would i do that? Im able to do that now but only if its the same product.

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

Name = "CFA Sandwich"
Price = "$" & "1.22"
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

Any suggestions?

Thanks again,
David
Stealthrt:
>         ListObj.Text = Val(ListObj.Text - QTY)
Should be Val(ListObj.Text) - QTY

Dabas