Huwebes, Mayo 19, 2011

Learning Visual Basic (for beginners)

Learning Visual Basic (for beginners)

Getting started
First, start the 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  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.
Declaring variable:
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
Numeric Data Types
Range of Values
1 byte
0 to 255
2 bytes
-32,768 to 32,767
4 bytes
-2,147,483,648 to 2,147,483,648
4 bytes
-3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values.
8 bytes
-1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
8 bytes
-922,337,203,685,477.5808 to 922,337,203,685,477.5807
12 bytes
+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use
+/- 7.9228162514264337593543950335 (28 decimal places).
Nonnumeric Data Types
Data Type
String(fixed length)
Length of string
1 to 65,400 characters
String(variable length)
Length + 10 bytes
0 to 2 billion characters
8 bytes
January 1, 100 to December 31, 9999
2 bytes
True or False
4 bytes
Any embedded object
16 bytes
Any value as large as Double
Length+22 bytes
Same as variable-length string

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
End If

In using Else or Else if:
If <condition> Then
Else If <condition>
End If

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 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()
End Sub

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 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
This is the syntax
This state that the Integer arrayz can have 6 values of integer (it includes zero).
Initializing array:
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

After you read this tutorial, and you want more about visual basic, you can Follow me on Tumblr,
and you can ask for a topic. Thanks for reading and Good Day!

Walang komento:

Mag-post ng isang Komento