calculation

I have three textfield, which is textfield1, textfield2, textfield3

I wish to multiply textfield1 with textfield2 and put the result in textfield3. User can enter decimal number in textfield2 but cannot in textfield1. result in textfiled3 cannot receive decimal place....in web based we can use onkeydown method..how about vb.net?

code in vb.net..thanks....
lovelyana24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tsayCommented:
I don't understand. If the user can enter a decimal in textfield2, then how can textfield3 NOT show decimals? you want to round it?
e.g: textfield1 = 10, textfield2 = 1.5, textfield 3 = 11.5 or 12?
What exactly do you want? The code to multiply? Or the code to check for decimals?
lovelyana24Author Commented:
the code to multiply...ya..round the results in textfield3
tsayCommented:
Here's the code:
(textfield1 = tf1, textfield2 = tf2 and textfield3 = tf3)

----------CODE----------
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then 'you can also check if they are digits, but that wasn't the Q
            Dim decResult As Decimal
            decResult = Decimal.Parse(TextBox1.Text) + Decimal.Parse(TextBox2.Text)
            decResult = Math.Round(decResult)
            TextBox3.Text = decResult.ToString
        End If
    End Sub
----------CODE----------
If you don't want to use a button, place the code in the OnValidate Event of textbox2.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

lovelyana24Author Commented:
Dear Tsay..

I dun want to use button..how if a user enter value in textfield2 first and then textfield1...i want the user to be able to enter any textfield first and still can get value in textfield3
tsayCommented:
   Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            Dim decResult As Decimal
            decResult = Decimal.Parse(TextBox1.Text) + Decimal.Parse(TextBox2.Text)
            decResult = Math.Round(decResult)
            TextBox3.Text = decResult.ToString
        End If
    End Sub

    Private Sub TextBox2_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Validated
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            Dim decResult As Decimal
            decResult = Decimal.Parse(TextBox1.Text) + Decimal.Parse(TextBox2.Text)
            decResult = Math.Round(decResult)
            TextBox3.Text = decResult.ToString
        End If
    End Sub

Make sure the textboxes are empty when form loads.
Bob LearnedCommented:
There are the TextBox.KeyDown and TextBox.KeyPress events when a key is pressed.

Bob
lovelyana24Author Commented:
Dear Tsay,

In your code is correct..but u forget to use *(u use +), i need to multiply..
in your code, how to avoid user from entering decimal values?i want to disable user from entering decimal....
lovelyana24Author Commented:
Dear Tsay,

Your code....

Private Sub txtB1a_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtB1a.Validated
        If txtB1a.Text <> "" And txtB1b.Text <> "" Then
            Dim decResult As Decimal
            decResult = Decimal.Parse(txtB1a.Text) * Decimal.Parse(txtB1b.Text)
            decResult = Math.Round(decResult)
            txtB1c.Text = decResult.ToString
        End If
    End Sub

    Private Sub txtB1b_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtB1b.Validated
        If txtB1a.Text <> "" And txtB1b.Text <> "" Then
            Dim decResult As Decimal
            decResult = Decimal.Parse(txtB1a.Text) * Decimal.Parse(txtB1b.Text)
            decResult = Math.Round(decResult)
            txtB1c.Text = decResult.ToString
        End If
    End Sub
lovelyana24Author Commented:
How to avoid user from entering char values, i only accept numbers and no decimals....
tsayCommented:
You should add points if you keep asking.... Change code as below to check for char values:

    Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
                Dim decResult As Decimal
                decResult = Decimal.Parse(TextBox1.Text) + Decimal.Parse(TextBox2.Text)
                decResult = Math.Round(decResult)
                TextBox3.Text = decResult.ToString
            Else
                MsgBox("Plz enter numeric values only!")
                If Not IsNumeric(TextBox1.Text) Then
                    TextBox1.Text = ""
                    TextBox1.Focus()
                End If
                If Not IsNumeric(TextBox2.Text) Then
                    TextBox2.Text = ""
                    TextBox2.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub TextBox2_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Validated
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
                Dim decResult As Decimal
                decResult = Decimal.Parse(TextBox1.Text) + Decimal.Parse(TextBox2.Text)
                decResult = Math.Round(decResult)
                TextBox3.Text = decResult.ToString
            Else
                MsgBox("Plz enter numeric values only!")
                If Not IsNumeric(TextBox1.Text) Then
                    TextBox1.Text = ""
                    TextBox1.Focus()
                End If
                If Not IsNumeric(TextBox2.Text) Then
                    TextBox2.Text = ""
                    TextBox2.Focus()
                End If
            End If
        End If
    End Sub

In your first question you said the user entered decimals, so post a new question for the decimal check and close this one.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.