Com 211 Oobasicvisual Basic Programming Practical
Com 211 Oobasicvisual Basic Programming Practical
VOCATIONAL EDUCATION
REVITALISATION PROJECT-PHASE II
NATIONAL DIPLOMA IN
COMPUTER TECHNOLOGY
YEAR I SEMESTER II
PRACTICAL
Version 1: December,2008
1
WEEK 1-LAB ............................................................................................................................................... 3
2
WEEK 1
LAB
1. State the stages of systems development cycle?
− ………………………………….
− ………………………………….
− ………………………………….
− ………………………………….
2. Start Visual Basic 6 and write the steps you followed to achieve that ?
− ………………………………….
− ………………………………….
− ………………………………….
− ………………………………….
3. Itemize the labeled parts of the Visual Basic environment as seen in the
figure below ?
1- ………………………………….
2- ………………………………….
3- ………………………………….
4- ………………………………….
5- ………………………………….
4. Get online help about picture box control ? and write the steps you
followed to achieve that ?
− ………………………………….
− ………………………………….
5. And, Get help about BackColor for the control ? and write the steps you
followed to achieve that ?
3
6. How do you display the contents of the help of VB ?
− ………………………………….
− ………………………………….
7. Open the “hello” project form from your own folder ? and write steps you
followed to achieve that ?
− ………………………………….
− ………………………………….
− ………………………………….
− ………………………………….
8. Create new project with application wizard from with the following
specifications:
10. Save the above project with the name (wizard project ) in your folder? and
write steps ?
− ………………………………….
− ………………………………….
11. Create executable File for the above project ? and write steps ?
− ………………………………….
− ………………………………….
− ………………………………….
− ………………………………….
4
WEEK 2
LAB
During This Lab we want to create a new project with the name “week2_1” and
the final form look like the following figure and when you click on the button "Click
Here" the text on the form will change with “Well Done”.
5
19. Click the control named Label1 to display the label's properties in the
Properties window.
20. Click the Properties window's Caption entry.
21. Type Have a happy day! As you type, you'll see the label's caption
change.
22. Click the Properties window's Font entry. A font name doesn't appear, but
a set of ellipsis does. Ellipsis after a property value always indicates that
you can select from a list of choices rather than type the value directly into
the Properties window.
23. Click the Font entry's ellipsis to display the Font dialog box. A font
supports multiple properties, so you must use the dialog box to specify the
font name, size, and style.
24. Select the Courier New font, with a Bold style and a 36-point size.
25. Close the Font dialog box. Resize the label's caption to see more of it.
Although you can drag the label's edges, you'll instead enter location and
size values directly in the Properties window so that your application
matches the one in this book.
26. Set these Properties window values: Left, 1,320; Height, 1,695; Top, 120;
and Width, 4,695. You can now read the new label's caption, but the
caption is still left-justified. Expand the Properties window's width if you
need more room to read the property values.
27. Click the label's Alignment property. Click the drop-down list box arrow to
open the list box and you'll find three values: Left Align, Right Justify, and
Center Justify. Select Center Justify, and the label straightens up nicely.
6
28. Select the form's command button, click the Caption property, and then
type Click Here for the caption.
29. Double click the command button, write this Code in the click event
procedure
30. Save your project with the name “week2_1” in your folder.
7
WEEK 3
Lab
During This Lab we want to create a new project with the name “week3” and the
final form look like the following figure and when you click on the Label “1” the
text "4" will change with “Welcome you to our Lab”.
4. Double click the Label with the caption “1”, write this Code in the click
event procedure
Label4.Caption = “Welcome you to our Lab”
5. Save your project with the name “week3” in your folder.
Practical Exercise:
1.Describe the various types of variables in Visual Basic
2. State the rules for forming variable name in Visual basic
8
Week 4
Lab
During This Lab we want to create a new project with the name “Week4” to
change the label properties and the final form look like the following figure. Each
button have a caption describe its effect.
9
Command7 Enable Label
Command8 Disable Label
36. Double-click on the buttons and write the code as in the following table.
10
WEEEK 5
Lab 1
During This Lab we want to create a new project with the name “week5-1” and
the final form look like the following figure. Label1 display the command button
which has the focus.
11
Private Sub Command1_GotFocus()
Label1.Caption = "cmdTopLeft has the Focus"
End Sub
End Sub
End Sub
11. Save your project with the name “week5_1” in your folder.
12. Run the program
12
Week 6
Lab 2
During This Lab we want to create a new project with the name “week5_2” and
the final form look like the following figure.
13
13. Create new project
14. In the New Project box, Click Standard.EXE and then Click OK.
15. Add command button controls and change its caption to "Swap pictures".
16. Add 2 picture box button and change its properties to as in the following
table
17. Add 3 image button and change its properties to as in the following table
19. Save your project with the name “week5_2” in your folder.
20. Run the program
14
WEEK 7
Lab 1
During This Lab we want to create a new project with the name “Week6_1” to
display your full name you entered and to convert temperature from Celsius to
Fahrenheit and. The main form look like this form
15
40. Double-click the "Convert to Fahrenheit " Command Button and write this
code
41. Save your project with the name “week6_1” in your folder.
42. Run the program
16
WEEK 8
Lab 2
During This Lab we want to Complete a project “Week6_2” to enter degree and
decide your result and your grade. The main form look like this form
degree = Val(Text1.Text)
17
Label5.Caption = "A-"
Case 8
Label5.Caption = "B"
Case 7
Label5.Caption = "B-"
Case 6
Label5.Caption = "C"
Case 5
Label5.Caption = "C-"
Case Else
Label5.Caption = "F"
End Select
45. Double-click the " text1" Command button and write this code
46. Save your project with the name “week6_2” in your folder.
47. Run the program
18
WEEK9
Lab 1
During This Lab we want to Complete a project “Week7_1” to enter a text and
format its font. The main form look like this form
If Check1.Value = 1 Then
Text1.FontBold = True
19
Else
Text1.FontBold = False
End If
50. Double-click the " Italic" Command button and write this code
If Check2.Value = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
51. Double-click the " Underline" Command button and write this code
If Check3.Value = 1 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
52. Double-click the " Small" Command button and write this code
Text1.FontSize = 12
53. Double-click the " Medium" Command button and write this code
Text1.FontSize = 18
54. Double-click the " Large" Command button and write this code
Text1.FontSize = 24
55. Save your project with the name “week7_1” in your folder.
56. Run the program
20
WEEK10
Lab 2
During This Lab we want to Complete a project “Week7_2” to display the
numbers from 5 to 20 and numbers from 15 to 5. The main form look like this
form
21
57. Open project week6_1
58. Double-click the " Start Do loop" Command button and write this code
counter1 = 5
Do While counter1 < 21
Label1.Caption = Label1.Caption + " " + Str(counter1)
counter1 = counter1 + 1
Loop
59. Double-click the " Clear Do text" Command button and write this code
Label1.Caption = ""
60. Double-click the " StartFor loop" Command button and write this code
counter1 = 5
Do While counter1 < 21
Label1.Caption = Label1.Caption + " " + Str(counter1)
counter1 = counter1 + 1
Loop
61. Double-click the " Clear For text " Command button and write this code
Label2.Caption = ""
62. Save your project with the name “week7_2” in your folder.
16 Run the program
22
63. Open project week8_1
64. Double-click on the form and write the following code for four procedures
in the general part of the form
- Procedure ( calc_sum )
- Procedure with arguments (calc_difference )
- Function (calc_multiplication )
- Function with arguments (calc_Division )
Sub calc_sum()
If Text1.Text = "" Or Text2.Text = "" Then
MsgBox "You must enter both first and second numbers"
Exit Sub
End If
Label3.Caption = Val(Text1.Text) + Val(Text2.Text)
End Sub
23
End Sub
End Function
End Function
65. Double-click the " + " Command button and write this code
calc_sum
66. Double-click the " - " Command button and write this code
67. Double-click the " * " Command button and write this code
Label3.Caption = calc_multiplication
68. Double-click the " / " Command button and write this code
69. Save your project with the name “week9_1” in your folder.
70. Run the program
24
WEEK11
Lab 2
During This Lab we want to Complete a project “Week9_2” to simulate Supplied
function. The main form look like this form
25
1. Open project week8_2
2. Double-click "the Integer ( Int )" Command button and write this
code
3. Double-click " Square Root ( Sqr )" Command button and write this
code
26
4. Double-click " Absolute ( Abs )" Command button and write this
code
6. Double-click " Length of text" Command button and write this code
Label8.Caption = Len(Text2.Text)
Label8.Caption = UCase(Text2.Text)
8. Double-click " convert text to lower case Command button and write
this code
Label8.Caption = LCase(Text2.Text)
9. Double-click " Now Function" Command button and write this code
Label11.Caption = Now
10. Double-click " Date Function" Command button and write this code
Label11.Caption = Date
11. Double-click " Time Function" Command button and write this code
Label11.Caption = Time
12. Save your project with the name “week8_2” in your folder.
13. Run the program
27
WEEK 12
Lab 1
During This Lab we want to Complete a project “Week9_1” to add the numbers
(0,2,4,………36,38 ) to one dimension array and display it in a text box. The
main form look like this form
Dim i As Integer
Dim iMyArray(19) As Integer
Dim BeginMsg As String
Dim MidMsg As String
Dim LoopMsg As String
Dim FullMsg As String
For i = 0 To 19
iMyArray(i) = i * 2
Next i
28
BeginMsg = "The element is: "
MidMsg = "The value is: "
FullMsg = "The elements of array are : " & vbCrLf
For i = 0 To 19
LoopMsg = LoopMsg & BeginMsg & i & "," & vbTab
LoopMsg = LoopMsg & MidMsg & iMyArray(i)
FullMsg = FullMsg & LoopMsg & vbCrLf
LoopMsg = ""
Next i
Text1.Text = FullMsg
73. Save your project with the name “week9_1” in your folder.
74. Run the program
During This Lab we want to Complete a project “Week9_2” to add the numbers
(0,38) (2,36) ………(36,2) (38,0 ) to two dimension array and display it in a text
box. The main form look like this form
29
75. Open project week9_2
76. Double-click the " Traverse Array" Command button and write this code
Dim i As Integer
Dim j As Integer
Dim iMyArray(19, 1) As Integer
Dim BeginMsg As String
Dim MidMsg As String
Dim LoopMsg As String
Dim FullMsg As String
For i = 0 To 19
iMyArray(i, 0) = i * 2
iMyArray(i, 1) = 38 - i * 2
Next i
30
For j = 0 To 1
LoopMsg = LoopMsg & BeginMsg & "( " & i & "," & j & " )"
LoopMsg = LoopMsg & vbTab & MidMsg & iMyArray(i, j)
LoopMsg = LoopMsg & vbTab
Next j
FullMsg = FullMsg & LoopMsg & vbCrLf
LoopMsg = ""
Next i
Text1.Text = FullMsg
77. Save your project with the name “week9_2” in your folder.
78. Run the program
31
82. Double-Click on the form itself and writ this code to add element to ListBox
control and the Combo Box Controls
List1.AddItem "Mohamed"
Combo1.AddItem "Mohamed"
Combo2.AddItem "Mohamed"
List1.AddItem "Ali"
Combo1.AddItem "Ali"
Combo2.AddItem "Ali"
List1.AddItem "Hassan"
Combo1.AddItem "Hassan"
Combo2.AddItem "Hassan"
List1.AddItem "Omer"
Combo1.AddItem "Omer"
Combo2.AddItem "Omer"
Combo1.Text = Combo1.List(0)
Combo2.Text = Combo2.List(0)
83. Double-click the " Add Name To All" Command button and write this code
84. Double-click the " Delete selected item" Command button and write this
code
85. Double-click the " Clear All items" Command button and write this code
List1.Clear
86. Save your project with the name “week10_1” in your folder.
87. Run the program
32
Week 13
Lab 2
During This Lab we want to Complete a project “Week10_2” to simulate a signal.
The main form look like this form
sequence = 0
HScroll1.Value = Timer1.interval
End Sub
3. Double-click the " Start Signal" Command button and write this
code
sequence = 0
Image5.Picture = Image1.Picture
Timer1.Enabled = True
End Sub
33
4. Double-click the " Stop Signal" Command button and write this
code
Timer1.Enabled = False
sequence = 0
Image5.Picture = Image4.Picture
End Sub
End Sub
During This Lab we want to Complete a project “Week11_1” to Use text boxes to
enter data into a text file and later access the content of the file back . The main
form looks like this form
34
88. Open project week11_1
89. Double-click on " The form" and write this code
35
Frmprint.Print "Address is ........"; Address
Frmprint.Print "Sex is ..........."; sex
Frmprint.Print "Age is ............"; age
Loop
End Sub
92. Double-click on " Command Save Rec" and write this code
Name = Txtname
Address = Txtaddress
sex = Txtsex
age = Txtage
During This Lab we want to Complete a project “Week11_1” to Use input box to
enter data and message box to display message . The main form look like this
form
36
95. Open project week11_2
96. Double-click on " Start Demo" and write this code
Message = "Your Name is : " & First_name & " " & Second_name
&_
vbCrLf & " Are you sure ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "MsgBox Demonstration" ' Define title.
Response = MsgBox(Message, Style, Title)
37
If Response = vbYes Then ' User chose Yes.
MsgBox "Thank you"
End If
97. Save your project with the name “week11_2” in your folder.
98. Run the program
Analysis: The command button in Figure 11.8 is named cmdSimple, so clicking the
command button executes the event procedure shown in Listing 11.3. Line 3 stores the
combo box's Text property value to that combo box's list of items. The combo box will
38
not contain a user's entry in the upper data entry portion of the combo box until an
AddItem method adds that entry to the list. The Text property always holds the current
value shown in the data entry portion of the combo box, but the AddItem method must
add that value to the list.
As soon as the user's entry is added, line 4 erases the data entry portion of the combo box.
After all, the user's text will now appear in the lower listing portion of the combo box
(thanks to line 3), so line 4 clears the data entry area for more input. In addition, line 5
sets the focus back to the combo box (the focus appears in the data entry area that line 4
cleared) so that the user is ready to add an additional item to the combo box.
The timer can be stopped while the application is running by setting the Enabled
property to FALSE or setting the Interval property to 0.
Start a new project and add two labels with Name property set to lblTime and lblDate
respectively. Change the FontName for the labels to Courier New (a non-proportional
font) and choose a suitable FontSize for each label. Change the caption of the form to
Clock.
Add a Timer Control to the form. It doesn’t matter where you put this control because it
is invisible when the project is running.
39
Select the Properties window for the Timer Control and set Enabled to True and the
Interval property to 1000.
The units for the interval are milliseconds. With these settings, the code in the event
Timer1_Timer will be executed approximately every second.
Add the following event procedure code and try out your application. Private Sub
Form_Load()
lblDate.Caption = Date
End Sub
40
Week 14
Lab 1
During This Lab we want to Complete a project “Week12_1” to simulate a Data
Control. The main form look like this form
41
Table For Data Control
Property Value
DataBase \\Programming\Samples\Week12\BIBLIO.MDB
Name
RecordSource Titles
102. For the textboxs adjust DataSource "Data1" and the textbox
DataField to its appropriate field as in above.
103. Save your project with the name “week13_1” in your folder.
104. Run the program
42
105. Create New Project.
106. Select the VB Application Wizard
107. Click Next until to start create the data form creation and select
"Access" and specify the Data Base name. Select the "Grid (Datasheet)"
and select the Record Source is "Title" and select all Available Fields .
108. Save your project with the name “week12_2” in your folder.
109. Run the program
43
1. Create New Project.
2. Select the Standard EXE
3. Create a data form with Data Form Wizard with this properties :
DataBase \\Programming\Samples\Week12\BIBLIO.MDB
Name
44
Week 15
Lab 1
Open the database form that you want to generate report for
45
3. Right click on connection 1
4. Select properties in the Data link pup-up menu windows displayed
5. Specify the provide or the Database engine. By selecting 3.51 OLE DB
provide for the Database Structure created within vb environment or using
MS Access ‘97
6. Click next, this takes you to connector Data link Properties windows
46
• Click on Add command Icon on the Data environment tool bar,
Command 1 is created
• Right Click the Command 1 created
• Click on property from the PUP-UP menu displayed
• Command properties window is displayed
47
Click Report Header for the Heading that appears once in the
Report eg NIGERIA BOTTLING COMPANY KADUNA
Click Page Header for the Column Title
Click Detail Section for Fields in the Report
Go To Properties window, select Data Source
Select data source, eg Destock
Click Data Member, select Command Object for the Data
Members i.e. (mdstock)
Right click any area within the Data report window
From the PUP-UP displayed, select retrieve structure, to link our
report to the command structure created earlier in the Data
environment
Click Yes
Select the band (i.e. the Page Header)
Double Click label on the tool bar
Set the properties e.g.
Change Caption to “STOCK REPORT”.
- Click on Details report
- Double Click the Report Text box
48
- Click Unbound
a.Click Data Member in Properties Text box
b.Select (cmdstock)
c. Select Data field Itemnum
12. Give your Data report a name e.g. drpstock
13. Set caption for the windows
14. Set window state to Maximise
15. Save the project to Update all the work
16. Then Go back to the interface form
17. Double click the Command Report
49
110. Open project week11_1
111. Use Menu Editor to create menu as in the following table .
50
Table For main menu
Font MnuFormatFont
Small MnuFormatFontSmall
Medium mnuFormatFontMedium
Large mnuFormatFontLarge
114. Save your project with the name “week15_1” in your folder.
115. Run the program
51