Hello,

Can someone help me adding two text box.
example:
textbox1 = (1)
textbox2 =(50)
textboxsum = (51)

i have already values on textbox1 and textbox2, I want the two values of textbox automatically computed on textboxsum.

Note:
textchange event works but i need to manually input the values on textbox in order to get the sum.

thanks in advance

Tis is a solution for vb.net

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim num1 As Double
        Dim num2 As Double
        Dim num3 As Double
        num1 = Convert.ToDouble(TextBox1.Text)
        num2 = Convert.ToDouble(TextBox2.Text)
        num3 = num1 + num2
        TextBox3.Text = CStr(num3)
    End Sub

@minimalist

Thanks for the reply. but the code seems that the sum is generated via button. As i said above, i have already values on 2 textbox which is i want the sum to be auto computed on the sum textbox.

textbox1 and textbox2 have already values that came from my database.

well just use a timer:

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
            Dim num1 As Double
            Dim num2 As Double
            Dim num3 As Double
            num1 = Convert.ToDouble(TextBox1.Text)
            num2 = Convert.ToDouble(TextBox2.Text)
            num3 = num1 + num2
            TextBox3.Text = CStr(num3)
        End If

@minimalist

Thank you for the reply but i already figure it out by creating a module that will add the textboxes. thanks again

Two possible solutions:

  1. Put code in the text_changed event for the two textboxes (you can use the same event handler for both) that computes the sum and puts it in the third textbox.
  2. Because you are getting the values from a database why not just add sum(val1,val2) to the query and write all three values to the textboxes at the same time.

If you really want it real time regardless of which textbox you fill first, then make use of the text changed event i.e

Private Sub textbox1 _TextChanged(sender As System.Object, e As System.EventArgs) Handles textbox1 .TextChanged

    textboxsum .Text = Format(Val(textbox1 .Text.Replace(",", "")) - Val(textbox2 .Text.Replace(",", "")), "0.00")
End Sub

Private Sub textbox2 _TextChanged(sender As System.Object, e As System.EventArgs) Handles textbox2 .TextChanged

   textboxsum .Text = Format(Val(textbox1 .Text.Replace(",", "")) - Val(textbox2 .Text.Replace(",", "")), "0.00")
End Sub  

Personally I have issues with timers
commented: i peaceful, correct interpretation, codification, for vb.net writing +0
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.