visual basic ppt

36
Welcome to Visual Basic Visual Basic IDE- Interactive Development Environment

Upload: konark-jain

Post on 07-Apr-2015

1.252 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: visual basic ppt

Welcome to Visual BasicVisual Basic IDE-

Interactive Development Environment

Page 2: visual basic ppt

                                                                                                                                                                                              

Visual Basic IDE

Page 3: visual basic ppt

• Visual Basic - The Basic Canvas Everytime you load a VB or VBA project, you will

be greeted by roughly the layout shown in Figure 1 and these five GUI tools. First, the toolbox(1) contains all the GUI elements/controls needed to create any VB form and the front end to all VB programs.

For example, after the pointer tool there is the image control, label, textbox, frame and command button as the first five of 20 standard controls which are used constantly in VB programs.

Another advanatge of these basic controls is that they fill 60-90% of all your programming needs and are automatically included in the VB runtime. It is possible to add many other Micrsoft supplied or third party components/controls to this toolbox.

Page 4: visual basic ppt

• Second is form(2). you can size it, color it, give it a caption ("Database Test" in this case) and fill the form with GUI controls which help your program do useful works. Putting controls on your form is as easy as clicking on the control (say the command button) in the toolbox and then dragging and sizing it on your form(see the "Exit" button on the form).

Page 5: visual basic ppt

• The third part of the Basic canvas are the menus and toolbars(3) which manage and control all of VB. Yes indeed there are lots of commands and operations to learn. Most students will be familiar with many of the menu and icons. File, Edit, View, Run, Window, Help menus should be familiar to any Word Perfect, Excel, or Netscape users. Likewise icons for File Open, File Save, Cut, Copy, Paste, Find, Run programs, Pause Program, Halt Program can be seen along the toolbar.

Page 6: visual basic ppt

• Fourth is the Project Explorer (4)which you use to access all the forms and coding files in your VB program. The PE-Project Explorer is such a handy navigation device you will find yourself using it all the time to switch among different forms and code.Yes, even in small to medium-sized programs multiple forms are easy to do and quite common.

Page 7: visual basic ppt

• Fifth, and even more frequently used than the Project Explorer is the Properties sheet(5). Note that the "Exit" command button is highlighted in the form and is also the control listed in the Properties sheet. If you want to change the property of any control like its color, shape, caption, or whatever - the Property sheet is the place to go. But a command button has 32 properties - and some controls have hundreds. Fortunately, the 80-20 rule applies - for example, 80 % of operations can be done with 6 properties on the command button - Appearance, Caption, Enabled, Tooltip, TabIndex, and Visible.

Page 8: visual basic ppt

• You will find in developing in Visual Basic that you spend a large percentage of time using the Project Explorer and Property sheet. It is worthwhile to get to know them well. Project Explorer is your means of navigating around the various parts of VB; while Property sheets allows you to set the very basic look and feel plus behaviour of all your forms in Visual Basic.

Page 9: visual basic ppt

VB Interface

Page 10: visual basic ppt

• Return to VB Project Explorer

VB's Project Explorer is key navigation aid among all the forms and code that make up a Visual Basic program.

• If the Project Explorer window is not showing just click the View | Project Explorer menu items (or Ctrl+R keyboard shortcut) to pop it up like in Figure on previous slide.

• As you might expect the Project Explorer works very much like Windows Explorer.

There are two folders - one for all the forms used in a VB program project and another for all the modules. Modules are Visual Basic program code that can be called and used anywhere in the VB program.

By clicking on the forms folder it expands out to show all the forms used in a VB program project. In effect the project file (.vbp file extension) keeps track of all the resources used in a VB program. Project Explorer is a window into that project file.

Page 11: visual basic ppt

• To add new forms to a project click on the Project | Add Form menu item. To delete a form no longer needed in a project click on the form in Project Explorer the select the Project | Remove menu items. However, the most common use of the Project Explorer is to quickly move among forms and modules.

Page 12: visual basic ppt

• As shown in Figure , clicking on the code icon brings up the text editor loaded with any code associated with the highlighted form or module. Right next to the code icon, is the form icon - clicking on this icon brings up the form ready for visual programming. Allternately, one can double or rightmouse click on the form or module also to quickly navigate to where you want to go.

Page 13: visual basic ppt

The Visual Basic Text Editor

Page 14: visual basic ppt

