How to Add Two Numbers in Visual Basic.NET
How to Add Two Numbers in Visual Basic.NET
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

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). If you don't have a Visual Basic editor, you can use Notepad ++ or download the official Visual Basic 2017 program.

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 "" tag in HTML.

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.

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.

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.

End the button-click section. Type in End Sub and press ↵ Enter.

Create a new section. Type in Private Sub Form1_Load(sender As Object, e as EventArgs) Handles MyBase.Load and press ↵ Enter.

Add a false label tag. Type in Label4.Visible = False and press ↵ Enter, then type in End Sub and press ↵ Enter.

Create the final section. Type in Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click and press ↵ Enter.

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.

Create the "addition" command. Type in Sum = Val(TextBox1.Text) + Val(TextBox2.Text) and press ↵ Enter.

Add the "sum" command. Type in TextBox3.Text = Sum and press ↵ Enter.

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.

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".

Original news source

What's your reaction?

Comments

https://filka.info/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!