Jump to content

VB 2008 - Add numbers in textbox


Recommended Posts

very new with the 2008 vb lol... but lets say i have textbox1 textbox2 and textbox3.

I type in 2 into textbox1 and 13 into textbox2, and i want it to add them together and post them in textbox3 as 15. How do you do this? Im getting alot of mixed results on google lol

Link to comment
Share on other sites


You will need 3 texboxes and a button for this code to work.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Add Numbers" Then
Button1.Text = "Clear Numbers"
If TextBox1.Text = "" And TextBox2.Text = "" Then
MsgBox("Please Fill In TextBox1 And TextBox2")
Else
Try
Dim A As Integer = TextBox1.Text / 1
Dim B As Integer = TextBox2.Text / 1
TextBox3.Text = A + B
Catch ex As Exception
MsgBox("There Must Be Only Numbers In TextBox1 And TextBox2")
End Try
End If
Else
Button1.Text = "Add Numbers"
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
End Sub

End Class

Link to comment
Share on other sites

sorry took a bit of time to get back. what i did was make a timer so that it calculated everything without a button but here is what i did.

Public Class Form1


Dim intNum1 As Integer
Dim intNum2 As Integer
Dim intNum3 As Integer

and so on... and so on...

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)



If TextBox1.Text = "" Then TextBox1.Text = "0"
If TextBox2.Text = "" Then TextBox2.Text = "0"
If TextBox3.Text = "" Then TextBox3.Text = "0"
If TextBox4.Text = "" Then TextBox4.Text = "0"

and so on... and so on...


intNum7 = TextBox1.Text
intNum8 = TextBox2.Text
intNum9 = TextBox3.Text
intNum10 = TextBox4.Text


and so on.... and so on...

(reason for that is if you deleted the number 0 out of the textbox before you typed in it the app would crash )

TextBox33.Text = intNum1 + intNum2 + intNum3 + intNum4 + intNum5 + intNum6 + intnum25 + intnum26 + intnum27 + intnum28 + intnum29 + intnum30
TextBox34.Text = intNum7 + intNum8 + intNum9 + intNum10 + intNum11 + intNum12 + intNum13 + intNum14 + intNum15 + intNum16 + intNum17 + intNum18 + intNum19 + intNum20 + intNum21 + intNum22 + intNum23 + intNum24

and that last code was a part of the math that i needed in the app so just ignore that, that is only for ref to someone that needs help in how it works . hope someone eventually finds this helpful

r3incarnat0r and gunsmokingman thank you both for your help on this issue :D !

Link to comment
Share on other sites

TextBox33.Text = intNum1 + intNum2 + intNum3 + intNum4 + intNum5 + intNum6 + intnum25 + intnum26 + intnum27 + intnum28 + intnum29 + intnum30
TextBox34.Text = intNum7 + intNum8 + intNum9 + intNum10 + intNum11 + intNum12 + intNum13 + intNum14 + intNum15 + intNum16 + intNum17 + intNum18 + intNum19 + intNum20 + intNum21 + intNum22 + intNum23 + intNum24

If you need so many variables, you should rather consider to use an array of them imo:

Dim tmp(2) As Integer  'array of 3 integers - tmp(0), tmp(1) and tmp(2)

Dim ints() As Integer = {2, 4, 0, 5, -1} 'array of 5 integers
Dim tmpint As Integer = 0

For i = 0 To ints.Length - 1 'i.e. ints(0) to ints(4)
tmpint += ints(i) 'adds all values
Next 'tmpint will be 2 + 4 + 0 + 5 - 1 = 10

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...