31
Visual Basic Visual Basic Precision Infomatics (M) Precision Infomatics (M) Pvt. Ltd. Pvt. Ltd.

Visual Basic6 Ppt

Embed Size (px)

Citation preview

Page 1: Visual Basic6 Ppt

Visual BasicVisual Basic

Precision Infomatics (M) Pvt. Ltd.Precision Infomatics (M) Pvt. Ltd.

Page 2: Visual Basic6 Ppt

What is Visual Basic?What is Visual Basic?

VISUAL BASICVISUAL BASIC is a high level programming language which was evolved is a high level programming language which was evolved from the earlier DOS version called BASIC.from the earlier DOS version called BASIC.

Visual Basic is an Object based Event Driven Programming Langugage.Visual Basic is an Object based Event Driven Programming Langugage.

However, it seems people only use Microsoft Visual Basic today, as it is a However, it seems people only use Microsoft Visual Basic today, as it is a well developed programming language and supporting resources are well developed programming language and supporting resources are available everywhere. available everywhere.

VISUAL BASIC Program is made up of many subprograms, each has its VISUAL BASIC Program is made up of many subprograms, each has its own program codes, and each can be executed independently and at the own program codes, and each can be executed independently and at the same time each can be linked together in one way or another. same time each can be linked together in one way or another.

Page 3: Visual Basic6 Ppt

Visual Basic EnvironmentVisual Basic Environment

On start up, Visual Basic 6.0  will On start up, Visual Basic 6.0  will display the following dialog box as display the following dialog box as shown in figure.shown in figure.

You can choose to either start a new You can choose to either start a new project, open an existing project or project, open an existing project or select a list of recently opened select a list of recently opened programs. programs.

A project is a collection of files that A project is a collection of files that make up your application. make up your application.

There are various types of applications There are various types of applications we could create, however, we shall we could create, however, we shall concentrate on creating Standard EXE concentrate on creating Standard EXE programs programs

Page 4: Visual Basic6 Ppt

Visual Basic EnvironmentVisual Basic Environment

Page 5: Visual Basic6 Ppt

Visual Basic EnvironmentVisual Basic Environment

Page 6: Visual Basic6 Ppt

Building Visual Basic Building Visual Basic ApplicationsApplications

First, you have to launch First, you have to launch Microsoft Visual Basic. Normally, Microsoft Visual Basic. Normally, a default form Form1 will be a default form Form1 will be available for you to start your available for you to start your new project. new project.

Double click on form1, the source Double click on form1, the source code window for form1 as shown code window for form1 as shown in figure will appear. in figure will appear.

The top of the source code The top of the source code window consists of a list of window consists of a list of objects and their associated objects and their associated events or procedures. In the events or procedures. In the given figure the object displayed given figure the object displayed is Form and the associated is Form and the associated procedure is Load. procedure is Load.

Page 7: Visual Basic6 Ppt

Building Visual Basic Building Visual Basic ApplicationsApplications

Private Sub Form_Load()Private Sub Form_Load()

Form1.ShowForm1.Show

Print "Welcome to Visual Basic Print "Welcome to Visual Basic Tutorial"Tutorial"

End SubEnd Sub

Page 8: Visual Basic6 Ppt

Building Visual Basic Building Visual Basic AppliationsAppliations

Page 9: Visual Basic6 Ppt

Building Visual Basic Building Visual Basic ApplicationsApplications

Page 10: Visual Basic6 Ppt

Working With ControlsWorking With Controls

Some of the Common Controls in VBSome of the Common Controls in VB Text BoxText Box LabelLabel Command ButtonCommand Button Picture BoxPicture Box Image BoxImage Box List BoxList Box Combo BoxCombo Box Check BoxCheck Box Option BoxOption Box Driver list BoxDriver list Box Directory list BoxDirectory list Box File List BoxFile List Box

Page 11: Visual Basic6 Ppt

Controls PropertiesControls Properties

Before writing an event Before writing an event procedure for the control procedure for the control to response to a user's to response to a user's input, you have to set input, you have to set certain properties for the certain properties for the control to determine its control to determine its appearance and how it appearance and how it will work with the event will work with the event procedure. You can set procedure. You can set the properties of the the properties of the controls in the properties controls in the properties window or at runtime.window or at runtime.

