How to add two textbox values in vb net

Supposing, you have three text boxes which you want to sum the values in Textbox1 and Textbox2, and calculate the result automatically into Textbox3 as following screenshot shown. How could you deal with this job in Excel?

Sum up values from textboxes with VBA code

Sum up values from textboxes with VBA code

To sum up values from two textboxes and get the result into the third textbox, the following VBA code may help you.

1. First, you should insert three textboxes, click Developer > Insert > Text Box (ActiveX Control), and then draw a textbox, and make two copies, see screenshot:

2. After inserting the textboxes, right click the sheet tab which contains the textboxes you want to use, and click View Code from the context menu to open the Microsoft Visual Basic for Applications window, and then copy and paste the following VBA code into the blank module:

VBA code: Sum up values from textboxes:

Private Sub Sum_Click() TextBox3.Text = Val(TextBox2.Text) + Val(TextBox1.Text) End Sub

3. Then type the values into Textbox1 and Textbox2, and then press F5 key or Run button to execute the code, the calculated result is displayed into Textbox3 immediately, see screenshot:

No ratings yet. Be the first to rate!

George, first of all you get the value in the textbox with this syntex if textBox1 is the name of your textbox control. Secondly, if you would write somthing like

Code:

textBox1.Text + textBox2.Text

you will not get the sum but the concatination of the two values stored in the two text boxes, cause everthing in text boxes is treated as a string

in VB .NET. You will acutally have to do something like

Code:

Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox1.Text)

to get the sum.

Simple thing it is, no doubt, but it'll take u some time to get accustumed VB .NET's ways.


But you will be doing fine, I'm sure, 'cause VB .NET is a very friendly language otherwise. Regards

Ankur Verma


Please read and help...

Hi

I have 8 tabs in windows form. I have a text box (test1) in tab5 and textbox (test2) in tab8. Both accepts only numbers.

If the total value of the two box is greater than 99, then the messge box display and focus on one of the control box. it works fine only in the following condition:

1. Run the windows form and click the first textbox in tab5 and then click on the textbox on tab8 - works fine if the total reaches 99.It fails and crash under the following condition. 

1. Run the windows form and click directly the textbox in tab8 - it crash when I leave the control.

The code I am using is given below.

Function PurchNoAddition() As BooleanIf (Test2.Text) + +(Test2.Text) > 99 Then
MsgBox(Test1.Text+ +Test2.Text)
Test2.Focus
End If
End Function

Private Sub PurchNo_leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Test2.LeavePurchNoAddition()End Sub

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

The proper way to do this would be to validate the two inputs and only perform the sum when the user has completed entering both valid inputs, e.g.

Private Sub TextBoxes_Validating(sender As Object, e As ComponentModel.CancelEventArgs) Handles TextBox2.Validating, TextBox1.Validating Dim source = DirectCast(sender, TextBox) If source.TextLength > 0 AndAlso Not Integer.TryParse(source.Text, Nothing) Then source.SelectAll() source.HideSelection = False MessageBox.Show("Please enter a valid integer") source.HideSelection = True 'Don't let the control lose focus with invalid contents. e.Cancel = True End If End Sub Private Sub TextBoxes_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated, TextBox1.Validated If TextBox1.TextLength > 0 AndAlso TextBox2.TextLength > 0 Then Label1.Text = (CInt(TextBox1.Text) + CInt(TextBox2.Text)).ToString() End If End Sub

Note that the arithmetic will not happen as the user types but rather when they leave a control. There's no need to click a Button but focus must leave the TextBoxes. You could do both the validation and arithmetic as the input changes if you really wanted to, but I don't really see the point in showing the user results that they have no interest in.

Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged, TextBox1.TextChanged Dim input1 As Integer Dim input2 As Integer If TextBox1.TextLength > 0 AndAlso TextBox2.TextLength > 0 AndAlso Integer.TryParse(TextBox1.Text, input1) AndAlso Integer.TryParse(TextBox2.Text, input2) Then Label1.Text = (input1 + input2).ToString() Else Label1.ResetText() End If End Sub

Minimalist 96 Posting Pro

3 Years Ago

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

gloria_4 0 Newbie Poster

3 Years Ago

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

Minimalist 96 Posting Pro

3 Years Ago

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

gloria_4 0 Newbie Poster

3 Years Ago

@minimalist

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

Last Seen 3 Hours Ago

Reverend Jim 4,175 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

3 Years Ago

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.

mukiibi 0 Newbie Poster

3 Years Ago

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

Be a part of the DaniWeb community

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

Neuester Beitrag

Stichworte