building a visual basic

23
BUILDING A VISUAL BASIC In Making a Visual Basic project, you have to consider the design of the form, setting of property values inside the properties window, setting controls, and the codes that you will input inside the code window. 1. Load the Visual Basic program. 2. When the computer prompts in the New Project window, choose the Standard EXE project. 3. Click the Open button. Standard EXE Open button

Upload: rjfcooper

Post on 18-Jul-2016

14 views

Category:

Documents


7 download

DESCRIPTION

VB 6.0

TRANSCRIPT

Page 1: Building a Visual Basic

BUILDING A VISUAL BASICIn Making a Visual Basic project, you have to consider the design of the form, setting of property values inside the properties window, setting controls, and the codes that you will input inside the code window.

1. Load the Visual Basic program.2. When the computer prompts in the New Project window, choose the Standard EXE project.3. Click the Open button.

Standard EXE

Open button

Page 2: Building a Visual Basic

Opening a ProjectThere are two ways in opening a project. Open the New Project window icon. Click File menu, the Open Project command.

1. Load the Visual Basic program.2. When the New Project window prompts, click the Existing tab.3. Look for the project, then click.4. Click the Open button.

Page 3: Building a Visual Basic

Property Basics

A rectangular window on the screen is called the form. The window's appearance is defined by a set of properties. The position of the form on the screen is named by its Left and Top properties while its size is labeled by its Width and Height properties. What you see in the title bar is the Caption property and if you specify the control buttons, they will appear on the form.

Title Bar

Height

Control Buttons

WidthA form’s appearance is defined by its properties.

Page 4: Building a Visual Basic

Placing Controls on the Form

Controls are the building blocks of a Visual Basic application.They are easy to use, when used properly. They add significant functionality to your programs. To add control to a form, you simply double-click the control you want to add or you can “draw” the control on the formby clicking the controland dragging the mousearound the area whereyou want to place it.

ControlsDouble-click

Created Controlson the form

To save a form as a project, Visual Basic creates a text file with an .FRM extension. This file includes the properties that define the form and its object

Page 5: Building a Visual Basic

Setting Control Properties (Design Mode)After placing controls on the form, set control properties inside the Properties Window.

To set the Name/Caption property of the form1. Look for Name Property in the Properties window, then double-click. Type frmfirst in the field.2. Then look for Caption Property, double-click.3. Type Justine Francis Penasbo. Notice that after changing the caption property, the caption name in the form changes too.

To set the Name/Caption property of the label4. Double-click the Label control.5. In the Properties window, double-click the Caption property.6. Type Justine’s First Program. Notice that the label box inside the form displays the

text that you typed in the Properties window.

Page 6: Building a Visual Basic

EVENT PROCEDURE

Visual Basic is an event-driven language. It is governed by an event processor. If the event is detected, the project finds a series of instructions related to that event, called event procedure. The procedure is executed, then program control is returned to the event. Event procedures are the steps taken when we do the actual computer programming.

Some of the main user actions that execute events in a program

Starting the program Press a key Click the mouse Move the mouse Close the program

Page 7: Building a Visual Basic

Events Common to Many ControlsEVENT ACTIONS

Change The user changes the textbook or combobox.

Click The user clicks an object with the primary mouse.

DblClick The user double-clicks an object with the mouse.

DragDrop The user drags a control to another place.

DragOver An object is dragged over a control.

GotFocus An object receives the focus.

KeyDown A key is pressed while an object has the focus.

KeyPress A key is pressed and released while an object has the focus.

KeyUp A key is released while an object has the focus.

LostFocus The focus has left an object.

MouseDown A button is pressed while the pointer is over an object.

MouseMove The cursor is moved over an object.

MouseUp The button is released while the pointer is over an object.

Events Procedure begins with the statement Private Sub and ends with End Sub.

Page 8: Building a Visual Basic

Events Common to Many Controls

EVENT PROCEDURE DESCRIPTION

Form_Load It is called once when form is loaded into memory. Place where to do initialization in the code for any part of the formwith the controls and variables.

Form_Paint It is called everytime the form needs to be repainted. Repainting is the process of updating the form image on the screen.

Form_Click It is called when the user clicks an area of the form not covered by a control.

buttonname_click It is called when the user clicks a button.

Events

Event Procedure

Page 9: Building a Visual Basic

Private-End block

body of the event

It is a section of code that goes together as a single unit.

Page 10: Building a Visual Basic

NAMING OF CONTROLS

Controls are the graphical features or tools that are placed on forms to allow user interaction, controls are referred to as objects. The Toolbox window is the selection menu for controls used in your application.

PointerLabelFrame

CheckBoxComboBoxHScrollBar

TimerDirListBox

ShapeImage

OLE

PictureBoxTextBoxCommandButtonOptionButtonListBoxVScrollBarDriveListBoxFileListBoxLineData

Page 11: Building a Visual Basic

CheckBox (chk) allows the user to select.ComboBox (cbo) combines the features of both text box and a list box. This allows you to select a predefined item from the list or to enter a new item not in the listCommandButton (cmd) is one of the most common controls found in Windows application. Command button is used to elicit simple responses from the user.

Data control (dat) creates a control that provides access to data in a database through bound controls on your forms.

DirListBox (dir) is used in conjunction with the drive control and file control.

FileListBox (fil) is used to list the actual filenames that are in the directory specified by the Path property.

Frame control (fra) is used for grouping the controls inside the frame so that when the frame is moved, the other controls inside it will move too. The controls normally placed in the frame are option buttons and check boxes. Shape (shp) creates a control that can draw a variety of shapes on your form at design time.TextBox (txt) is used for accepting user input or for entering data.

Timer control (tmr) checks the system clock.

DriveListBox (drv) is used in conjunction with the directory list and the file list controls.

