Start New Discussion within our Software Development Community

Hello dear friends this is my first question and i am new to vb.net and programming i am trying to build the simple xbase calculation like if i had 2 eggs value 2 dolars so 3 eags value calculated by program i did this code but the answer never show up in the textbox 4

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As Integer
a = TextBox1.Text
Dim b As Integer
b = TextBox2.Text
Dim c As Integer
c = TextBox3.Text
Dim d As Integer
d = TextBox4.Text
TextBox4.Text = b * c / a


End Sub

End Class

i want textbox automatically show result when i put the values.

thanks

1st of all you are using the Form_Load event to perform the calculations. This means that your program runs when your form is loaded.
At the time your form is loaded, textbox 1-3 don't contain any text, so the result should be 0.
To do the calculation automatically after the values on the textboxes have changed you need either a sub that will handle the TextChanged event for all 3 textboxes or a sub that will be called by all 3 TextChanged events.

To go with a sub that will handle all 3 events you need to do as following:

Private Sub Calc () Handles TextBox1.TextChanged,TextBox2.TextChanged, TextBox3.TextChanged 
Dim a As Integer
a = TextBox1.Text
Dim b As Integer
b = TextBox2.Text
Dim c As Integer
c = TextBox3.Text
Dim d As Integer
d = TextBox4.Text
TextBox4.Text = b * c / a 

End Sub

Unfortunately your method has a few other problems.
First is that you are not verifying that your textboxes contain numbers.
Second is that you don't take into account that some results (or inputs) can contain decimals.
3rd is that you don't need var d (not you need to assign it the text of textbox4) if you are assigning the result to TextBox4 directly.

This article has been dead for over six months. Start a new discussion instead.