A step-by-step guide to add two numbers in Visual Basic.NET with examples

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

  1. 1
    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).
  2. 2
    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
  3. 3
    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.
  4. 4
    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.
  5. 5
    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.
  6. 6
    End the button-click section. Type in End Sub and press Enter.
  7. 7
    Create a new section. Type in Private Sub Form1_Load(sender As Object, e as EventArgs) Handles MyBase.Load and press Enter.
  8. 8
    Add a false label tag. Type in Label4.Visible = False and press Enter, then type in End Sub and press Enter.
  9. 9
    Create the final section. Type in Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click and press Enter.
  10. 10
    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.
  11. 11
    Create the "addition" command. Type in Sum = Val(TextBox1.Text) + Val(TextBox2.Text) and press Enter.
  12. 12
    Add the "sum" command. Type in TextBox3.Text = Sum and press Enter.
  13. 13
    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.
  14. 14
    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".
  15. Advertisement

Community Q&A

  • Question
    How can I make a simple, basic program for simple number addition?
    Community Answer
    Community Answer
    Use this code: #include void main() {int a,b; couta; coutb; cout
  • Question
    What should I program to add two numbers?
    Community Answer
    Community Answer
    Use this code: #include void main() {int a,b; couta; coutb; cout
  • Question
    How can I get the program with the code to add two numbers?
    Winters
    Winters
    Community Answer
    If 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

Jack Lloyd
Written by:
wikiHow Technology Writer
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.
How helpful is this?
Co-authors: 21
Updated: December 10, 2022
Views: 391,756
Categories: Programming
Advertisement