Page 12: Building a Visual Basic

Image control (img) is used to display a picture on a form. It consumes less memory that Picture control.

Label control (lbl) displays read-only text.

Line (lin) lets you create a control to draw a variety of line styles on your form at design time.

ListBox (lst) is used in presenting users with a list of data. Users can browse the data in the list box or select one or more items for further processing.

OLE icon (ole) allows you to create a control that links and embed objects from other applications in your Visual Basic application.

OptionButton (opt) is also called radio button. This allows the user to select only one option from a group of options.

PictureBox (pic) control is used to display graphics; for example, bitmaps, icons, JPEGs, GIFs.Pointer allows you to click a control object and thereby displays the properties for that control in the property window.

ScrollBars are used to increase and decrease a value; such as when you want to change the color setting, a number, or the volume of a digital audio device. This bar acts as a sliding scale with starting point and ending point.

Page 13: Building a Visual Basic

WORKING WITH CONTROLS AND OBJECTSControls allow you to trigger events or perform actions. These are the building blocks of Visual Basic application, to add controls on the form, double-click the control you want to add or draw the control by clicking the control and then drag the mouse around the area where you want to put the control.

To use the Command Button1. Select the Command Button from the Toolbox.2. Click and draw the button on the form. Resize it depending on how or where you

want it to appear.3. Change the Name of the Command Button to cmdTinboy. Change the Caption to Tinboy.4. Select a CommandButton again from the Toolbox. Draw the CommandButton in the form.

Page 14: Building a Visual Basic

5. Change the Name of the CommandButton to cmdTin. Change the Caption to Tin. 6. Double-click the Tinboy CommandButton. This brings you to the Code Editor window. Type the following: cmdTin.enabled=True cmdTinboy.enabled=False7. Click the X button of the Code Editor window.8. Double-click the Tin Command Button. Type the following code inside the Code Editor window. cmdTin.enabled=False cmdTinboy.enabled=True9. Run the program by pressing F5 or by clicking the Run menu, then Start. Observe two CommandButtons.

X button (to close the Code Editor window

codes for Tinboy and Tin CommandButtons

Page 15: Building a Visual Basic

To use the Label control1. Select Label from the Toolbox.2. Draw the label on the previous form.3. Change the label’s property Name to Jhennylabel.4. Change the property BorderStyle to Fix Single.5. Make changes on the code for each CommandButton. Type the following codes: for Tinboy CommandButton: cmdTin.enabled=True cmdTinboy.enabled=False Jhennylabel.caption=cmdtin.caption6. Run the program by pressing F5 or click button. Observe.

Page 16: Building a Visual Basic

To use the TextBox1. Make a new project.2. Select TextBox from the Toolbox.3. Change the property Name to txtfrancis.

4. Set the property Multiline to True. (This property allows you to type to

the next line.) 5. Set the property Scrollbars to Both.

6. Run the program. Type a list inside the TextBox and observe scrollbars.

Page 17: Building a Visual Basic

To use the Frame and Option buttons1. Select Frame in the Toolbox and draw in the form. Change the property Name to fraStatus. Change the property Caption to Civil Status.2. Select Option button. Draw inside the frame. Change the property Name to optSingle. Change the property Caption to Single.3. Select another Option button. Draw inside the frame. Change the property Name to optMarried. Change the property Caption to Married.4. Select Frame again and draw on the form. Change the property Name to frachildren. Change the property Caption to Number of Children.5. Select TextBox in the Toolbox and draw inside the Number of Children frame. Change the property name to txtchildren.6. Double-click the Single option box, then type the following code.

frachildren.enabled=Falsetxtchildren.enabled=False

7. Click the X button to exit in the Code Editor window.8. Double-click the Married option box, then type the following code:

frachildren.enabled=Truetxtchildren.enabled=True

9. Run the program by pressing F5 or click button. Observe the frame and textbox of Number of Children frame.

Page 18: Building a Visual Basic

In the Single option, the Number of Children frame is not accessible.

In the Married option, the Number of Children frame is accessible.

Page 19: Building a Visual Basic

RUNNING, CLOSING AND SAVING A PROJECT

Running the project

There are three ways to run the program.

Press the F5 key.

Click Run in the menu, then select Start.

Click the button.

Page 20: Building a Visual Basic

Saving the project

There are to ways to save the project. Use the Toolbar , click the Save Project toolbar icon. Click the File menu, then the Save Project/Save Project As dialog box. The Save File As dialog box specifies form file name that will store all the form’s information. After specifying the name and directory, click the Save button.

After clicking the Save button, the next dialog box that appears is the Save Project As… dialog. The features of this dialog box is similar to the features of Save File As dialog, except that in Save Project As dialog box it specifies the project file name.

current directory

contents of current directory

save file as

Page 21: Building a Visual Basic

Closing/ Terminating the Project

There are three ways to close/terminate the project. Click the form’s Close button icon ( the X button of the form)

Close button of the form

Click the Run menu, then the End command.

Run menu.

End command in the run menu.

Click the End icon in the Toolbar.End command.

Page 22: Building a Visual Basic

CREATING AN .EXE FILEIt is very simple to create an .EXE file in the Visual Basic program. First, you have to save your work. Then, Open File menu and look for Make Project1.Exe. The program itself will compile your work and will make an .EXE file for your project.

File menu.

Make Projectname.Exe command

It displays folders where you want to put your .EXE file. By clicking the OK button, the program starts to compile your project.

Page 23: Building a Visual Basic

QUITTING VISUAL BASICThere are three ways to quit Visual Basic.

Click the File menu, then the Exit command.

Click the X button or the Close button of the program.

The Close button or the X button of the program

The Exit command in the File menu

Press the Alt key together with the Q key on the keyboard.