How to add two numbers in Visual Basic.NET

Author: Virginia Floyd
Date Of Creation: 5 August 2021
Update Date: 1 July 2024
Anonim
Adding two numbers in VB.NET .
Video: Adding two numbers in VB.NET .

Content

This article will show you how to write a simple Visual Basic program to calculate the sum of two numbers. You need a Visual Basic compiler such as Visual Studio 2017 to run the program.

Steps

  1. 1 Open the Visual Basic Editor. To test your code later, make sure you have a program in which you can debug your code (for example, Visual Basic 2017).
    • If you don't have a Visual Basic editor, use Notepad ++ or download Visual Basic 2017.
  2. 2 Enter the first line of code. Enter Private Class Form1 in the Visual Basic Editor and then click ↵ Enter... This line defines the following lines of code.
    • The "Private Class" tag in Visual Basic is the same as the html> tag in HTML.
  3. 3 Set the variables. To find the sum, you need to add two integers, so you need to make Visual Basic recognize the numbers as variables. For this:
    • Enter Private Sub Button1_Click (sender As Object, e As EventArgs) and press ↵ Enter.
    • Enter Handle (Button1_Click) and press ↵ Enter.
    • Enter Dim sum As Integer and press ↵ Enter.
    • Enter Dim a As Integer and press ↵ Enter.
    • Enter Dim b As Integer and press ↵ Enter.
  4. 4 Set exceptions for empty fields. In this case, the program will generate an error if some number is not entered. For this:
    • Enter Label4.Visible = True and press ↵ Enter.
    • Enter If TextBox1.Text = "" Then and press ↵ Enter.
    • Enter Label4.Visible = False and press ↵ Enter.
    • Enter MessageBox.Show ("Sorry, the field cannot be empty.") and press ↵ Enter.
    • Enter TextBox1.Focus () and press ↵ Enter.
    • Enter End If and press ↵ Enter.
  5. 5 Create text boxes to enter numbers. For this:
    • Enter a = Val (TextBox1.Text) and press ↵ Enter.
    • Enter b = Val (TextBox2.Text) and press ↵ Enter.
    • Enter sum = (a + b) and press ↵ Enter.
    • Enter Label4.Text = "The sum of" & a & "and" & b & "is equal to" & sum & "." and press ↵ Enter.
  6. 6 Close the button press section. Enter End Sub and press ↵ Enter.
  7. 7 Create a new section. Enter Private Sub Form1_Load (sender As Object, e as EventArgs) Handles MyBase.Load and press ↵ Enter.
  8. 8 Enter the tags "false" and "label". Enter Label4.Visible = False, click ↵ Enter, then enter End Sub and press ↵ Enter .
  9. 9 Create the last section. Enter Private Sub Button2_Click (sender As Object, e As EventArgs) Handles Button2.Click and press ↵ Enter.
  10. 10 Add links to text boxes. This will add numbers to the finished program. For this:
    • Enter TextBox1.Text = "" and press ↵ Enter.
    • Enter TextBox2.Text = "" and press ↵ Enter.
    • Enter Label4.Text = "" and press ↵ Enter.
    • Enter TextBox1.Focus () and press ↵ Enter.
  11. 11 Create a command to add numbers. Enter Sum = Val (TextBox1.Text) + Val (TextBox2.Text) and press ↵ Enter.
  12. 12 Enter the "sum" command. Enter TextBox3.Text = Sum and press ↵ Enter.
  13. 13 Close the code. Enter End Sub and press ↵ Enterto close the last section, then type End Classto close the entire program.
  14. 14 Debug the program. Go to the Debug tab, click Start Debugging and wait for debugging to complete. Once the program is fully debugged, a window will open with three text fields and a button; now enter numbers in the top two boxes and click the button to add the numbers.
    • If the code was written in a simple text editor, there will be no Debug tab.In this case, open the code you wrote in Visual Studio 2017 to debug and run it.
    • If you wrote your code in Notepad or TextEdit, save the file in ".vb" format, not ".txt" or ".text".

Tips

  • Visual Studio 2017 can be downloaded for free from Microsoft.
  • In Notepad or TextEdit, different sections of code can be marked with indentation to make it easier to navigate the code.

Warnings

  • Visual Basic is not case sensitive, but try capitalizing where indicated in the code here.