A step-by-step guide to add two numbers in Visual Basic.NET with examples
X
This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher.
This article has been viewed 391,756 times.
Learn more...
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
-
1Open 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.
-
2Add 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 -
3Set 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.
-
4Create 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.
-
5Create 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.
-
6End the button-click section. Type in End Sub and press ↵ Enter.
-
7Create a new section. Type in Private Sub Form1_Load(sender As Object, e as EventArgs) Handles MyBase.Load and press ↵ Enter.
-
8Add a false label tag. Type in Label4.Visible = False and press ↵ Enter, then type in End Sub and press ↵ Enter.
-
9Create the final section. Type in Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click and press ↵ Enter.
-
10Add 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.
-
11Create the "addition" command. Type in Sum = Val(TextBox1.Text) + Val(TextBox2.Text) and press ↵ Enter.
-
12Add the "sum" command. Type in TextBox3.Text = Sum and press ↵ Enter.
-
13Close the code. Type in End Sub and press ↵ Enter to close the final section, then type in End Class to close the whole program.
-
14Debug 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
-
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
Advertisement
Warnings
- Visual Basic isn't case-sensitive, but try to capitalize where indicated in the code listed here.⧼thumbs_response⧽
Advertisement
About This Article
Advertisement