Hello...

I have three textboxes on a form.

The first two are for entering numbers for addition.
And the third is for displaying the result of addition.

I want the result of addition to appear in the third textbox as soon as I enter
the numbers in the first two textboxes, without pressing any buttons.

Does anyone know how to do this?

Member Avatar for Unhnd_Exception
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged

        If String.IsNullOrEmpty(TextBox1.Text) OrElse String.IsNullOrEmpty(TextBox2.Text) Then Exit Sub
        If Not IsNumeric(TextBox1.Text) OrElse Not IsNumeric(TextBox2.Text) Then Exit Sub

        TextBox3.Text = CDbl(TextBox1.Text) + CDbl(TextBox2.Text)

    End Sub

The above code calculates the result, but the result doesn't
appear instantly in the third textbox.

It only appears when I click the third textbox and press any key on the keyboard.

Is there a code that can make the result appear instantly in the third textbox,
as soon as you fill in the first two textboxes?

Maybe some code for simulating a key press or a button click?

Member Avatar for Unhnd_Exception

The above code calculates the result and shows it instantly in the third textbox.

Copy it and paste it again and name your textboxes Textbox1,Textbox2, and Textbox3.

Type in Textbox1 and Textbox2. Textbox3 will show the result instantly.

Sorry, I rushed.

The code works perfectly!

Thanks.

i know this post has been resolved but where should i paste the code? in texbox1? textbox2? or textbox3? thanx! for helping me

Just add this to your program:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
    If String.IsNullOrEmpty(TextBox1.Text) OrElse String.IsNullOrEmpty(TextBox2.Text) Then Exit Sub
    If Not IsNumeric(TextBox1.Text) OrElse Not IsNumeric(TextBox2.Text) Then Exit Sub
    TextBox3.Text = CDbl(TextBox1.Text) + CDbl(TextBox2.Text)
End Sub

Because this is handling both textbox1 and textbox2

This code works for me but when i delete both value of the textbox the total is still there... any solution for my problem?

What about if it is more that 2 variables?

commented: It has really worked for me while dealing with 12 Textboxes and it has been all a success. +0

BY a simple TextChange event you could do it

Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        Dim x As Double = 0
        For Each actTxtBox As TextBox In Me.Controls
            If (actTxtBox.Name = "TextBox1") Or (actTxtBox.Name = "TextBox2") Then
                x += Val(actTxtBox.Text)
            End If
        Next

        IIf(x > 0, TextBox3.Text = x, TextBox3.Text = "")
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.