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


Wednesday, 21 August 2013

CHAPTER 1

Troubleshooting If you see an error message indicating that the project you want to open is in a newer file format, you might be trying to load Visual Basic 2010 files into an older version of the Visual Basic software. (Earlier versions of Visual Basic can’t open the Visual Basic 2010 projects included on the companion CD.) To check which version of Visual Basic you’re using, click the About command on the Help menu.
Visual Studio provides a special check box named Always Show Solution to control several options related to solutions within the IDE. The check box is located on the Projects and Solutions/General tab of the Options dialog box, which you open by clicking the Options command on the Tools menu. If the check box is selected, a subfolder is created for each new solution, placing the project and its files in a separate folder beneath the solution. Also, if you select the Always Show Solution check box, a few options related to solutions appear in the IDE, such as commands on the File menu and a solution entry in Solution Explorer. If you like the idea of creating separate folders for solutions and seeing solution-related commands and settings, select this check box. You’ll learn more about these options at the end of the chapter.

Projects and Solutions
In Visual Studio, programs under development are typically called projects or solutions because they contain many individual components, not just one file. Visual Basic 2010 programs include a project file (.vbproj) and a solution file (.sln), and if you examine these files within a file browsing utility such as Windows Explorer, you’ll notice that the solution file icons have a tiny 10 in them, an indication of their version number. (Visual Basic 2010 is referred to as VB 10 internally.)
A project file contains information specific to a single programming task. A solution file contains information about one or more projects. Solution files are useful to manage multiple related projects. The samples included with this book typically have a single project for each solution, so opening the project file (.vbproj) has the same effect as opening the solution file (.sln). But for a multi-project solution, you will want to open the solution file. Visual Basic 2010 offers a new file format for its projects and solutions, but the basic terminology that you might have learned while using Visual Basic 2005 or 2008 still applies.

Tuesday, 16 July 2013

Chapter 1
Exploring the Visual Studio
Integrated Development
Environment

After completing this chapter, you will be able to:
  • Use the Visual Studio Integrated Development Environment.
  • Open and run a Visual Basic program.
  • Change property settings.
  • Move, resize, dock, and automatically hide tool windows.
  • Use the IDE Navigator.
  • Open a Web browser within Visual Studio.
  • Get Help and manage Help settings.
  • Customize IDE settings to match this book’s step-by-step instructions.
Are you ready to start working with Microsoft Visual Studio 2010? This chapter gives you the skills you need to get up and running with the Visual Studio 2010 Integrated Development Environment (IDE)—the place where you will write Microsoft Visual Basic programs.
You should read this chapter whether you are new to Visual Basic programming or you have used previous versions of Visual Basic or Visual Studio.
In this chapter, you’ll learn the essential Visual Studio menu commands and programming procedures. You’ll open and run a simple Visual Basic program named Music Trivia; 
you’ll change a programming setting called a property; and you’ll practice moving, sizing, docking, and hiding tool windows. You’ll also learn how to switch between files and tools with the IDE Navigator, open a Web browser within Visual Studio, get more information by using the online Help documentation, and customize the IDE to match this book’s step-by-step instructions. These are common tasks that you’ll use in most Visual Studio programming
sessions, and they will soon become second nature to you (if they are not already)

The Visual Studio Development Environment
First, a quick note to readers upgrading from Visual Studio 2008: Although there have been lots of internal improvements to Visual Studio 2010, the Visual Studio 2010 IDE is largely the same IDE that you worked with in Visual Studio 2008. But because you may be new to Visual Studio, I’m going to explain the basics in this chapter. Also, if you’re new to Visual Studio, something else that you should know is that although the programming language you’ll be learning in this book is Visual Basic, most of the features in the Visual Studio IDE apply equally to Visual Basic, Microsoft Visual C++, and Microsoft Visual C#. All of these programs
(or more properly, compiler technologies) are available to you in the same IDE, which you can experiment with now by starting Visual Studio and looking at the product.

Start Visual Studio 2010
  1. On the Windows taskbar, click Start, click All Programs, and then click the Microsoft Visual Studio 2010 folder.The folders and icons in the Microsoft Visual Studio 2010 folder appear in a list.
  2. Click the Microsoft Visual Studio 2010 icon.
Tip If you are using Visual Basic 2010 Express, click the Microsoft Visual Basic 2010
Express icon.

 
If this is the first time you are starting Visual Studio, the program will take a few moments to configure the environment. If you are prompted to identify your programming preferences at this time, select Visual Basic development settings.
When Visual Studio starts, you see the development environment on the screen with its many menus, tools, and component windows, as shown here. (These windows are sometimes called tool windows.) You also should see a Start Page containing a set of tabs with links, guidance and learning resources, news, and project options. The Start Page is a comprehensive source of information about your project, as well as resources
Part I
Getting Started with Microsoft Visual Basic 2010

In Part I, you’ll receive an overview of essential Microsoft Visual Basic 2010 programming techniques and an introduction to the tools and features that you will work with during most Visual Basic programming sessions. You’ll learn to use the Visual Studio 2010 Integrated Development Environment (IDE), with its fulsome collection of programming tools, windows, and menu commands, and you’ll receive step-by-step instruction on how to build and run several interesting programs from scratch. This is the place to start if you’re new to Visual Basic programming or upgrading from an earlier version.
Chapter 2 introduces how controls, forms, properties, and program code can be used in combination to create an entertaining Lucky Seven slot machine game. 
Chapter 3 provides an overview of the most useful Toolbox controls, which help you present information
or program choices to the user, gather input, work with dates and times, and connect to the Web. 
Chapter 4 focuses on adding menus, toolbars, and dialog boxes to Visual Basic programs that will give your program the flair of a commercial Windows application.