• The Visual Basic Text Editor

The VB development team has lavished great effort on making editing in VB as easy yet efficient as possible. Figure on previous slide highlights some of the key features of the text editor.

The first thing you notice is that the editor uses color coding: comments are in green; VB keywords are in blue; and most other code is in black.

Page 15: visual basic ppt

• Even more important is the fact that VB has a very smart text editor. Everytime a line of code is entered, VB checks its syntax and immediately flags any errors.

• VB's Intellisense feature gives users lots of help in getting the syntax right in the first place. Figure 2 shows Intellisense in action - as I started to type the text2 property "text", the Intellisense combo popped up with all the possible properties and had honed in on the right suggestion by the time I had type "te". All I had to do was to hit the Tab key and Intellisense filled in the rest of the command.

• Not only that but on completion of the line it checks the syntax and, if correct, immediately fancy formats the line. Fancy formatting includes adding blank spaces around operators (like "+", "-", "*", etc), auto-indenting code blocks

Page 16: visual basic ppt

• In Figure we point to the two combo box coding aids. The first combo box lists all the controls used in a form. The second combo lists all the events that a specific control (a text field in the example)can respond to as shown in Figure.

Page 17: visual basic ppt

Visual Basic An Overview

VISUAL BASIC is a high level programming language evolved from the earlier DOS version called BASIC. BASIC means Beginners' All-purpose Symbolic Instruction Code. It is a fairly easy programming language to learn. The codes look a bit like English Language. Different software companies produced different version of BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA and so on.

VISUAL BASIC is a VISUAL and  events driven Programming Language. These are the main divergence from the old BASIC. In BASIC, programming is done in a text-only environment and the program is executed sequentially. In VISUAL BASIC, programming is done in a graphical environment. Because  users may click on a certain object randomly, so each object has to be programmed independently to be able to response to those actions (events). Therefore, a VISUAL BASIC Program is made up of many subprograms, each has its own program codes, and each can be executed independently and at the same time each can be linked together in one way or another

Page 18: visual basic ppt

The Visual Basic Integrated Development Environment (IDE)

Has Standard MicroSoft “feel”.• Title Bar. Status : Design, run, debug.

• Menu Bar. Pull down menus.

• Tool Bar. • Tool Box. For adding controls.

• Project Explorer. List project modules.

• Properties Window.• Form Layout.

Page 19: visual basic ppt

The Visual Basic Integrated Development Environment (IDE)

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

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

A project is a collection of files that make up your application. There are various types of applications we could create, however, we shall concentrate on creating

Standard EXE programs (EXE means executable program). Now, click on the Standard EXE icon to go into the actual VB programming environment.

 

Page 20: visual basic ppt

Visual Basic Environment  The Visual Basic Environment consists the following :

• A  Blank Form for you to design your application's interface.

• The Project window which displays the files that are created in your application.

• The Properties window which displays the properties of various controls and objects that are created in your applications.

• It also includes a Toolbox that consists of all the controls essential for developing a VB Application. Controls are tools such as text box, command button, label, combo box, picture box, image box, timer and other objects that can be dragged and drawn on a form to perform certain tasks according to the events associated with them.

• Additional objects can be added by clicking on the project item on the menu and click on components on the drop-down list, then select those controls you need to use in your program

Page 21: visual basic ppt

Building a Visual Basic Application

Creating Your First Application

In this section, we are not going into the technical aspects of VB programming; just have a feel of it. Now, you can try out the examples below:

Example 1 It is a simple program. First of all, you

have to start Microsoft Visual Basic. Normally, a default form Form1 will be

available for you to start your new project

Now, double click on form1, the source code window for form1 (as shown ) will appear.

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

Source code window ---------------------

Page 22: visual basic ppt

Objects & Events

When you click on the object box, the drop-down list will display a list of objects you have inserted into your form as shown in figure.

Here, you can see a form, command button with the name Command1, a Label with the name Label1 a PictureBox with the name Picture1.

Similarly, when you click on the procedure box, a list of procedures associated with the object will be displayed as shown in figure 2.

Some of the procedures associated with the object Form are Activate, Click, DblClick (which means Double-Click) , DragDrop, keyPress and etc.

Each object has its own set of procedures. You can always select an object and write codes for any of its procedure in order to perform certain tasks.(AS per requirement)

 

Page 23: visual basic ppt

Structure of VB Program.VB programs are made up of different

