Results 1 to 6 of 6

Thread: help please!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    18

    Unhappy 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

  2. #2
    Junior Member RvA's Avatar
    Join Date
    Oct 2002
    Location
    USA
    Posts
    19
    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!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    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

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Junior Member RvA's Avatar
    Join Date
    Oct 2002
    Location
    USA
    Posts
    19
    Hello,

    Ok I rewote a few section and this works here.
    VB Code:
    1. ' declare some form level vars
    2.   Dim dTotalQuantity, dTotalCost As Double
    3.  
    4.   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.     Dim I As Integer
    6.    
    7.     ' didn't need the loop or select case but it was fun
    8.     For I = 0 To 2
    9.    
    10.       Select Case I
    11.         Case 0
    12.           lstProduct.Items.Add("Computers")
    13.           lstCost.Items.Add("1500.00")
    14.           lstQuantity.Items.Add("5")
    15.  
    16.         Case 1
    17.           lstProduct.Items.Add("Cell Phones")
    18.           lstCost.Items.Add("150")
    19.           lstQuantity.Items.Add("13")
    20.  
    21.         Case 2
    22.           lstProduct.Items.Add("Optical Mouse")
    23.           lstCost.Items.Add("10")
    24.           lstQuantity.Items.Add("25")
    25.       End Select
    26.     Next I
    27.  
    28.     ' get the values
    29.     For I = 0 To lstProduct.Items.Count - 1
    30.       dTotalCost += lstCost.Items.Item(I)
    31.       dTotalQuantity += lstQuantity.Items.Item(I)
    32.     Next
    33.    
    34.     ' show the values
    35.     lblTotalCost.Text = dTotalCost
    36.     lblTotalQuantity.Text = dTotalQuantity
    37.  
    38.   End Sub
    39.  
    40.  
    41.   Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
    42.     Dim dQuantity, dProductCost As Double
    43.    
    44.     ' do some work
    45.     If lstProduct.SelectedIndex <> -1 Then
    46.       dQuantity = lstQuantity.SelectedItem
    47.       dTotalQuantity = dTotalQuantity - dQuantity
    48.       lblTotalQuantity.Text = dTotalQuantity
    49.  
    50.       dTotalCost = Convert.ToDouble(lblTotalCost.Text)
    51.       dTotalCost = dTotalCost - lstCost.SelectedItem
    52.       lblTotalCost.Text = dTotalCost.ToString
    53.  
    54.       lstQuantity.Items.RemoveAt(lstProduct.SelectedIndex)
    55.       lstCost.Items.RemoveAt(lstProduct.SelectedIndex)
    56.       lstProduct.Items.RemoveAt(lstProduct.SelectedIndex)
    57.  
    58.     Else
    59.       MsgBox("PLease select an item to be removed")
    60.  
    61.     End If
    62.   End Sub
    63.  
    64.   Private Sub lstProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstProduct.SelectedIndexChanged
    65.  
    66.     ' just to make sure we have all the items selected
    67.     ' if the other listbox's are used by the user
    68.     ' you may want to add similare code in those
    69.     ' listboxes as well
    70.     lstQuantity.SelectedIndex = lstProduct.SelectedIndex
    71.     lstCost.SelectedIndex = lstProduct.SelectedIndex
    72.  
    73.   End Sub
    74.  
    75.  
    76. End Class

    Hope this helps a little.
    Best,

    Roger

    VB6.0 SR5 & VB.Net Pro
    -----------------------------------------------
    Do or do not, there is no try!

  6. #6
    New Member
    Join Date
    Dec 2002
    Posts
    5
    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
  •  



Click Here to Expand Forum to Full Width