# 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....
###### 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.

Commented:
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?
Author Commented:
the code to multiply...ya..round the results in textfield3
Commented:
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.
Author 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
Commented:
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.
Commented:
There are the TextBox.KeyDown and TextBox.KeyPress events when a key is pressed.

Bob
Author 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....
Author Commented:
Dear Tsay,

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
Author Commented:
How to avoid user from entering char values, i only accept numbers and no decimals....
Commented:
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