Easy: Windows Form- Add two or more fileds


This is a very easy one, I have done it on MS Access but can't do it in VB.NET.... (it has no database, only windows form)

On a windows form I have two or more text fields (txt1, txt2, txt3 ....etc) and I have another text field call (txtAnswer).

I need to add the values entered on the text fields to the "txtAnswer" field, but I want the sum as I keep on entering value to each field.

 I also need to be able to only enter numbers on the text field.

Any help?
Who is Participating?
karthikeyanTPConnect With a Mentor Commented:
Hai nibirkhan,

Here is the code.

'declare total to sum up. I am assuming total as double

Private total As Double

'there are three text boxes named TextBox1, TextBox2 and TextBox3.
'this function is invoked whenever text user changes a value in TextBox 1 and 2. TextBox3 finds the
'total and display it

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        Dim a, b As Double
        If TextBox2.Text = "" Then
            a = 0
            a = Convert.ToDouble(TextBox2.Text)
        End If
        If TextBox1.Text = "" Then
            b = 0
            b = Convert.ToDouble(TextBox1.Text)
        End If
        TextBox3.Text = Convert.ToDouble(a + b)
    End Sub

    'This function restricts user to type digits and decimal point.

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
        'Restrict user to key numeric values, decimal point and control keys
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) _
                And Not e.KeyChar = "." Then
            e.Handled = True
            ' allow only one decimal point in textbox
            If e.KeyChar = "." And CType(sender, Control).Text.IndexOf(".") <> -1 Then
                e.Handled = True
            End If
        End If
    End Sub

nibirkhanAuthor Commented:
hi Kar,

thanks for the code.....

Enjoy the points...

All Courses

From novice to tech pro — start learning today.