Академический Документы
Профессиональный Документы
Культура Документы
Event?
Event processor
Basic Code
Basic Code
Basic Code
Event procedure
Form 2 (.FRM)
Control 1 Control 2 Control 3
Form 3 (.FRM)
Control 1 Control 2 Control 3
Module 1 (.BAS)
Start a new project Open an existing project Select a list of recently opened programs.
Project
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). Note:
By clicking on the Standard EXE icon to go into the actual VB programming environment.
2
PLP College of Computer Studies
- rct- 6/06
Figure 2: The Visual Basic Environment The Visual Basic Environment consists of the: 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.
The Form Window is central to developing Visual Basic applications. It is where you draw your
application. (see figure 3)
Note:
The most prominent element of the Visual Basic program development environment is the blank
Form that is displayed in the middle of the screen. A Visual Basic Form Window is a window in which you design the user interface for your application
4
PLP College of Computer Studies
- rct- 6/06
The Toolbox is the selection menu for controls used in your application
Pointer Label Frame Check Box Combo Box Horizontal Scroll Bar Timer Directory List Box Shapes Image Box Object Linking Embedding
Picture Box Text Box Command Button Option Button List Box Vertical Scroll Bar Drive List Box File List Box Lines Data Control
5
PLP College of Computer Studies
- rct- 6/06
vsb tmr
vsbTrebleControl tmrDelay drvHardDiskContent dirTree filProgramFiles shpCircle linBorder imgClouds datEmployeeRecords oleSpreadsheet
6
Drive List drv Box Directory dir List Box File List Box fil Shape Line Image Data OLE shp lin img dat ole
Figure 5: Sample Form Layout/Interface ` Program Listing 1: Example of Text Box Controls Private Sub Command1_Click() To add the values in text box 1 and text box 2 Sum = Val(Text1.Text) + Val(Text2.Text) To display the answer on label 1 Label1.Caption = Sum End Sub
7
PLP College of Computer Studies
- rct- 6/06
You can change its caption in the properties window and also at runtime. Please refer to
Program Listing 1 and Figure 5 for the usage of label.
Object Box
The Properties Window is used to establish initial property values for objects. The drop-down List
11
PLP College of Computer Studies
- rct- 6/06
Properties
Setting Box
box at the top of the window lists all objects in the current Form. Two views are available;
shows where (upon program execution) your Form will be displayed relative to your monitors screen.
The Project Window displays a list of all Forms and modules making up your application. You
can also obtain a view of the Form or Code windows (window containing the actual Basic coding) from the Project window.
View Object
View Code
Toggle Folders
o Procedure box list the event procedures to which the selected object is capable of
responding Input box
Before typing the Form name makes sure that the Save File As dialog box (as shown besides) is displayed in your screen. Click Save button Then, displayed Save Project As dialog box Type the project name (follow the PEPJT01RICHARDGWAPO
4.
5.
6.
project
Format
filename)
ex:
Note:
Before typing the project name makes sure that the Save Project As dialog box (as shown besides) is displayed in your screen. Click Save button
7.
Note: To RUN/EXECUTE the program/application Click on Run menu then, click Start - OR Press F5 function key OR Click Start button
14
PLP College of Computer Studies
- rct- 6/06
15
PLP College of Computer Studies
- rct- 6/06
Steps: 1. Launch Visual Basic application/program; Displays New Project as shown (see figure 1) Be sure that Standard EXE is being selected 2. Click Open button Then, displays Form Window as shown below (see figure 2)
3.
Add the following controls as shown (see figure 3) Labels Text Box Combo Box List Box Frame (Course & Subjects enrolled) Command Button Click Save button or File Save Project or File Save Project As Follow the project name and Form name Format Apply necessary enhancement: Font, Font Size, Font Color, Changing Caption etc.
4.
5.
16
PLP College of Computer Studies
- rct- 6/06
Practice Exercise #2: Inserting/Adding Form in the existing project & using MsgBox
Project Name Form Name Steps: 1. Exercise project 1) 2. 3. 4. Click <Project> menu then select <Add Form> New Form will be added in your existing project (see Figure 1) Add 2 controls/objects in your new Form as (see Figure 2) Change Properties: Form = Caption: Using MsgBox Label1 = Name: lblMsg Caption: Message goes here Command1 = Name: cmdTest Caption: Test (see Figure 3) Apply codes to youre application: Click <View> menu then <Code> OR select View Code button project explorer window OR double click the Test Button Then, displayed the Code Window (see Figure 4) : PEPJT01<FirstNameLastName> : PE02FRM02<FirstNameLastName> Example: PEPJT01RichardGwapo Example: PE02FRM02RichardGwapo
shown
Figure 1: New Form in Project Explorer Window
5.
from
Figure 2: Form2 with Label1 & Command1 controls
Object
6.
In the Event combo box (General) select cmdTest and in the Procedure Figure 3: combo box (Declarations) select click (see Figure 5) Note: The default event for a CommandButton Object is Click (see Figure 6)
Modified Form2
Figure 6: Code Window for cmdTest Object with Procedure click Figure 5: Object cmdTest
7.
Inside the procedure Type the source code as shown: (see Figure 7)
17
PLP College of Computer Studies
Figure 7: Sample procedure/code/command/message of cmdTest Object - rct- 6/06
18
PLP College of Computer Studies
- rct- 6/06
Note: - In object programming, the access to method and properties is done by joining the name of the object and the property (or method) with a dot. - The message is enclosed with double quotation marks ( ) because the text is a string constant. Note: Before running the program you need to change the project properties to run Form2 otherwise, Form1 will always be the Form to be executed. o Click <Project> menu then click <Project Properties> then, displays the Project Properties dialog box/window (see Figure 8) o Select Form2 (or the Form that you wants to run/execute) from the Startup objects. Then, click OK button Try to run the program and see the output : (see Figure 9)
8.
9.
Creating event/procedure for the object Form. Click the View Code button or simply Double click the Form to work on with the code window (see Figure 10) Notice that the default event for Form is not Click but Load. Select Click and type the procedure/code as shown (see Figure 11)
10. 11.
Save and Run the program to test the output. To add Message Box command: Modify cmdTest procedure and add this line of command (see Figure 12)
19
PLP College of Computer Studies
20
PLP College of Computer Studies
- rct- 6/06
Arithmetic Operators:
Operator ^ * / Mod \ + or & Mathematical function Exponential Multiplication Division Modulus(return the remainder from an integer division) Integer Division(discards the decimal places) String concatenation Example 2^4=16 4*3=12 12/4=3 15 Mod 4=3 19\4=4 "Visual"&"Basic"="Visual Basic"
Program Listing 2: Example of Mathematical/Arithmetic Operators 1 Dim firstName As String Dim secondName As String Dim yourName As String Private Sub Command1_Click() firstName = Text1.Text secondName = Text2.Text yourName = secondName + " " + firstName Label1.Caption = yourName End Sub In this example Program Listing 3, three variables are declared as string. For variables firstName and secondName will receive their data from the users input into textbox1 and textbox2, and the variable yourName will be assigned the data by combining the first two variables. Finally, yourName is displayed on Label1.
Type Conversions
Values are classified as either String or Numeric. Both values can be manipulated separately. For example, we can concatenate two strings or add two numbers but we cannot add or concatenate a string and a number. In many occasions, we need to convert a string format to numeric format for computation and a numeric format to string format for manipulation. To do this, we can use the STR( ) and VAL( ) functions. STR( ) converts a number to string VAL( ) converts a string to number
21
PLP College of Computer Studies
- rct- 6/06
C = 20 AGE = 25
Z= Y + STR(AGE) Output of Z
How old are you 25
D = A + B + VAL(C) Output of D
35
Program Listing 3: Example of Mathematical/Arithmetic Operators 2 Dim number1, number2, number3 as Integer Dim total, average as variant Private sub Form_Click( ) number1=val(Text1.Text) number2=val(Text2.Text) number3= val(Text3.Text) Total=number1+number2+number3 Average=Total/5 Label1.Caption=Total Label2.Caption=Average End Sub In the Program Listing 4, three variables are declared as integer and two variables are declared as variant. Variant means the variable can hold any numeric data type. The program computes the total and average of the three numbers that are entered into three text boxes.
22
PLP College of Computer Studies
- rct- 6/06
Rating:
Retrieve/Open your PEPJT01 (Practice Exercise Project 1) Click <Project> menu then select <Add Form> Sample Form Layout/Interface: (see Figure 1) Add the following objects/controls as shown Change properties: Form = Caption: Mathematical Operations Text1 = Name: txtMyName Text2 = Name: txtYourName Text3 = Name: txtMyAge Text4 = Name: txtYourAge Label1 = Name: lblOurName Label2 = Name: lblSumAge Command1 = Name: cmdOurName Command2 = Name: cmdSumAge Please type the given names as it is to avoid errors in program codes.
Note: 6.
Double Click NAME button then, type the following line of codes.
cmdOurName Event: Private Sub cmdOurName_Click() ' Displaying "Welcome MyName and YourName value" lblOurName.Caption = "Welcome! " + txtMyName.Text + " " + txtYourName.Text End Sub 7. Double Click SUM button then, type the following line of codes.
cmdSumAge Event: Private Sub cmdSumAge_Click() ' Displaying MyAge and YourAge lblSumAge.Caption = Val(txtMyAge.Text) + Val(txtYourAge.Text) End Sub 8. 9. Save and run the program to test the output (see Figure 2) Apply necessary enhancement to your Form (optional)
Single Quotation ( )
23
PLP College of Computer Studies
- rct- 6/06
24
PLP College of Computer Studies
- rct- 6/06
SAMPLE OUTPUT:
1)
2)
3)
4)
5)
6)
Caption: Using Mathematical Operators] Argument 1 Argument 2 Sum (+) Difference () Product (*) Quotient (/) Text Box: 1. 2. 3. 4. 5. 6. Name: txtArg1 Name: txtArg2 Name: txtSum Name: txtDiff Name: txtProd Name: txtQuo Alignment: Right Alignment: Right Alignment: Right Alignment: Right Alignment: Right Alignment: Right
Command Button:
25
PLP College of Computer Studies
- rct- 6/06
Caption: SIMPLE CALCULATOR ContolBox: False Note: If ControlBox is set to False the contol menu box will be removed along with the minimize, maximize and close button (see the sample output below)
5) 6)
7) 8)
9)
Text Box: 1. Name: txtArg1 2. Name: txtArg2 3. Name: txtResult Enabled: False Caption &Clear &Add &Subtract &Multiply &Divide E&xit Description To clear all entries (text box) To add two numbers To subtract two numbers To multiply two numbers To divide two numbers To end/close the program
Note: You can also use the following keys Alt + C Clear Alt + A Add Alt + M Multiply Alt + S Subtract Alt + D Divide Alt + X Exit ***** The effect of & symbol is to have a short-cut key for the following command button objects stated above. Example: You type Effect: &Clear Clear
- rct- 6/06
26
PLP College of Computer Studies
27
PLP College of Computer Studies
- rct- 6/06
-922,337,203,685,477.5808 to 922,337,203,685,477.5807 +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use +/- 7.9228162514264337593543950335 (28 decimal places).
In addition, we need to enclose string literals within two quotations and date and time literals within two # sign. Strings can contain any characters, including numbers. Examples: memberName="Turban, John." TelNumber="1800-900-888-777" LastDay=#31-Dec-00# ExpTime=#12:00 am#
Scoping of variables:
Variables can be Local to the procedure or Local to the whole Form. Local variables to a procedure are declared at the start of a procedure and can be used only in that procedure. Local variables to the whole Form, on the other hand, can be defined at the Declaration section of the General object found in the code window. See figure below.
Variables that are Local to the procedure are declared after DIM reserve word while variables that
are Local to the Form are declared either by using DIM or PRIVATE reserve words. See figure 12.
29
PLP College of Computer Studies
- rct- 6/06
Loc al Variables
Managing Variables
Variables are like mail boxes in the post office. The contents of the variables changes every now and then, just like the mail boxes. In term of VB, variables are areas allocated by the computer memory to hold data. Like the mail boxes, each variable must be given a name. To name a variable in Visual Basic, you have to follow a set of rules.
Constant
Refers to fixed values. Like variable, constants can be Local for a specific procedure, Local for the entire Form, Private for a Module, and Public for the whole project. The reserve word Const is use to declare a constant see Figure 13.
Figure 13: Constant Variable Declarations Examples: Valid and Invalid Variable Names Valid Name My_Variable ThisDate Long_Name_Can_beUSE Invalid Name My.Variable 7Day Mine&Yours *& is not acceptable
30
PLP College of Computer Studies
- rct- 6/06
Declaring Variables
In Visual Basic, one needs to declare the variables before using them by assigning names and
data types. They are normally declared in the general section of the codes' windows using the Dim statement. Format: Dim variableNmae as DataType
Example: Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date Note: You may also combine them in one line , separating each variable with a comma, as follows: Example: Dim password As String, yourName As String, firstnum As Integer,............. If data type is not specified, VB will automatically declare the variable as a Variant. For string declaration, there are two possible formats, one for the variable-length string and another for the fixed-length string. For the variable-length string, just use the same format as example 5.1 above. However, for the fixed-length string, you have to use the Format: Dim VariableName as String * n, (where n defines the number of characters the string can hold.) Example: Dim yourName as String * 10 yourName can holds no more than 10 Characters.
31
PLP College of Computer Studies
- rct- 6/06
Randomize Seed
If you use the same Seed each time your run your application, the same sequence of random numbers will be generated. To ensure you get different numbers everytime you use your application (preferred for games), use the Timer funtion to seed the generator: Randomize Timer Note: Place Randomize Timer statement in the Form_Load event procedure Examples: 1. To roll a six-sided die, the number of spots would be computed using: Answer: NumberSpots = Int(6 * Rnd) + 1 2. To randomly choose a number between 100 and 200, use: Answer: Number = Int(100 * Rnd) + 100
33
PLP College of Computer Studies
- rct- 6/06
Practice Exercise #4: Stopwatch Application (Using several Visual Basic Functions)
Project Name Form Name : PEPJT02<FirstNameLastName> : PE04FRM01<FirstNameLastName> Example: PEPJT02RichardGwapo Example: PE04FRM01RichardGwapo
Project 2 / Form 1
Steps: 1. 2. 3. Create a New Project Add the following controls as shown (see Figure 1) (place three command buttons and six labels on the form) Set Properties of the form, three buttons and six labels: Form1: BoarderStyle 1-Fixed Single Caption Stopwatch Application Name frmStopWatch Command1: Caption &Start Timing Name cmdStart Command2: Caption &Start Timing Name cmdEnd Command3: Caption E&xit Name cmdExit Label1: Caption Start Time Label2: Caption End Time Label3: Caption Elapsed Time Label4: BoarderStyle 1-Fixed Single Caption [Blank] Name lblStart Label5: BoarderStyle 1-Fixed Single Caption [Blank] Name lblEnd Label6: BoarderStyle 1-Fixed Single Caption [Blank] Name lblElapsed
4. 5. 6.
(See Figure 2: Modified form properties) Stopwatch Application Attaching Code Double click anywhere on the form to open the code window. Or select View Code from the project window. Select specific object then type the following line of codes.
34
PLP College of Computer Studies
- rct- 6/06
35
PLP College of Computer Studies
- rct- 6/06
Rating:
Project 2 / Form 2
Steps: 1. 2. 3. Retrieve/Open your PEPJT02 (Practice Exercise Project 2) Add Form2 and add the following controls as shown (see Figure 1) (place 5 labels and 1 timer) Set Properties: Form2: Caption My Calendar BorderStyle 1-Fixed Single Timer: Name timDisplay Interval 1000 Figure 1: Sample Form Layout (Calendar) Label1: Name lblDay Caption Sunday FontName Times New Roman FontStyle Bold FontSize 24 Label2: Name lblTime Caption 00:00:00 PM FontName Times New Roman FontStyle Bold FontSize 24 Label3: Figure 2: Sample Run (Output) Name lblYear Alignment 2 Center Caption 2004 FontName Times New Roman FontStyle Bold FontSize 24 Label5: Label4: Name lblMonth Name lblNumber Alignment 2 Center Alignment 2 Center Caption July Caption 18 FontName Time New Roman FontName Arial FontStyle Bold FontStyle Bold FontSize 72 FontSize 24
36
PLP College of Computer Studies
- rct- 6/06
37
PLP College of Computer Studies
- rct- 6/06
Note: You can also compare strings with the above operators. However, there are certain rules to follows: Upper case letters are less than lowercase letters, "A"<"B"<"C"<"D".......<"Z" and number are less than letters.
Logical Operators
In addition to conditional operators, there are a few logical operators which offer added power to the VB programs. Operator And or Xor Not Meaning Both sides must be true One side or other must be true One side or other must be true but not both Negates truth
38
PLP College of Computer Studies
- rct- 6/06
39
PLP College of Computer Studies
- rct- 6/06
we use the keyword Is here to impose the conditions. This is generally used for numeric data.
Program Listing 7: Example of Selective Structure 3 Dim mark As Single Private Sub Compute_Click() 'Examination Marks mark = mrk.Text Select Case mark Case 0 to 49 comment.Caption = "Need to work harder" Case 50 to 59 comment.Caption = "Average" Case 60 to 69 comment.Caption = "Above Average" Case 70 to 84 comment.Caption = "Good" Case Else comment.Caption = "Excellence" End Select End Sub
41
PLP College of Computer Studies
- rct- 6/06
Rating:
Sample Form: Specs: 3 Labels: Name: lblNum1 Name: lblNum2 Name: lblNum3 FontSize: 20 1 CommandButton: Name: cmdSpin
Sample Output:
cmdSpic_Click Event: Private Sub cmdSpin_Click() Dim Num1 As Integer Dim Num2 As Integer Dim Num3 As Integer Num1 = Int(Rnd * 10) Num2 = Int(Rnd * 10) Num3 = Int(Rnd * 10) lblNum1.Caption = Format(Num1, "#0") lblNum2.Caption = Format(Num2, "#0") lblNum3.Caption = Format(Num3, "#0") If Num1 = 7 Or Num2 = 7 Or Num3 = 7 Then MsgBox "You win" Else MsgBox "Sorry " End If End Sub
PROJECT 2: FORM #1: 3 Digits Lotto Program Instructions: 1. Your bet should not be less than 10 pesos, otherwise it will prompt you with Enter minimum amount of Php 10.00 2. Enter single digits for each of the text box 3. If the content of the text box are all equal to the winning numbers (in order) it will prompt you with You have won 1 MILLION PESOS JACKPOT (see sample Message box below), otherwise it will prompt you with Sorry better luck next time 4. But, if the content of the text box are equal to the winning numbers (but not in order) it will prompt you with You have won a consolation prize of ____. The consolation prizes will depend to the BET amount (Example: Bet is 100, multiply your bet to 1,000.00 to get the consolation amount.)
43
PLP College of Computer Studies
- rct- 6/06
Sample Output:
Formula: Semestral Grade = 25% of Prelim + 25% of Midterm + 50% of Final Point Equivalent Table Sem. Grade Equivalent 98 100 1.00 95 97 1.25 92 94 1.50 89 91 1.75 86 88 2.00
Additional Logical Problems: For New Entry Button: Upon clicking displays this message box If Yes will clear all entries else nothing happen For Compute Button: Upon clicking displays this message box If Yes will display the results else nothing happen For Exit Button: Will close the program/application
44
PLP College of Computer Studies
- rct- 6/06
An optional keyword used to signify the transfer of program control to another Procedure Name The name of the procedure to pass control to. Argumentlist The variables or expression to pass to another procedure.
Introduction to VB Functions I
Functions are similar to normal procedures but the main purpose of the functions is to accept certain inputs and pass them on to the main program to finish the execution. They are two types of function, the built-in functions(or internal functions) and the functions created by the programmers. The general format of a function is: functionName(arguments) where arguments are values that are passed on to the functions. In this lesson, we are going to learn two very basic but useful internal functions, i.e. the MsgBox( ) and InputBox ( ) functions.
The first argument, Prompt - will display the message in the message box. The Style Value will determine what type of command buttons appear on the message box. The Title argument will display the title of the message board.
45
PLP College of Computer Studies
- rct- 6/06
Note: We can use named constant in place of integers for the second argument to make the programs more readable. In fact, VB6 will automatically shows up a list of names constant where you can select one of them. Example: yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu") and yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu") are the same. yourMsg is a variable that holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the users. It has to be declared as Integer data type in the procedure or in the general declaration section. Return Values and Command Buttons Value Named Constant 1 vbOk 2 vbCancel 3 vbAbort 4 vbRetry 5 vbIgnore 6 vbYes 7 vbNo Example: Button Clicked Ok button Cancel button Abort button Retry button Ignore button Yes button No button
When a user click on the test button, the image like the one shown in Figure 15 will appear. As
the user click on the OK button, the message "Testing successful" will be displayed and when he/ she clicks on the Cancel button, the message "Testing fail" will be displayed.
o To make the message box looks more sophisticated, you can add an icon besides the message.
There are four types of icons available in VB as shown in the table below: Value 16 32 48 64 Named Constant vbCritical vbQuestion vbExclamation vbInformation Icon
47
PLP College of Computer Studies
- rct- 6/06
You could draw the same Interface as in example program listing 8, but modify the codes
as follows: Program Listing 9: Example of MsgBox with Yes, No & Cancel button Private Sub test2_Click() Dim testMsg2 As Integer testMsg2 = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message") If testMsg2 = 6 Then display2.Caption = "Testing successful" ElseIf testMsg2 = 7 Then display2.Caption = "Are you sure?" Else display2.Caption = "Testing fail" End If End Sub
An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. Format: myMessage=InputBox(Prompt, Title, default_text, x-position, y-position) Where:
myMessage - is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows: o Prompt - The message displayed normally as a question asked. o Title - The title of the Input Box. o default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to key in. o x-position and y-position - the position or the coordinate of the input box.
48
- rct- 6/06
Figure 17: Sample Interface for InputBox function Program Listing 10: Example of an InputBox function The procedure for the OK button Private Sub OK_Click() Dim userMsg As String userMsg = InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700) If userMsg <> "" Then message.Caption = userMsg Else message.Caption = "No Message" End If End Sub
Note:
When a user click the OK button, the input box as shown in Figure 18 will appear. After user
entering the message and click OK, the message will be displayed on the caption, if he click Cancel, "No message" will be displayed.
Rating:
Project 2 / Form 3
Steps: 1. 2. Retrieve/Open your PEPJT02 (Practice Exercise Project 2) Add Form3 and add the following controls as shown (see Figure 1) (place 5 labels
Label4
Label7
Label5
and 1 timer) 3. BorderStyle Caption Name Caption Enabled Name Caption Name FontName FontSize MaxLenght Name Alignment FontName FontSize Set Properties: Form3 1 Fixed Single Flash Card Addition Command1 cmdNext &Next Problem False Command2 cmdExit E&xit Text1 txtAnswer Arial 48 2 Label1 lblNum1 2 Center Arial 48 50
PLP College of Computer Studies
- rct- 6/06
Note: After entering your answer press enter key to check if the answer is correct or not.
General Declaration Option Explicit Dim Sum As Integer Dim NumProb As Integer, NumRight As Integer cmdExit Event: Private Sub cmdExit_Click() End End Sub cmdNext Event: Private Sub cmdNext_Click() 51
PLP College of Computer Studies
- rct- 6/06
53
PLP College of Computer Studies
- rct- 6/06
PROJECT #3: FORM #1: SAMPLE FORM LAYOUT PRICES: Size: Small Php 100.00 Medium 150.00 Large 200.00 Crust Type: Thin Crust same as size prices Thick Crust Plus 50% of size prices Drinks: Soft Drinks Php 20.00 Fruit Juice 15.00 Coffee/Chocolate 25.00 Extra Toppings: Plus Php 10.00 per extra toppings
Build Pizza Button: will display message box Your Pizza (see sample above) Clear Toppings Button: will uncheck all check boxes. Compute Button: will display all values under Amount frame Clear All Button: will clear all entries Exit Button: will close the program. 54
PLP College of Computer Studies
- rct- 6/06
PROJECT #3: FORM #2: SAMPLE FORM LAYOUT EMPLOYEE PAYROLL COMPUTATION
Logical Problems & Formulas: Position Code Rate/Day A 500.00 B 400.00 C 300.00
Rate 10% 5% 5%
Basic Pay SSS Rate 10,000 & above 7% 5,000 9,999 5% 1,000 4,999 3% below 1,000 1%
Basic Pay = No. of Days Worked * Rate/Day SSS Contribution = Basic Pay * SSS Rate Withholding Tax = Basic Pay * Tax Rate Net Pay = Basic Pay (SSS Contribution + Withholding Tax) Upon choosing the desired Employee Number, it will automatically display the Employee Name. For Compute Button, will compute the Basic Pay, SSS Contribution, Withholding Tax and Net Pay 55
PLP College of Computer Studies
- rct- 6/06
56
PLP College of Computer Studies
- rct- 6/06
PROJECT 4: FORM #1: Hotel Billing System Sample Form and sample output:
Room Type
Rate/day
Regular 100.00 De Luxe 300.00 Suite 500.00 Regular 200.00 Problem: Double De Luxe 500.00 Compute for Total Amount based on the date of Suite 800.00 check-in and check-out. Regular 500.00 Family SelectLuxe capacity, 750.00 and payment room type De room type before clicking Compute button otherwise, will Suite 1,000.00 prompt you with No selected room capacity or No selected room type or No selected type of payment 57
PLP College of Computer Studies
- rct- 6/06
Looping
Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping .
Do Loop
Format: a) Do While condition Block of one or more VB statements Loop b) Do Block of one or more VB statements Loop While condition c) Do Until condition Block of one or more VB statements Loop Do Block of one or more VB statements Loop Until condition Example: Do while counter <=1000 num.Text=counter counter =counter+1 Loop Note: The above example will keep on adding until counter >1000. The above example can be rewritten as Do num.Text=counter counter=counter+1 Loop until counter>1000
d)
58
PLP College of Computer Studies
- rct- 6/06
(b)
(c)
59
PLP College of Computer Studies
- rct- 6/06
Introduction to VB Functions II
Creating Your Own Functions
Format: Public Function functionName (Arg As dataType,..........) As dataType or Function functionName (Arg As dataType,..........) As dataType Note:
Public indicates that the function is applicable to the whole program and
Private indicates that the function is only applicable to a certain module or procedure. Example: The following program will automatically compute examination grades based on the marks that a student obtained.
Figure 11: Sample Interface Program Listing 19: Example of Functions declaration Declaration of Function grade Public Function grade(mark As Variant) As String Select Case mark Case Is >= 80 grade = "A" Case Is >= 70 grade = "B" Case Is >= 60 grade = "C" Case Is >= 50
60
PLP College of Computer Studies
- rct- 6/06
61
PLP College of Computer Studies
- rct- 6/06
Rating:
Display this message box if you failed to enter the password correctly (three (3) attempts only) Click OK: to quits the application/program.
62
PLP College of Computer Studies
- rct- 6/06
63
PLP College of Computer Studies
- rct- 6/06
PROJECT #5: FORMS: SAMPLE FORM LAYOUT Upon running the program displays the 1st form, which is the PASSWORD ENTRY FORM 1:
Note: No limit for password, if incorrect password will prompt the user to RETRY & CANCEL and user name should have an entered value otherwise will prompt the user to enter value for user name. If the password is correct will display the 2nd form (MENU) FORM 2: (with File & Help Menu) Main Menu: File Help Sub Menus: Transaction About Exit <File> <Transaction>, will display the Transaction Form: For TRANSACTION Form: Initially; BUTTONS: New, Clear, Close & Compute are all in disabled layout (as shown below) <File> <Exit>, will close the program If Transaction is clicked, displays 3rd Form: (as shown below)
64
PLP College of Computer Studies
- rct- 6/06
User Name:
Railee Darrel
Appliances/Items Unit Price Electric Fan 500.00 Refrigerator 9,000.00 Washing Machine 4,000.00 Oven Toaster 550.00 Flat Iron 350.00 VCD Player 1,200.00 Mini Component 3,500.00
Appliances/Items Unit Price Television 5,000.00 Air Conditioner 10,000.00 Rice Cooker 600.00 Electric Stove 1,000.00 Electric Pot 800.00 Karaoke 2,500.00 Microwave Oven 3,000.00
Logical Problems: Double click Appliance item (List Box) then the selected item will be displayed to the Appliance Sold (List Box) and UnitPrice (List Box) (see example below) Note: Be sure that you have enter value in the Quantity If quantity is less than or equal to zero (0) it will prompt the user to input quantity. Amount Text Box will be based on the Appliance Unit Price * Quantity Example: Appliance Sold: Electric Fan, Unit Price: 500, Quantity: 2 (see example below) Sub-total Text Box will be based on the total item sold. Example: Appliance Sold: Air Conditioner 10,000.00 X 1 and Electric Fan 500.00 X 2 (see example below); Choose Type of Payment: Cash or Charge then, will enable Compute button If Compute button is clicked; for CASH Total Amount = Sub-total less 5% discount
65
PLP College of Computer Studies
- rct- 6/06
Then, will enable buttons; New, Clear & Close Enter value for Amount Tendered: Amount Tendered should be exact amount or should be greater than Total Amount otherwise, will prompt the user to enter valid amount. Click Change button to display the amount change. For New button, will clear all entries For Clear button, will Quantity, Appliance Sold, Unit Price and Type of Payment only. If Compute button is clicked; for CHARGE Total Amount = Sub-total + 5% additional charge For Close button, will close the transaction window only.
User Name:
Richard Gwapo
66
PLP College of Computer Studies
- rct- 6/06
FORM 4: If Help then About is clicked, will display a Form About the program If button OK is clicked will close the form window.
67
PLP College of Computer Studies
- rct- 6/06