Page 12: Visual Basic6 Ppt

Control PropertiesControl Properties

You can also change the You can also change the properties at runtime to give properties at runtime to give special effects such special effects such as change of color, shape, as change of color, shape, animation effect and so on.animation effect and so on.

For example the following For example the following code will change the form code will change the form color to red every time the color to red every time the form is loaded. form is loaded.

VB uses hexadecimal system VB uses hexadecimal system to represent the color.to represent the color.

You can check the color You can check the color codes in the properties codes in the properties windows which are showed windows which are showed up under ForeColor and up under ForeColor and BackColor . BackColor .

Private Sub Form_Load()Private Sub Form_Load()Form1.ShowForm1.ShowForm1.BackColor = Form1.BackColor = &H000000FF&&H000000FF&End SubEnd Sub

Page 13: Visual Basic6 Ppt

Text Box ControlText Box Control

The text box is the standard The text box is the standard control that is used to receive control that is used to receive input from the user as well as input from the user as well as to display the output.to display the output.

It can handle string (text) and It can handle string (text) and numeric data but not images numeric data but not images or pictures. or pictures.

String in a text box can be String in a text box can be converted to a numeric data converted to a numeric data by using the function by using the function Val(text). Val(text).

The following example The following example illustrates a simple program illustrates a simple program that processes the inputs that processes the inputs from the user. from the user. 

Page 14: Visual Basic6 Ppt

Text Box ControlText Box Control

The text box is the standard The text box is the standard control that is used to receive control that is used to receive input from the user as well as input from the user as well as to display the output.to display the output.

It can handle string (text) and It can handle string (text) and numeric data but not images numeric data but not images or pictures. or pictures.

String in a text box can be String in a text box can be converted to a numeric data converted to a numeric data by using the function by using the function Val(text). Val(text).

The following example The following example illustrates a simple program illustrates a simple program that processes the inputs that processes the inputs from the user. from the user. 

Page 15: Visual Basic6 Ppt

Text Box ControlText Box Control

In this program, three text boxes are In this program, three text boxes are inserted into the form together with a few inserted into the form together with a few labels.labels.

The two text boxes are used to accept The two text boxes are used to accept inputs from the user and one of the text inputs from the user and one of the text box will be used to display the sum of two box will be used to display the sum of two numbers that are entered into the two text numbers that are entered into the two text boxes.boxes.

Besides, a command button is also Besides, a command button is also programmed to calculate the sum of the programmed to calculate the sum of the two numbers using the plus operator.two numbers using the plus operator.

The program use creates a variable sum The program use creates a variable sum to accept the summation of values from to accept the summation of values from text box 1 and text box 2.The procedure to text box 1 and text box 2.The procedure to calculate and to display the output on the calculate and to display the output on the textbox3 is shown below. textbox3 is shown below.

Private Sub cmdcalculate_Click()Private Sub cmdcalculate_Click()txtsum = Val(txtnumber1.Text) + txtsum = Val(txtnumber1.Text) + Val(txtnumber2.Text)Val(txtnumber2.Text)End SubEnd Sub

Page 16: Visual Basic6 Ppt

Label ControlLabel Control

The label is a very useful control for Visual The label is a very useful control for Visual Basic, as it is not only used to provide Basic, as it is not only used to provide instructions and guides to the users, it can also instructions and guides to the users, it can also be used to display outputs. be used to display outputs.

One of its most important properties is One of its most important properties is CaptionCaption. Using the syntax . Using the syntax label.Captionlabel.Caption, it , it can display text and numeric data . can display text and numeric data .

You can change its caption in the properties You can change its caption in the properties window and also at runtime.  window and also at runtime. 

Page 17: Visual Basic6 Ppt

The Command ButtonThe Command Button

The command button is a very important The command button is a very important control as it is used to execute commands. control as it is used to execute commands.

It displays an illusion that the button is pressed It displays an illusion that the button is pressed when the user click on it. when the user click on it.

The most common event associated with the The most common event associated with the command button is the Click event, and the command button is the Click event, and the syntax for the procedure issyntax for the procedure is

