dcsimg
Results 1 to 6 of 6

Thread: The fourth textbox value not display when multiply by 10 onward

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Posts
    94

    Exclamation The fourth textbox value not display when multiply by 10 onward

    Dear Friend,
    I have 4 textbox the problem is that the fourth textbox is not display the result as require when multiply by 10 onwards. First two text box i use for multiplying the value of textbox 1 and textbox 2 and display the result in third textbox, but when adding the third textbox value with the fourth textbox value (fourth the value is already populate from database) and display the result in the fourth textbox, here the value is not display properly. kindly correct me where i make mistake in my code below.

    Code:
    Private Sub pro_unit_price_txt_TextChanged(sender As Object, e As EventArgs) Handles pro_unit_price_txt.TextChanged
            Dim tempo As String
    
            tempo = Val(pro_unit_price_txt.Text) * Val(pro_pieces_txt.Text)
    
            total_amt.Text = tempo
    
            Dim temp As String
            Dim temp1 As String
    
            temp = Val(grand_total_amt.Text)
            temp1 = Val(tempo) + temp
    
            grand_total_amt.Text = temp1
    
        End Sub
    Last edited by kiki2012; Feb 26th, 2015 at 02:46 PM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: The fourth textbox value not display when multiply by 10 onward

    I'm not exactly sure what is wrong other than "the value is not display properly" (ie what did you input, what did you expect to be the output, and what is the actual output), but I'm tempted to guess that part of the problem might be that you are not using the correct DataTypes. I'd suggest you turn Option Strict On to help with providing the correct DataTypes for your variables. For example, you have "Dim temp1 As String" thereby making this variable a String. You then have "temp1 = Val(tempo) + temp" which I'm assuming means you want to add tempo and temp together. However the + operator for Strings is for concatenation, not addition. Thus what gets stored in temp1 might be a concatenation of tempo and temp instead of adding the 2 values together. Rewriting your code using proper DataTypes would look more like:
    Code:
        Private Sub pro_unit_price_txt_TextChanged(sender As Object, e As EventArgs) Handles pro_unit_price_txt.TextChanged
            ' Since this TextBox is named pro_unit_price, it looks like this is for calculating financial data, so the best DataType for money is Decimals
            ' Also, add some more variables to convert the other TextBox values into decimals as well as to hold the calculated results
            Dim ProUnitPrice As Decimal
            Dim ProPieces As Decimal
            Dim TotalAmount As Decimal
            Dim GrandTotal As Decimal
            
            ' Use TryParse to convert the TextBox values into the correct DataType instead of using the outdated Val()
            If Not Decimal.TryParse(pro_unit_price_txt.Text, ProUnitPrice) Then
                MsgBox("Invalid Unit Price. Please enter in a valid unit price.", MsgBoxStyle.Exclamation, "Invalid Entry")
                Exit Sub
            End If
            If Not Decimal.TryParse(pro_pieces_txt.Text, ProPieces) Then
                MsgBox("Invalid Pro Piece value. Please enter in a valid pro piece.", MsgBoxStyle.Exclamation, "Invalid Entry")
                Exit Sub
            End If
            ' Since grand_total_amt.Text looks like a previous calculated value and not a user-entered value, use Parse to do the conversion
            GrandTotal = Decimal.Parse(grand_total_amt.Text) 
    
            ' Perform the calculations
            TotalAmount = ProUnitPrice * ProPieces
            GrandTotal += TotalAmount
            ' The += operator uses the previous value, adds the new value to it (where the new value is after the operator), and stores the sum back into that variable
    
            ' Display the calculated results in the correct TextBoxes
            total_amt.Text = TotalAmount.ToString("N2") ' The "N2" String format will display the number with 2 decimal places
            grand_total_amt.Text = GrandTotal.ToString("N2")
        End Sub
    Last edited by Pyth007; Feb 26th, 2015 at 03:23 PM. Reason: Fixed typos in code

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: The fourth textbox value not display when multiply by 10 onward

    Don't use Val(), not to mention all your work is done as strings and numbers are integers which will make life harder for you.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: The fourth textbox value not display when multiply by 10 onward

    Both good points. Val() often does what you expect, but it has very specific behavior that is not always what you expect. For example:

    Val("1,444") = 1

    The reason is that Val parses the characters in the string from left to right and gives up on the first character it sees as non-numeric. In that example, it parses the 1, then gives up on the comma. So, if you have anything else in those strings, Val could be the whole problem.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Posts
    94

    Lightbulb Re: The fourth textbox value not display when multiply by 10 onward

    Quote Originally Posted by Pyth007 View Post
    I'm not exactly sure what is wrong other than "the value is not display properly" (ie what did you input, what did you expect to be the output, and what is the actual output), but I'm tempted to guess that part of the problem might be that you are not using the correct DataTypes. I'd suggest you turn Option Strict On to help with providing the correct DataTypes for your variables. For example, you have "Dim temp1 As String" thereby making this variable a String. You then have "temp1 = Val(tempo) + temp" which I'm assuming means you want to add tempo and temp together. However the + operator for Strings is for concatenation, not addition. Thus what gets stored in temp1 might be a concatenation of tempo and temp instead of adding the 2 values together. Rewriting your code using proper DataTypes would look more like:
    Code:
        Private Sub pro_unit_price_txt_TextChanged(sender As Object, e As EventArgs) Handles pro_unit_price_txt.TextChanged
            ' Since this TextBox is named pro_unit_price, it looks like this is for calculating financial data, so the best DataType for money is Decimals
            ' Also, add some more variables to convert the other TextBox values into decimals as well as to hold the calculated results
            Dim ProUnitPrice As Decimal
            Dim ProPieces As Decimal
            Dim TotalAmount As Decimal
            Dim GrandTotal As Decimal
            
            ' Use TryParse to convert the TextBox values into the correct DataType instead of using the outdated Val()
            If Not Decimal.TryParse(pro_unit_price_txt.Text, ProUnitPrice) Then
                MsgBox("Invalid Unit Price. Please enter in a valid unit price.", MsgBoxStyle.Exclamation, "Invalid Entry")
                Exit Sub
            End If
            If Not Decimal.TryParse(pro_pieces_txt.Text, ProPieces) Then
                MsgBox("Invalid Pro Piece value. Please enter in a valid pro piece.", MsgBoxStyle.Exclamation, "Invalid Entry")
                Exit Sub
            End If
            ' Since grand_total_amt.Text looks like a previous calculated value and not a user-entered value, use Parse to do the conversion
            GrandTotal = Decimal.Parse(grand_total_amt.Text) 
    
            ' Perform the calculations
            TotalAmount = ProUnitPrice * ProPieces
            GrandTotal += TotalAmount
            ' The += operator uses the previous value, adds the new value to it (where the new value is after the operator), and stores the sum back into that variable
    
            ' Display the calculated results in the correct TextBoxes
            total_amt.Text = TotalAmount.ToString("N2") ' The "N2" String format will display the number with 2 decimal places
            grand_total_amt.Text = GrandTotal.ToString("N2")
        End Sub
    Dear Pyth007 ,
    I have tried with your code the grand total still not adding with the totalamt properly as aspected. The ProPiece i change it to Interger Prase since its is interger number. When ProUnitPrice * ProPieces the total value appear correct, here the Grandtotal value at present is populate from database ,but when Sum the Grandtotal with the TotalAmt the amount is in the Grandtotal is not change to the correct number.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: The fourth textbox value not display when multiply by 10 onward

    The next thing you need to do is a bit of debugging. The code looks reasonable, so put a breakpoint somewhere early on, such as the first If statement, and step through the code watching what the variables are doing. If you haven't learned breakpoints and code stepping, ask about that, because nothing is more useful than that, and little is easier, too.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width