Learning Visual Basic (for beginners)
First, start the vb.net application
When it was open, click the new project at the left side
after that, it will as what project you will do, choose the window forms application, rename your application below that window and click ok
You now have your Form to start a new project
Making the GUI
To start making an application using VisualBasic.net you must first learn on how to create a GUI or the Graphical User’s Interface of your application. For example, you are going to do an application that inputs a number, and when the user click the “submit button” it will compute the sum of all numbers from 1-the number inputted, the result will be displayed at the bottom of the button. Before you start coding you must first makke the GUI.
First thing is to click the toolbox at the left side of the Form you are doing.
After that you can choose what component you need in your form
In the problem above, you need a TextBox, a Button and Label (where you will post the result)
So, drag the component you needed in the form and place it where you want. It will look lik this.
To edit the name of the button and the label you can edit it in the properties on the lower right side of the window.
Click the button and go to properties.
Select the Text and change the value from Button1 to submit and click <enter>.
Same procedure on the label, change it’s name to result is:
*Note: when you change th text value of a component, it didn’t change its name as a variable, so when you code the button, you still need to call it as button1 instead of submit.
Now, when you finish renaming it, it will look like this.
Now you have an interface.
To insert code in your application, double click the Submit button to view this code panel for your form:
Inside the Private Sub Button1_Click(...) sub you will now enter the process. In the problem, you need to get first the value inserted in the box. But before you code it you must first learn the basic coding in visual basic.
This is the syntax for declaring a variable
Dim Variable_name As DataType
The “Dim” is a reserve word that declare and allocates a memory for a variable. The “As” is the keyword for assigning what is the datatype of the variable.
The following table is the list of all the datatypes in vb.net
Now, you can declare a variable that will hold the value inside the textbox, you can say that:
Dim num As Integer
just above the button1_click sub
Even though you will not use a condition syntax in the problem i will teach you the use of If ... Then... End If:
This is the simple syntax of If:
If <condition> Then
In using Else or Else if:
If <condition> Then
Else If <condition>
Getting value from textBox to a variable:
You can insert the code
num = TextBox1.Text
now, because of this code the value inputted in the textBox1 (it’s “text”) was now passed to the variable num. Which is now ready for the computation.
If you review the problem above, the form must compute the sum of all numbers from 1 to inputted number, so, it needs a loop code. The loop in vb.net is not that different from the loop syntax in C++ but some syntax has made easier to understand, here , i will use the For.. Next Loop.
You can say that:
Dim ctr,sum As Integer
For ctr=0 To num Step 1
sum = sum + ctr
the syntax is
For <initialization> To <limitation> Step <iteration>
Assigning value in a Label:
Now that you have the result you can put it to be viewed by the user, simply
Label1.Text = “result is: ” & sum
Now that you learn how to make a simple application and code a “loop” and an “If” you can solve other problems like:
1. Compute the area of a triangle given the height and width
2. Fahrenheit to Celsius converter
3. Sum of all even numbers from 1-n
4. Sum of all odd numbers from 1-n
Visual Basic Functions:
Function is the one that you can call to perform a certain task and make your work become easier.For example, the Function MsgBox() , it accept a string parameter and process it to be put to another form that shows the parameter.
This tutorial, will teach you how to make your own function, Function that returns a value, with and without arguments/parameter.
Function without argument:
The syntax for this type of function is
Private Sub name_of_function()
You will notice that when you run this program and click the button, the button call your function and your function assign a string value to a label in the form that say “function without argument was called!”
Function with argument:
This time the function must receive a parameter when it was called, example of function with argument is the MsgBox() function, it receives a string argument.
The string inside is the parameter.
Here, when the function withArg() was called, it gets an integer argument, which is number 67, so the value was pass to num, the number that was declared in the function withArg(), that’s why the value of label become “the number is 67”
Function that returns a value:
Now, notice that instead of the term Sub , I use Function because the Sub cannot return a value,another is that I added the As Integer at the end of the line of the function declaration, it means that it was assigned a datatype and must have a value that is an integer. And at the end of the function, the statement Return num + 10 gives a value to the function. That is why in the third line, when the text of the label was equate in the function it receives the integer value.
Knowing console and array
Vb.net also have a console application maker for making a command line application.
To start a console application, click new, and choose the Console Application:
Why did I use console application instead of GUI? Simply because here, it is easier to explain the array and easy to make an example. But before i will show you the simple way to print and scan in a console.
We use the Console.WriteLine() function to print the string “hello world!”.
Now, let’s have an excercise for example, you need to input two numbers and find the sum, and it will goes like this:
As you notice, I inclose the Console.ReadLine() inside the Val() function, that’s because it’s value is a string and cannot be passed to an integer variable num1 and num2, the Vall() function converts the string into an integer. (in java, its the Integer.parseInt() method)
Using Select Case... End Select
The case is really more likely on java syntax, so if you allready studied java, c and c++, that will be easy to understand.
Before we proceed on array, do a little excercise to practice your skill on making a console application.
3-sum of two inputted numbers
Now, how to declare an array on vb.net?
This is the syntax
This state that the Integer arrayz can have 6 values of integer (it includes zero).
It can b like this.
Or simply like this.
Now, to use this with a short syntax, we often use loop in using array, that’s why instead of printing the value of this array one by one like this
We can make it easier this way:
Or making it really really simple
and you can ask for a topic. Thanks for reading and Good Day!