Results 1 to 3 of 3

Thread: Visual Basic Help Multiplying Textbox integer with selected listbox decimal?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    8

    Visual Basic Help Multiplying Textbox integer with selected listbox decimal?

    So I'm stuck on this for some time with no luck. Here is my code that i have so far.


    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' fills the list box with prices

    For decPrice As Decimal = 5 To 10 Step 0.5D
    lstPrices.Items.Add(decPrice.ToString("C2"))
    Next decPrice
    lstPrices.SelectedIndex = 0
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    ' calculates the total due

    Dim intQuantity As Integer
    Dim decPrice As Decimal
    Dim decDue As Decimal

    Integer.TryParse(txtQuantity.Text, intQuantity)
    Decimal.TryParse(lstPrices.SelectedItem.ToString, decPrice)

    txtQuantity.Text = intQuantity.ToString

    decDue = intQuantity * decPrice
    lblDue.Text = decDue.ToString("C2")


    decDue = intQuantity * decPrice is just not working. I keep getting $0.00.Name:  Debug 2.PNG
Views: 1689
Size:  9.1 KB

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Visual Basic Help Multiplying Textbox integer with selected listbox decimal?

    Put a breakpoint on the line for the calculation. When execution stops at the breakpoint, hover the mouse over intQuantity, then hover the mouse over decPrice. What are the values in the two variables (they should show up in a tooltip, if not, click on the variable and press Shift+F9). Most likely, it is the decPrice that is not right. So, at that point, highlight lstPrices.SelectedItem from a couple lines earlier, and press Shift+F9. What do you see? Not what you were expecting, I'd guess.
    My usual boring signature: Nothing

  3. #3
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Visual Basic Help Multiplying Textbox integer with selected listbox decimal?

    Remove currency symbol from listbox:

    Code:
    Option Explicit On
    Option Strict On
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' fills the list box with prices 
            For decPrice As Decimal = 5 To 10 Step 0.5D
                lstPrices.Items.Add(decPrice)
            Next decPrice
            lstPrices.SelectedIndex = 0
        End Sub
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            ' calculates the total due 
            Dim intQuantity As Integer
            Dim decPrice As Decimal
            Dim decDue As Decimal
    
            Integer.TryParse(txtQuantity.Text, intQuantity)
            Decimal.TryParse(lstPrices.SelectedItem.ToString, decPrice)
    
            txtQuantity.Text = intQuantity.ToString
    
            decDue = intQuantity * decPrice
            lblDue.Text = decDue.ToString("C2")
           
    
            decDue = CDec(intQuantity * decPrice)
            lblDue.Text = decDue.ToString("C2")
        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