Download Article
A step-by-step guide to add two numbers in Visual Basic.NET with examples
Download Article
This wikiHow teaches you how to create a simple Visual Basic program that allows you to find the sum of two numbers. In order to run your program, you will need a Visual Basic compiler such as Visual Studio 2017.
Steps
-
Open your preferred Visual Basic editor. If you want to test your program later, make sure that you have a program which supports debugging (e.g., Visual Basic 2017).
- If you don't have a Visual Basic editor, you can use Notepad ++ or download the official Visual Basic 2017 program .
-
Add the initial line. Type Private Class Form1 into the Visual Basic editor, then press ↵ Enter . This sets up the rest of your document.
- The "Private Class" tag in Visual Basic is similar to the "<html>" tag in HTML.
Advertisement -
Set up your document to recognize variables. Since you'll be adding two integers to each other to find the sum, you'll need to prompt Visual Basic to recognize numbers as variables. To do so:
- Type in Private Sub Button1_Click(sender As Object, e As EventArgs) and press ↵ Enter .
- Type in Handle(Button1_Click) and press ↵ Enter .
- Type in Dim sum As Integer and press ↵ Enter .
- Type in Dim a As Integer and press ↵ Enter .
- Type in Dim b As Integer and press ↵ Enter .
-
Create an exception for the text boxes. This will prompt your program to result in an error if you don't fill in a box. To do so:
- Type in Label4.Visible = True and press ↵ Enter .
- Type in If TextBox1.Text = "" Then and press ↵ Enter .
- Type in Label4.Visible = False and press ↵ Enter .
- Type in MessageBox.Show("Sorry, box cannot be empty.") and press ↵ Enter .
- Type in TextBox1.Focus() and press ↵ Enter .
- Type in End If and press ↵ Enter .
-
Create text boxes for your numbers. This will be the interface that you use to enter your numbers. To do so:
- Type in a = Val(TextBox1.Text) and press ↵ Enter .
- Type in b = Val(TextBox2.Text) and press ↵ Enter .
- Type in sum = (a + b) and press ↵ Enter .
- Type in Label4.Text = "The sum of" & a & " and " & b & " is " & sum & "." and press ↵ Enter .
-
End the button-click section. Type in End Sub and press ↵ Enter .
-
Create a new section. Type in Private Sub Form1_Load(sender As Object, e as EventArgs) Handles MyBase.Load and press ↵ Enter .
-
Add a false label tag. Type in Label4.Visible = False and press ↵ Enter , then type in End Sub and press ↵ Enter .
-
Create the final section. Type in Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click and press ↵ Enter .
-
Add references to the text boxes. This will allow you to add numbers in your completed program. To do so:
- Type in TextBox1.Text = "" and press ↵ Enter .
- Type in TextBox2.Text = "" and press ↵ Enter .
- Type in Label4.Text = "" and press ↵ Enter .
- Type in TextBox1.Focus() and press ↵ Enter .
-
Create the "addition" command. Type in Sum = Val(TextBox1.Text) + Val(TextBox2.Text) and press ↵ Enter .
-
Add the "sum" command. Type in TextBox3.Text = Sum and press ↵ Enter .
-
Close the code. Type in End Sub and press ↵ Enter to close the final section, then type in End Class to close the whole program.
-
Debug your program. Click the Debug tab, click Start Debugging , and wait for the debugging process to complete. Once your program has been completely debugged, a window with three text boxes and an addition button should open; you can then add a number to the top two boxes and click the button to add the numbers together.
- If you're using a basic text editor to create your Visual Basic code, you won't have a Debug tab. Consider opening your project in Visual Studio 2017 to debug and run the program.
- If you're using Notepad or TextEdit to create your code, make sure to save the final file in ".vb" format rather than ".txt" or ".text".
Advertisement
Community Q&A
Search
-
QuestionHow can I make a simple, basic program for simple number addition?Community AnswerUse this code: #include void main() {int a,b; couta; coutb; cout
-
QuestionWhat should I program to add two numbers?Community AnswerUse this code: #include void main() {int a,b; couta; coutb; cout
-
QuestionHow can I get the program with the code to add two numbers?WintersCommunity AnswerIf it's a compiled language, then first compile and then run the program. For example, in C++ you may write #include int main() { int a,b; std::cin>>a>>b; std::cout
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
- Visual Studio 2017 is free to download from Microsoft's website.Thanks
- When using apps like Notepad or TextEdit, it helps to indent manually specific sections of code so that you can more easily see the different parts of the program.Thanks
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement
Warnings
- Visual Basic isn't case-sensitive, but try to capitalize where indicated in the code listed here.Thanks
Advertisement
About This Article
Thanks to all authors for creating a page that has been read 422,697 times.
Advertisement