Private Sub Command1_Click ()Private Sub Command1_Click ()StatementsStatementsEnd SubEnd Sub

Page 18: Visual Basic6 Ppt

Picture ControlPicture Control

The Picture Box is one of the controls that used to The Picture Box is one of the controls that used to handle graphics. handle graphics.

You can load a picture at design phase by clicking on You can load a picture at design phase by clicking on the picture item in the properties window and select the the picture item in the properties window and select the picture from the selected folder.picture from the selected folder.

You can also load the picture at runtime using the You can also load the picture at runtime using the LoadPicture LoadPicture method. For example, the statement will method. For example, the statement will load the picture sample.gif into the picture box.load the picture sample.gif into the picture box.

Picture1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")Picture1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")

Page 19: Visual Basic6 Ppt

Image BoxImage Box

The Image Box is another control that handles images The Image Box is another control that handles images and pictures.and pictures.

It functions almost identically to the picture box. It functions almost identically to the picture box. However, there is one major difference, the image in However, there is one major difference, the image in an Image Box is stretchable, which means it can be an Image Box is stretchable, which means it can be resized.resized.

This feature is not available in the Picture Box. Similar This feature is not available in the Picture Box. Similar to the Picture Box, it can also use the LoadPicture to the Picture Box, it can also use the LoadPicture method to load the picture.method to load the picture.

For example, the statement loads the picture For example, the statement loads the picture Sample.gif into the image box.Sample.gif into the image box.

Image1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")Image1.Picture=LoadPicture ("C:\VB program\Images\sample.gif")

Page 20: Visual Basic6 Ppt

List BoxList Box

The function of the List Box is to present a list of items where the user can click and select the The function of the List Box is to present a list of items where the user can click and select the items from the list. items from the list.

In order to add items to the list, we can use the In order to add items to the list, we can use the AddItem methodAddItem method. .

For example, if you wish to add a number of items to list box1, you can key in the following For example, if you wish to add a number of items to list box1, you can key in the following statements.statements.

Private Sub Form_Load ( )Private Sub Form_Load ( )  

List1.AddItem “Lesson1”List1.AddItem “Lesson1”List1.AddItem “Lesson2”List1.AddItem “Lesson2”List1.AddItem “Lesson3”List1.AddItem “Lesson3”List1.AddItem “Lesson4”List1.AddItem “Lesson4”End SubEnd Sub

The items in the list box can be identified by the The items in the list box can be identified by the ListIndexListIndex property, the value of the ListIndex property, the value of the ListIndex for the first item is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2 for the first item is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2 and so on.and so on.

Page 21: Visual Basic6 Ppt

Combo Box Combo Box The function of the Combo Box is also to present a list of items where the The function of the Combo Box is also to present a list of items where the

user can click and select the items from the list.user can click and select the items from the list. However, the user needs to click on the small arrowhead on the right of However, the user needs to click on the small arrowhead on the right of

the combo box to see the items which are presented in a drop-down list.the combo box to see the items which are presented in a drop-down list. In order to add items to the list, you can also use the In order to add items to the list, you can also use the AddItem methodAddItem method. .

For example, if you wish to add a number of items to Combo box 1, you For example, if you wish to add a number of items to Combo box 1, you can key in the following statementscan key in the following statements

Example 3.3Example 3.3Private Sub Form_Load ( )Private Sub Form_Load ( )

  Combo1.AddItem “Item1”Combo1.AddItem “Item1”Combo1.AddItem “Item2”Combo1.AddItem “Item2”Combo1.AddItem “Item3”Combo1.AddItem “Item3”Combo1.AddItem “Item4”Combo1.AddItem “Item4”End SubEnd Sub

Page 22: Visual Basic6 Ppt

Check BoxCheck Box

The Check Box control lets the user to select or The Check Box control lets the user to select or unselect an option.unselect an option.

When the Check Box is checked, its value is set to 1 When the Check Box is checked, its value is set to 1 and when it is unchecked, the value is set to 0.  You and when it is unchecked, the value is set to 0.  You can include the statements Check1.Value=1 to mark can include the statements Check1.Value=1 to mark the Check Box and Check1.Value=0 unmark the Check the Check Box and Check1.Value=0 unmark the Check Box, and use them to initiate certain actions. Box, and use them to initiate certain actions.

