none
About calc RRS feed

  • Question

  • I need some help here i have two textbox and one label and i want to calculate the sum of the textbox's. The problem is that does not calculate the number if is in this format

    12,23 * 10 where the result is 122,3 and it is show me 122 to the label. The code that i use is

    label1.text = val(textbox1.text) * val(textbox2.text ) 
    

    Any idea how to fix it.

    Thank's in advance.

    Tuesday, January 10, 2012 3:00 PM

Answers

  • Your code "does not calc the whole number" because Val() is used.

    If you really want to calculate each time the content of either TextBox changes, and catch exceptions if inappropriate characters are entered, do this:

    Public Class Form1
        Private Num1, Num2, Sum As Double
    
    
        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim GoodChar1 As Boolean = False
            
            GoodChar1 = Double.TryParse(TextBox1.Text, Num1)
            If Not GoodChar1 Then
                TextBox1.Text = ""
                TextBox1.Focus()
            Else
                Sum = Num1 + Num2
                Label1.Text = Sum.ToString()
            End If
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
            Dim GoodChar2 As Boolean = False
           
            GoodChar2 = Double.TryParse(TextBox2.Text, Num2)
            If Not GoodChar2 Then
                TextBox2.Text = ""
                TextBox2.Focus()
            Else
                Sum = Num1 + Num2
                Label1.Text = Sum.ToString()
            End If
    
        End Sub
    End Class
    
    

    • Marked as answer by antonisa512 Wednesday, January 11, 2012 10:07 PM
    Wednesday, January 11, 2012 1:23 AM

All replies

  • Read here about the Val() function:

    http://msdn.microsoft.com/en-US/library/2cy61f9z(v=VS.80).aspx

    Especially this: "Numeric or Currency data type. VAL( ) returns the numbers in the character expression from left to right until a non-numeric character is encountered."

    There is a better way to convert strings to real numbers in VB. Here's a simple example:

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Declare variables to hold conterted TextBox values
            Dim t1 As Double, t2 As Double, sum As Double
    
            'Convert...
            t1 = Convert.ToDouble(TextBox1.Text)
            t2 = Convert.ToDouble(TextBox2.Text)
            'Do the math
            sum = t1 + t2
            'Convert back to string for display
            Label1.Text = sum.ToString()
    
        End Sub
    

    There are better ways than this simple example that will check for invalid input in the TextBoxes before converting. For more information on VB programming, you should ask in the VB developers forums, here:

    http://social.msdn.microsoft.com/Forums/en-US/category/visualbasic

     

    Tuesday, January 10, 2012 3:48 PM
  • Thank you for your answer pvdg42. I forgot to mention that i use the code that i post inside the textbox so the code is like this

     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    label1.text = val(textbox1.text) * val(textbox2.text ) 
    

    I use this because i want to show the changes directly to the label. I try the code that you give me and its working if i use it on a button but not in the textbox.

     

    Tuesday, January 10, 2012 4:14 PM
  • Have you studied the TextChanged event and when it fires? It fires every time the contents of the TextBox changes! That means the code in that event procedure runs every time the user enters a character or removes a character. I really doubt that is what you want.

    Correct me if I'm wrong, but don't you want the code that calculates and displays the result to run when the user has completed the entries in the two TextBoxes?

    I also pointed out why the Val function is not working for you. Did you read about that?

     

    Tuesday, January 10, 2012 9:28 PM
  • I read everything that you point and realy thank you for that. The truth is that i realy need to "change" the result every time that a user input numbers (or remove) inside the textbox. So far with the code that i posted it is working with no problem. The only problem is that it does not calc the whole number. So i am going to aproach it in a different way. By the way thank you for the link that you give me. It is very helpfull.

    Thank you one more time for your answer and for your time.


    • Edited by antonisa512 Tuesday, January 10, 2012 10:40 PM
    Tuesday, January 10, 2012 10:40 PM
  • Your code "does not calc the whole number" because Val() is used.

    If you really want to calculate each time the content of either TextBox changes, and catch exceptions if inappropriate characters are entered, do this:

    Public Class Form1
        Private Num1, Num2, Sum As Double
    
    
        Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim GoodChar1 As Boolean = False
            
            GoodChar1 = Double.TryParse(TextBox1.Text, Num1)
            If Not GoodChar1 Then
                TextBox1.Text = ""
                TextBox1.Focus()
            Else
                Sum = Num1 + Num2
                Label1.Text = Sum.ToString()
            End If
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
            Dim GoodChar2 As Boolean = False
           
            GoodChar2 = Double.TryParse(TextBox2.Text, Num2)
            If Not GoodChar2 Then
                TextBox2.Text = ""
                TextBox2.Focus()
            Else
                Sum = Num1 + Num2
                Label1.Text = Sum.ToString()
            End If
    
        End Sub
    End Class
    
    

    • Marked as answer by antonisa512 Wednesday, January 11, 2012 10:07 PM
    Wednesday, January 11, 2012 1:23 AM
  • Thank you Pvdg42 that code realy does the work done and the app does not "stuck". But i notice something and i don't know why this is happening. When i press the "." from numeric keypad it is calculate the number as whole for example 12.5 * 1 it show me as a result 125 instead of 12.5 when i use the "," from letter keyboard it is show me the correct result which is 12.5 , Any idea why is this happening?
    Wednesday, January 11, 2012 11:40 AM
  • Your issue must have to do with the locale set up on your system. I use US English here, and the keys in question (".") work correctly here.

    As I am unable to duplicate your problem, I suggest you follow my previous advice and ask about your issue in the Visual Basic Developer forums, here:

    http://social.msdn.microsoft.com/Forums/en-US/category/visualbasic

     

    Wednesday, January 11, 2012 2:16 PM
  • Thank you for your help once more Pvdg42 you help me a lot. And thank you for that and for your time. I will follow your advice and open a new post to correct section(i was confusing and i opened it in wrong section).
    Wednesday, January 11, 2012 10:07 PM
  • There are so many different forums that it's easy to get "lost". You're certainly not alone!

    Good luck in the VB forums. There are many helpful experts there.

    Wednesday, January 11, 2012 10:16 PM