|
-
Dec 3rd, 2002, 07:40 PM
#1
Thread Starter
Junior Member
help please!!
ok guys i really need u to have a look at this.
... my prob. is when i remove a quanitity or cost from their lstBoxs, the totals should be subtracted from that quantity or cost. but instead both totals go up by one???
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
If lstProduct.SelectedIndex <> -1 Then
quantity = CDbl(lstQuantity.SelectedIndex)
totalQuantity = Val(lblTotalQuantity.Text) - quantity
lblTotalQuantity.Text = CStr(totalQuantity)
totalQuantity = 0 ' Intialising totalQuantity
totalCost = CDec(Val(lblTotalCost.Text) - lstCost.SelectedIndex)
lblTotalCost.Text = CStr(totalCost)
totalCost = 0 ' initialising totalcost
lstQuantity.Items.RemoveAt(lstProduct.SelectedIndex)
lstCost.Items.RemoveAt(lstProduct.SelectedIndex)
lstProduct.Items.RemoveAt(lstProduct.SelectedIndex)
Else
MsgBox("PLease select an item to be removed")
End If
End Sub
also i changed to something like this:
quantity = CDbl(lstProduct.SelectedIndex) , but it doesnot do any subtractions...
thanks
-
Dec 3rd, 2002, 09:09 PM
#2
Junior Member
Hello aerohaith,
I see this is your second post on this problem. Maybe you could explain in some detail what your trying to do. Then once we can get what your trying to do I'm sure you'll get the help your looking for.
Best,
Roger
VB6.0 SR5 & VB.Net Pro
-----------------------------------------------
Do or do not, there is no try!
-
Dec 3rd, 2002, 10:53 PM
#3
Thread Starter
Junior Member
i'm trying to get the quatities& costs totals to work ,when an item has been deleted. the logic is when an item get deteled ,then the total quantity and total cost should be reduced as an item has just been removed. so basically , i would like to that.
what happens with this code ,is an item is removed ,but the totals go up by one , instead of its being reduced
-
Dec 4th, 2002, 09:38 AM
#4
i already answered this question once.
lstCost.SelectedIndex and the other one returning -1 becasue you dont hve an item selected in those listboxes.
a subtracting -1 from a number is the same as +1
-
Dec 6th, 2002, 02:30 PM
#5
Junior Member
Hello,
Ok I rewote a few section and this works here.
VB Code:
' declare some form level vars
Dim dTotalQuantity, dTotalCost As Double
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim I As Integer
' didn't need the loop or select case but it was fun
For I = 0 To 2
Select Case I
Case 0
lstProduct.Items.Add("Computers")
lstCost.Items.Add("1500.00")
lstQuantity.Items.Add("5")
Case 1
lstProduct.Items.Add("Cell Phones")
lstCost.Items.Add("150")
lstQuantity.Items.Add("13")
Case 2
lstProduct.Items.Add("Optical Mouse")
lstCost.Items.Add("10")
lstQuantity.Items.Add("25")
End Select
Next I
' get the values
For I = 0 To lstProduct.Items.Count - 1
dTotalCost += lstCost.Items.Item(I)
dTotalQuantity += lstQuantity.Items.Item(I)
Next
' show the values
lblTotalCost.Text = dTotalCost
lblTotalQuantity.Text = dTotalQuantity
End Sub
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
Dim dQuantity, dProductCost As Double
' do some work
If lstProduct.SelectedIndex <> -1 Then
dQuantity = lstQuantity.SelectedItem
dTotalQuantity = dTotalQuantity - dQuantity
lblTotalQuantity.Text = dTotalQuantity
dTotalCost = Convert.ToDouble(lblTotalCost.Text)
dTotalCost = dTotalCost - lstCost.SelectedItem
lblTotalCost.Text = dTotalCost.ToString
lstQuantity.Items.RemoveAt(lstProduct.SelectedIndex)
lstCost.Items.RemoveAt(lstProduct.SelectedIndex)
lstProduct.Items.RemoveAt(lstProduct.SelectedIndex)
Else
MsgBox("PLease select an item to be removed")
End If
End Sub
Private Sub lstProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstProduct.SelectedIndexChanged
' just to make sure we have all the items selected
' if the other listbox's are used by the user
' you may want to add similare code in those
' listboxes as well
lstQuantity.SelectedIndex = lstProduct.SelectedIndex
lstCost.SelectedIndex = lstProduct.SelectedIndex
End Sub
End Class
Hope this helps a little.
Best,
Roger
VB6.0 SR5 & VB.Net Pro
-----------------------------------------------
Do or do not, there is no try!
-
Dec 8th, 2002, 04:36 PM
#6
New Member
Here you are Ithink this is what you are looking for
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
Dim Index As Integer = lstProduct.SelectedIndex
If lstProduct.SelectedIndex = -1 Then
MsgBox("Please Select a Product")
Exit Sub
End If
'remove
lstProduct.Items.RemoveAt(lstProduct.SelectedIndex)
totalSum = totalSum - CDbl(lstCost.Items(Index))
txtTotalCost.Text = CStr(totalSum)
totalQuantity = totalQuantity - CDbl(lstQuantity.Items(Index))
txtTotalQuantity.Text = CStr(totalQuantity)
lstCost.Items.RemoveAt(Index)
lstQuantity.Items.RemoveAt(Index)
End Sub
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
|