For example, the program will change the background For example, the program will change the background color of the form to red when the check box is color of the form to red when the check box is unchecked and it will change to blue when the check unchecked and it will change to blue when the check box is checked.box is checked.

Page 23: Visual Basic6 Ppt

Option BoxOption Box

The Option Box control also lets the user selects one of the The Option Box control also lets the user selects one of the choices.choices.

However, two or more Option Boxes must work together because However, two or more Option Boxes must work together because as one of the Option Boxes is selected, the other Option Boxes as one of the Option Boxes is selected, the other Option Boxes will be unselected.will be unselected.

In fact, only one Option Box can be selected at one time. When an In fact, only one Option Box can be selected at one time. When an option box is selected, its value is set to “True” and when it is option box is selected, its value is set to “True” and when it is unselected; its value is set to “False”. unselected; its value is set to “False”.

In the following example, the shape control is placed in the form In the following example, the shape control is placed in the form together with six Option Boxes. When the user clicks on different together with six Option Boxes. When the user clicks on different option boxes, different shapes will appear. option boxes, different shapes will appear.

The values of the shape control are 0, 1, and 2,3,4,5 which will The values of the shape control are 0, 1, and 2,3,4,5 which will make it appear as a rectangle, a square, an oval shape, a make it appear as a rectangle, a square, an oval shape, a rounded rectangle and a rounded square respectively.rounded rectangle and a rounded square respectively.

Page 24: Visual Basic6 Ppt

Option Box ExampleOption Box Example

Private Sub Option1_Click ( )Private Sub Option1_Click ( )Shape1.Shape = 0Shape1.Shape = 0End SubEnd SubPrivate Sub Option2_Click()Private Sub Option2_Click()Shape1.Shape = 1Shape1.Shape = 1End SubEnd SubPrivate Sub Option3_Click()Private Sub Option3_Click()Shape1.Shape = 2Shape1.Shape = 2End SubEnd SubPrivate Sub Option4_Click()Private Sub Option4_Click()Shape1.Shape = 3Shape1.Shape = 3End SubEnd SubPrivate Sub Option5_Click()Private Sub Option5_Click()Shape1.Shape = 4Shape1.Shape = 4End SubEnd SubPrivate Sub Option6_Click()Private Sub Option6_Click()Shape1.Shape = 5Shape1.Shape = 5End Sub End Sub

Page 25: Visual Basic6 Ppt

Driver List BoxDriver List Box

The Drive ListBox is The Drive ListBox is used to display a list of used to display a list of drives available in your drives available in your computer. When you computer. When you place this control into the place this control into the form and run the form and run the program, you will be able program, you will be able to select different drives to select different drives from your computer as from your computer as shown in Figure.shown in Figure.

Page 26: Visual Basic6 Ppt

Directory List BoxDirectory List Box

The Directory List Box is The Directory List Box is used to display the list of used to display the list of directories or folders in a directories or folders in a selected drive. selected drive.

When you place this When you place this control into the form and control into the form and run the program, you will run the program, you will be able to select be able to select different directories from different directories from a selected drive in your a selected drive in your computer as shown in computer as shown in FigureFigure

Page 27: Visual Basic6 Ppt

File List BoxFile List Box

The File List Box is used to display the The File List Box is used to display the list of files in a selected directory or list of files in a selected directory or folder. When you place this control into folder. When you place this control into the form and run the program, you will be the form and run the program, you will be able to a list of files in a selected able to a list of files in a selected directory.directory.

Page 28: Visual Basic6 Ppt

Data Types in Visual Data Types in Visual BasicBasic

Visual Basic classifies the information mentioned into two Visual Basic classifies the information mentioned into two

major data types, they are numeric data type and non-major data types, they are numeric data type and non-

numeric data types.numeric data types.

Numeric Data TypeNon Numeric

Data Type

Data Types

Page 29: Visual Basic6 Ppt

Numeric Data TypeNumeric Data Type

Page 30: Visual Basic6 Ppt

Non Numeric Data TypeNon Numeric Data Type

Page 31: Visual Basic6 Ppt