subroutines (or procedures) of the form:

Private Sub <name>()

Comment statement(s)

Declaration statement(s)

BASIC statement(s)

End Sub

Page 24: visual basic ppt

VB Statements.

• Comments : Used to document programs and make them more readable. USE THEM !!!

‘ A Comment!

• Terminate a program.

End

Page 25: visual basic ppt

List of Procedures

 

Do not have to worry about the beginning and the end statements (i.e. Private Sub Form_Load.......End Sub.);

Just key in the lines in between the above two statements exactly as are shown here.

When you run the program, you will be surprise that nothing shown up .In order to display the output of the program, you have to add the Form1.show statement like in Example1   or you can just use Form_Activate ( )  event procedure as shown in example 2.

The command Print does not mean printing using a printer but it means displaying the output on the computer screen.

Now, press F5 or click on the run button to run the program and you will get the output as shown on next slide 

Example 1Private Sub Form_Load ( ) Form1.show Print “Welcome VB”End Sub

Example 2Private Sub Form_Activate ( )

Print 20 + 10Print 20 - 10Print 20 * 10Print 20 / 10

End Sub

Page 26: visual basic ppt

Arithmetic calculations

• You can also perform simple arithmetic calculations

• VB uses * to denote the multiplication operator

• / to denote the division operator.

• The output is shown in figure, where the results are arranged vertically.

Page 27: visual basic ppt

Print command

• Example 2. can also be written as 

Private Sub Form_Activate ( )

Print 20 + 10, 20 – 10, 20 * 10, 20 / 10

End Sub

• The numbers will be arranged in a horizontal line separated by spaces as shown in figure.

Output in a horizontal line

Page 28: visual basic ppt

Variables Example is an improved version of previous example as it

employs two variables x and y and assigned initial values of 20 and 10 to them respectively. When you need to change the values of x and y, just change the initial values rather than changing every individual value which is more time consuming.

Example Private Sub Form_Activate ( )

x = 20y = 10Print x + yPrint x - yPrint x * yPrint x / y

End Sub

Page 29: visual basic ppt

Concatenation

use the + or the & operator to join two or more texts (string) together like in examples

Example Private Sub  Form_Activate( )                

A = "Tom"B = “likes"C = “to"D = “eat"E = “burger"Print A + B + C + D + E

End Sub

Example Private Sub Form_Activate()

A = "Tom"B = “likes"C = “to"D = “eat"E = “burger"Print A & B & C & D & E

End Sub

Page 30: visual basic ppt

Steps in Building a Visual Basic Application

Generally, there are three basic steps in building a VB application. The steps are as follows:

Step 1 : Design the interface

Step 2 : Set Properties of the controls (Objects)

Step 3 : Write the events' procedures

Page 31: visual basic ppt

Example This program is a

simple program that calculates the volume of a cylinder. Lets design the interface:

Figure   A program to calculate the Volume of a Cylinder

Go to the properties window and change the form caption to Volume of Cylinder

then drag and insert three labels into the form and change their captions to Base Radius, height and volume respectively.

After that, insert three Text Boxes and clear its text contents so that you get three empty boxes. Named the text boxes as radius, hght (we cannot use height as it is the built-in control name of VB) and volume respectively.

Lastly, insert a command button and change its caption to O.K and its name to OK.

Now save the project as cylinder.vbp and the form name as cylinderl.

Page 32: visual basic ppt

Working with Controls

The Control Properties

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

Page 33: visual basic ppt

Working with ControlsProperties window for a form. • You can rename the form caption to

any name that you like best. (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.

• Properties can be set by highlighting the items in the right column then change them by typing or selecting the options available. For example, in order to change the caption, just highlight Form1 under the name Caption and change it to other names.

Page 34: visual basic ppt

Working with Controls

Change its foreground and background color, change the font type and font size, enable or disable minimize and maximize buttons and etc. For example the following code will change the form color to red every time the form is loaded. VB uses hexadecimal system to represent the color. You can check the color codes in the properties windows which are showed up under ForeColor and BackColor .

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

End Sub

Page 35: visual basic ppt

Color

• BackColor — returns or sets the background color of an object.

• ForeColor — returns or sets the foreground color used to display text and graphics in an object.

• Visual Basic uses the Microsoft Windows operating environment red-green-blue (RGB) color scheme.

Page 36: visual basic ppt