How to Add Two Numbers in Visual Basic.NET: 9 Steps

Table of contents:

How to Add Two Numbers in Visual Basic.NET: 9 Steps
How to Add Two Numbers in Visual Basic.NET: 9 Steps
Anonim

This article explains how to create a simple program in Visual Basic that allows you to calculate the sum of two numbers entered by the user. In order to run the program, you need to have a Visual Basic compiler such as Visual Studio 2017.

Steps

Add Two Numbers in Visual Basic. NET Step 1
Add Two Numbers in Visual Basic. NET Step 1

Step 1. Start the Visual Basic editor you usually use

If after creation you need to test the operation of your program, make sure you have an editor with a debugger (for example Visual Basic 2017).

If you don't have a Visual Basic editor, you can use Notepad ++ to create the code or you can download Visual Basic 2017 for free

Add Two Numbers in Visual Basic. NET Step 2
Add Two Numbers in Visual Basic. NET Step 2

Step 2. Start creating the code

Enter the following text Private Class Form1 inside the Visual Basic editor you have chosen to use, then press the Enter key. This is the initial declaration of the program.

The purpose of the Visual Basic "Private Class" code is similar to that of the "" tag in HTML

Add Two Numbers in Visual Basic. NET Step 3
Add Two Numbers in Visual Basic. NET Step 3

Step 3. Insert the part relating to the declaration of the variables that will be used within the program

Since you will have to add two integers, you will have to make sure that the program can store them inside two variables. Follow these instructions:

  • Type the code Private Sub Button1_Click (sender As Object, and As EventArgs) and press the Enter key;
  • Type the code Handle (Button1_Click) and press the Enter key;
  • Enter the code Dim Somma As Integer and press the Enter key;
  • Type the code Dim a As Integer and press the Enter key;
  • Type the code Dim b As Integer and press the Enter key.
Add Two Numbers in Visual Basic. NET Step 4
Add Two Numbers in Visual Basic. NET Step 4

Step 4. Create the code that will handle the exception related to the text fields in which the values to be added will be inserted

This will indicate to the program that it should display an error message if no number is entered into the text fields. Follow these instructions:

  • Type the code Label4. Visible = True and press the Enter key;
  • Type the code If TextBox1. Text = "" Then and press the Enter key;
  • Type the code Label4. Visible = False and press the Enter key;
  • Type the code MessageBox. Show ("Error: text fields cannot be empty.") And press the Enter key;
  • Type the code TextBox1. Focus () and press the Enter key;
  • Type the code End If and press the Enter key.
Add Two Numbers in Visual Basic. NET Step 5
Add Two Numbers in Visual Basic. NET Step 5

Step 5. Create the text fields in which to enter the values to be added

This is the user interface that must be used to enter the two numbers to add. Follow these instructions:

  • Type the code a = Val (TextBox1. Text) and press the Enter key;
  • Type the code b = Val (TextBox2. Text) and press the Enter key;
  • Type the code Sum = (a + b) and press the Enter key;
  • Enter the code Label4. Text = "The sum of the values" & a & "and" & b & "is equal to" & Sum & "." and press the Enter key.
Add Two Numbers in Visual Basic. NET Step 6
Add Two Numbers in Visual Basic. NET Step 6

Step 6. Complete the code routine that will handle the event triggered by the mouse click on the "Button1" element of the program interface

Type the End Sub code and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 7
Add Two Numbers in Visual Basic. NET Step 7

Step 7. Create a new program section

Type the command Private Sub Form1_Load (sender As Object, e as EventArgs) Handles MyBase. Load and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 8
Add Two Numbers in Visual Basic. NET Step 8

Step 8. Hide the text label containing the error message

Type the code Label4. Visible = False and press the Enter key, then type the following code End Sub and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 9
Add Two Numbers in Visual Basic. NET Step 9

Step 9. Create the final part of the program

Type the code Private Sub Button2_Click (sender As Object, and As EventArgs) Handles Button2. Click and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 10
Add Two Numbers in Visual Basic. NET Step 10

Step 10. Add the code needed to initialize the interface controls (labels and text fields)

In this way the program will be ready to correctly execute the sum of the values that will be entered by the user. Follow these instructions:

  • Type the code TextBox1. Text = "" and press the Enter key;
  • Type the code TextBox2. Text = "" and press the Enter key;
  • Type the code Label4. Text = "" and press the Enter key;
  • Type the code TextBox1. Focus () and press the Enter key.
Add Two Numbers in Visual Basic. NET Step 11
Add Two Numbers in Visual Basic. NET Step 11

Step 11. Create the code that will do the sum of the entered values

Type the text Sum = Val (TextBox1. Text) + Val (TextBox2. Text) and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 12
Add Two Numbers in Visual Basic. NET Step 12

Step 12. Create the code that will display the sum result on the screen

Type the text TextBox3. Text = Sum and press the Enter key.

Add Two Numbers in Visual Basic. NET Step 13
Add Two Numbers in Visual Basic. NET Step 13

Step 13. Complete the program

Type the End Sub code and press the Enter key to tell the Visual Basic compiler that the procedure is complete, then enter the End Class code to indicate that the program has ended.

Add Two Numbers in Visual Basic. NET Step 14
Add Two Numbers in Visual Basic. NET Step 14

Step 14. Debug the code

Click on the menu Debug, then click the option Start Debugging and wait for the debugging process to finish. If the program passes this check step, a window with three text fields and a button will be displayed. Enter the values to be added in the first two text fields, then click on the button to perform the sum.

  • If you used a regular text editor to create the code in Visual Basic, you won't have the menu Debug. In order to compile, start and debug the program, you'll need to use Visual Studio 2017 by inserting the code you created into a new project.
  • If you are using Notepad or TextEdit to create your code, be sure to save the file with the ".vb" extension, rather than ".txt" or ".text".

Advice

  • Visual Studio 2017 can be downloaded for free from the Microsoft website.
  • If you use a program such as Notepad or TextEdit to write code, it may be useful to indent the text manually, so that it is easier to read and distinguish the various parts that make up the program.

Recommended: