Sunday, 18 September 2016

visual Basic 2010 Lesson 5-Writing the Code

In preceding  lessons, you have learned some basic concepts of  Visual Basic 2010 programming. In this lesson, we  learn to write actual programmes . We will keep the theories short so that it would not be too difficult for you.

5.1 The event Procedure
Visual Basic 2010 is an object oriented and event driven programming language. In fact, all windows applications are event driven. Event driven means the user decides what actions to take, like clicking a command button or  entering text in a text box. An event is related to an object, it is an incident that happens to the object due to the action of the user , such as clicking the mouse or depressing a key on the keyboard. A class has events as it creates an instant of a class or an object.
When we launch Visual Basic 2010 , we will see a default form with the Form1 appears in its IDE, it is actually the Form1 Class that inherits from the Form class System.Windows.Forms.Form, as shown in the Form1 properties windows.


Visual Basic 2010

When we click on any part of the form, we will see the code window as shown below. The is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with Private Sub and end with End Sub. This procedure includes the Form1 class and the event Load, and they are bind together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. You don’t have to worry the rest of the stuff at the moment, they will be explained in later lessons.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
The are other events associated with the Form1 class, such as click, cursorChanged, DoubleClick, DragDrop, Enter as so on, as shown in the diagram below (It appears when you click on the upper right pane of the code window)
Visual Basic 2010

5.2 Writing the code
Now you are ready to write the code for the event procedure so that it will do something more than loading a blank form. The code must be entered between Private Sub…….End Sub. Let’s enter the following code :

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = “My First VB2010 Program”
Me.ForeColor = Color.ForestGreen
Me.BackColor = Color.Cyan
End Sub
End Classs

The first line of the code will change the title of the form to My First Visual Basic 2010 Program, the second line will change the foreground object to Forest Green( in this case, it is a label that you insert into the form and change its name to Foreground) and the last line changes the background to Cyan color.
The equal (=)in the code actually is used to assign something to the object, like assigning yellow color to the foreground of the Form1 object (or an instance of Form1). Me is the name given to the Form1 class. We can also call those lines as Statements. So, the actions of the program will depend on the statements entered by the programmer.
The output is shown in the windows below:
http://www.vbtutor.net/vb2010/vb2010_Image/vb2010_5.3.gif


here is another example.

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name1, name2, name3 As String
name1 = “John”
name2 = “Georges”
name3 = “Ali”
MsgBox(” The names are ” & name1 & ” , ” & name2 & ” and ” & name3)
End Sub

In this example, you insert one command button into the form and rename its caption as Show Hidden Names. The keyword Dim is to declare variables name1, name2 and name3 as string, which means they can only handle text. The function MsgBox is to display the names in a message box that are joined together by the “&” signs. The output is shown below:
Visual Basic 2010


Visual Basic 2010 Lesson 4 -Object Oriented Programming

The Concept of Object Oriented Programming
In preceding lessons, you have learned how to write simple VB programme code. However, you still don’t have much understanding about the underlying concepts surrounding VB2010, particularly the concept of object oriented programming.
Though VB2010 is very much similar to VB6 in terms of syntaxes and program structure, their underlying concepts are very different. The main difference is that VB2010 is an Object Oriented Programming Language. Though VB6 may have some OOP capabilities, it is not an object oriented programming language. An object oriented programming language has three core technologies namely encapsulation, inheritance and polymorphism. These three concepts are explained below:

Encapsulation
Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called classes. Each class contains data as well as a set of methods which manipulate the data. The data components of a class are called instance variables and one instance of a class is an object. For example, in a library system, a class could be member, and John and Sharon could be two instances (two objects) of the library class.

Inheritance
In object oriented programming, classes are created according to their hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy to another class. That means less programming is required when adding functions to complex systems, therefore save time and effort. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object oriented programming.

Polymorphism
Object-oriented programming allows procedures about objects to be created whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on screen in response to mouse movement would be written for “cursor,” and polymorphism allows that cursor to take on whatever shape is required at runtime. It also allows new shapes to be easily integrated.
VB6 is not an object oriented programming language because it does not have inheritance capabilities(although it can make use of some benefits of inheritance). However, VB2010 is a fully functional Object Oriented Programming Language, just like other OOP such as C++ and Java. It is different from the earlier versions of VB because it focuses more on the data itself while the previous versions focus more on the actions. Previous versions of VB are known as procedural or functional programming language. Some other procedural programming languages are C, Pascal and Fortran.
VB2010 allows users to write programs that break down into modules. These modules represents the real-world objects known as classes or types. An object can be created out of a class known as an instance of the class. A class can also comprise subclass. For example, apple tree is a subclass of the plant class and the apple in your backyard is an instance of the apple tree class. Another example is student class is a subclass of the human class while your son John is an instance of the student class.
A class consists of data members as well as methods. In VB2010, the program structure to define a Human class can be written as follows:

Public Class Human
‘Data Members
Private Name As String
Private Birthdate As String
Private Gender As String
Private Age As Integer
‘Methods
Overridable Sub ShowInfo( )
MessageBox.Show(Name)
MessageBox.Show(Birthdate)
MessageBox.Show(Gender)
MessageBox.Show(Age)
End Sub
End Class


Let’s look at one example on how to create a class. The following example shows you how to create a class that can calculate your BMI (Body Mass Index).
To create class, start VB2010 as usual and choose Windows Applications. In the VB2010 IDE, click on Project on the menu bar and select Add Class, the Add New Item dialog appears, as shown in the Figure below:

The default class Class1.vb will appear as a new tab with a code window. Rename the class as MyClass.vb. Rename the form as MyFirstClass.vb.
Now, in the MyClass.vb window, enter the follow code

Public Function BMI(ByVal height As Single, ByVal weight As Single)
BMI = Format((weight) / (height ^ 2), “0.00”)
End Function

Now you have created a class (an object) called MyClass with a method known as BMI.
In order to use the BMI class,  insert a button into the form and click on the button to enter the following code:

Private Sub BtnCalBmi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalBmi.Click
Dim MyObject As Object
Dim h, w As Single
MyObject = New MyClass1()
h = InputBox(“What is your height in meter”)
w = InputBox(“What is your weight in kg”)
MessageBox.Show(MyObject.BMI(h, w))
End Sub

When you run this program and click the button, the user will be presented with two input boxes to enter his or her height and weight subsequently and the value of BMI will be shown in a pop-up message box.

Visual Basic 2010 Lesson 3-Working with Control Properties

3.1 The Control Properties in VB2010

After adding a control to the default form,  set certain properties for the control to determine its appearance and how should it behaves. You can set the properties of the controls in the properties window at design time or at runtime. Figure 3.1 is a typical properties window for a form in Visual Basic 2010 IDE:
 

Figure 3.1




The title of the form is defined by the Text property and its default name is Form 1. To change the form’s title to any name that you like, simple click in the box on the right of the Text property and type in the new name.In this example, the title is Addition Calculator. Notice that the title will appear on top of the window. In the properties window, the item appears at the top part is the object currently selected (in Figure 3.2, the object selected is Form1). At the bottom part, the items listed in the left column represent the names of various properties associated with the selected object while the items listed in the right column represent the states of the properties.
Visual Basic 2010

Figure 3.2




Properties can be set by highlighting the items in the right column then change them by typing or selecting the options available. You may also alter other properties of the form such as font, location, size, foreground color, background color ,MaximizeBox, MinimizeBox and etc. You can also change the properties of the object at runtime to give special effects such as change of color, shape, animation effect and so on.
For example the following code will change the form color to yellow every time the form is loaded. Visual Basic 2010 uses RGB(Red, Green, Blue) to determine the colors. The RGB code for yellow is 255,255,0. Me in the code refer to the current form and Backcolor is the property of the form’s background color. The formula to assign the RGB color to the form is Color.FormArbg(RGB code).  The event procedure is as follows:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackColor = Color.FromArgb(255, 0, 255)
End Sub
End Class
You may also use the follow procedure to assign the color at run time.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackColor = Color.Magenta
End Sub
Both procedures above will load the form with a magenta background as follows:

Here are some of the common colors and the corresponding RGB codes. You can always experiment with other combinations, but remember the maximum number for each color is 255 and the minimum number is 0.

Visual Basic 2010
The following is another program that allows the user to enter the RGB codes into three different text boxes and when he or she clicks the display color button, the background color of the form will change according to the RGB codes. So, this program allows users to change the color properties of the form at run time.
Visual Basic 2010
The code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rgb1, rgb2, rgb3 As Integer
rgb1 = TextBox1.Text
rgb2 = TextBox2.Text
rgb3 = TextBox3.Text
Me.BackColor = Color.FromArgb(rgb1, rgb2, rgb3)

End Sub

Monday, 12 September 2016

Visual Basic 2010 Lesson 2-Working with Controls

Controls  are objects that can be inserted into the form of VB2010 IDE for various purposes. You can write relevant code for them to perform certain tasks. The figure below shows the Toolbox that contains the controls. They are categorized into Common Controls, Containers, Menus, Toolbars, Data, Components, Printings and Dialogs. The most used common controls are Button, Label, ComboBox, ListBox, PictureBox and TextBox.
To insert a control into the form, drag the control from the tool box and drop it into the form. You can customise it according to your needs. For example, you can reposition and resize it. Let’s examine a few examples that involved Button, Label, TextBox , ListBox and PictureBox . You don’t have to worry so much about the code yet because we will explain the programme syntaxes as you progress to later lessons.
Visual Basic 2010

When you click on the Toolbox tab, the common controls Toolbox will appear.

2.1 Creating your first VB2010 programme

To create your first programme, drag the button control into the form, and change its default Text Button1 to OK in the properties window, the word OK will appear on the button in the form, as shown below:
Visual Basic 2010
Now click on the OK button and the code window appears. Enter the code as follows:
When you run the the program and click on the OK button, a dialog box will appear and display the “WELCOME TO VISUAL BASIC 2010” message,as shown below:
Visual Basic 2010
There you are, you have created your first Visual Basic 2010 program.

2.2 Using the Text Box

We will show you how to create a simple calculator using the TextBox control. In this programme, insert two text boxes , three labels and one button. The two text boxes are for the user to enter two numbers, one label is to display the addition operator and the other label is to display the equal sign. The last label is to display the answer. Now change the label on the button to Calculate,then click on this button and enter the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, product As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
product = num1 + num2
Label1.Text = product
End Sub
When you run the program and enter two numbers, pressing the calculate button will allows the program to add the two numbers.
Visual Basic 2010