PDF download Download Article
A step-by-step guide to add two numbers in Visual Basic.NET with examples
PDF download 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.

  1. If you want to test your program later, make sure that you have a program which supports debugging (e.g., Visual Basic 2017).
  2. 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. 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. 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. 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. Type in End Sub and press Enter .
  7. Type in Private Sub Form1_Load(sender As Object, e as EventArgs) Handles MyBase.Load and press Enter .
  8. Type in Label4.Visible = False and press Enter , then type in End Sub and press Enter .
  9. Type in Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click and press Enter .
  10. 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. Type in Sum = Val(TextBox1.Text) + Val(TextBox2.Text) and press Enter .
  12. Type in TextBox3.Text = Sum and press Enter .
  13. Type in End Sub and press Enter to close the final section, then type in End Class to close the whole program.
  14. 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

Search
Add New Question
  • Question
    How can I make a simple, basic program for simple number addition?
    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
    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
    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
Ask a Question
      Advertisement

      Tips

      • Visual Studio 2017 is free to download from Microsoft's website.
      • 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.
      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.
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 421,857 times.

      Is this article up to date?

      Advertisement