[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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

Private Sub Form_Load()
    With LV1
        .ColumnHeaders.Add , , "QTY", 550
        .ColumnHeaders.Add , , "Item Name", 2500
        .ColumnHeaders.Add , , "Price"
        .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.

Thanks for your help,
David
0
Stealthrt
Asked:
Stealthrt
  • 4
  • 4
1 Solution
 
JR2003Commented:
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

Private Sub Form_Load()
    With LV1
        .FullRowSelect = True
        .LabelEdit = lvwManual
        .ColumnHeaders.Add , , "QTY", 550
        .ColumnHeaders.Add , , "Item Name", 2500
        .ColumnHeaders.Add , , "Price"
        .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
 
JR2003Commented:
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

Private Sub Form_Load()
    With LV1
        .FullRowSelect = True
        .LabelEdit = lvwManual
        .ColumnHeaders.Add , , "QTY", 550
        .ColumnHeaders.Add , , "Item Name", 2500
        .ColumnHeaders.Add , , "Price"
        .ColumnHeaders.Add , , "Total"
        .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
 
StealthrtAuthor 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JR2003Commented:
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
 
StealthrtAuthor 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
 
JR2003Commented:
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:
      .ColumnHeaders.Add , , "Total"
JR
0
 
StealthrtAuthor Commented:
JR2003: I do have that in my load form:

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

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

Thanks for the code, works great!

:o)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now