You are on page 1of 13

Microsoft

Visual Basic

Programming Module

Ismail bin Shaari Sek. Raja Perempuan Taayah Ipoh BSc. Computer Science with Education, UTM H/P : 019 4636171 e-mail : ismail@taayah.com

Microsoft Visual Basic 6.0 Integrated Development Environments (IDE)

End button to stop the running program. Also user can click close button

Project Explorer Start button, To run the program

View Code

View Object

Form Windows

Project Windows Container

Toolbox

Properties Window. To setting object properties such as name, forecolor dan caption

Important Tips. Windows Properties Windows Project Explorer Toolbox Shortcut-Key F4 Ctrl-R Alt-VX Menu From view menu Select Properties Windows From View menu Select Project Explorer From View menu Select Toolbox Toolbar

How to start Microsoft Visual Basic 6.0

2 2

How to start Microsoft Visual Basic 6.0 1. 2. Click the Start Button. If you see Microsoft Visual Basic 6.0 in the main Start menu, click it. If you don't see Microsoft Visual Basic 6.0 in the main Start menu, point to All Programs, Microsoft Visual Basic 6.0, and then click Microsoft Visual Basic 6.0. The Microsoft Visual Basic 6.0 window opens and a Visual Basic button appears on the taskbar. 3. 4. 5. Click Open button. Click maximize button on the container windows. Resize the form to appropriate size.

First Program Hello World

Name : txtHello

2
Name : cmdHello Caption : Hello To start coding, double click Hello button

Properties windows

Do all the steps below 1. Drag textbox and command button into the form. 2. Name the textbox and command button by entering value in the properties windows. Object Textbox1 Command1 Properties Name Name Caption Value txtHello cmdHello Hello

3.

Double click Command Button to insert code.

3. Double click command button and type the statement , txtHello.Text = Hello, World 4. Click the start button to run the program and click Hello Button. 4

Save File
1. Hello.frm 1 (form file) 3

4 2. Hello.vbp (Project file)

3. 1

Hello.exe (Execution File)

2 4 5

How to show file extension ( *.exe, *.frm, *.vbp )

4.

Second program Favourite Food I


Do all the steps below 1. 2. txtName txtFood Drag textbox1, textbox2, picturebox1, a command1 into the form. Setting objects properties in properties windows Object TextBox1 TextBox2 picOutput Label1 Label2 Picturebox1 cmdPrint 3. Command1 Properties Name Name Caption Caption Name Name Caption Value txtName txtFood Name Favourite Food picOutput cmdPrint Print

Double click print button to enter below statement

picOutput.Print "Thank! " ; txtName picOutput.Print "I will treat you "; txtFood 4. Click the start button to running the program.

Third program Favourite Food II

Do all the steps below 1. Drag textbox1, combobox1, picturebox1, a command1 into the form. Setting objects properties in properties windows Object TextBox1 ComboBox1 Properties Name Name List Picturebox1 Command1 Name Name Caption 3. Value txtName cboFood Nasi Lemak Fried Rice Burger picOutput cmdPrint Print

txtName

2. cboFood

picOutput

cmdPrint

Double click print button to enter below statement

picOutput.Print "Thank! " ; txtName picOutput.Print "I will treat you " ; cboFood 7

Fourth program Pass & Failed

Private Sub cmdCheck_Click() picOutput.Print "Name :"; txtName picOutput.Print "Result :"; If Val(txtBM) > 49 Then picOutput.Print "Pass" Else picOutput.Print "Failed" End If End Sub

Object Label1 Label2 TextBox1 TextBox2 Picturebox1 Command1

Properties Caption Caption Name Name Name Name Caption

Value Name BM txtName txtBM picOutput cmdCheck Check

Fifth Project Students Grade

Private Sub cmdCheck_Click() picOutput.Print "Name :"; txtName picOutput.Print "Result :"; If Val(txtBM) > 49 Then picOutput.Print "Pass" Else picOutput.Print "Failed" End If Select Case Val(txtBM) Case 80 To 100: Grade = "A" Case 70 To 79: Grade = "B" Case 60 To 69: Grade = "C" Case 50 To 59: Grade = "D" Case 0 To 49: Grade = "E" End Select picOutput.Print "Grade :" + Grade End Sub

Sixth Project Show and Hide

End imgOne.Visible = True imgOne.Visible = False

Object Form

Properties Name Caption Icon

Command Button1

Name Caption Picture Nama Caption Name Caption Name Picture

Value frmShowHide My sixth program c:\ProgramFiles\ Microsoft Visual Studio\Common\ Graphic\Icons\ Misc\eye.ico cmdShow Show cmdHide Hide cmdExit Exit imgOne c:\ProgramFiles\ Microsoft Visual Studio\Common\ Metafile\Business\ AnsMach.wmf True

Command Button2 Command Button32 Image1

Stretch

Seventh Program Simple Math


If val(txtAns) = val(txt1) + val(txt2) Then imgRight.Visible = True imgWrong.Visible = False Else imgWrong.Visible = True imgRight.Visible = False End If

Object TextBox1 TextBox2 TextBox3 Image1

Properties Name Name Name Name Picture Visible

Value txt1 txt2 txtAns imgRight Right.bmp False imgWrong Wrong.bmp False cmdCheck Check cmdExit Exit

Image

Name Picture Visible

Command1 Command2

Name Caption Name Caption

End

Eighth Program Find the area of circle


Option Explicit Const PI = 3.142 Dim Dim Dim Dim Area As Double Radius As Double Circumference As Double Diameter As Double

Private Sub cmdCircle_Click() Radius = Val(txtRadius) Area = PI * Radius * Radius picResult.Print "Area of circle is "; Area End Sub Object Label1 TextBox1 Picturebox1 Command1 Command2 Command3 Properties Caption Name Name Name Caption Name Caption Name Caption Value Radius txtRadius picResult cmdCircle Circle cmdCircumference Circumference cmdDiamter Diameter 10

Ninth Program Introduction to User Define Function


Option Explicit Function Add(x As Integer, y As Integer) Add = x + y End Function Private Sub cmdAdd_Click() Dim a As Integer Dim b As Integer

Object Label1

Label2 TextBox1 TextBox2 TextBox3 Command1 Command2 Command3

Properties Name Caption Alignment Caption Alignment Name Name Name Name Caption Name Caption Name Caption

Value lblOperator Center = Center txt1 txt2 txtResult cmdAdd Add cmdMultiply Multiply cmdSubtract Subtract

a = Val(txt1) b = Val(txt2)

'

txtResult = Add(a, b) lblOperator = "+" End Sub

Extra Project 1 Image Viewer


Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub

Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub

Private Sub File1_Click() Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName) End Sub

11

Extra Project II : Message Formatter The Date Time Picker and UpDown control may not appear in your toolbox window. They are custom control, and you must add them to the toolbox window before you can use them. 1. 2. 3. Open project menu Choose component Scroll down the list of the components windows until you see microsoft windows common controls-2 6.0 (SP4). If there is a check mark to the left of microsoft windows common controls-2 6.0 (SP4) that means it already resides in the toolbox windows. If there is no check mark in the check box, place one there by clicking the check box. Click OK button of the Component windows.

4.

5.

UpDown Control

Date Time Picker Control

12

DTPicker Control Name:dtpDate Textbox Name:txtMessage

UpDown Control Name:updSize Value:8 Min:8 Max:20 Increment:2

Option Button Name:optBlue Caption:Blue

Checkbox Name:chkBold Caption:Bold

Frame Name:fraStyle Caption:Style Frame Name:fraColor Caption:Color Textbox Name:txtSize

Label Name:lblMessage

Image Name:imgBig Picture: Microsoft Visual Studio\Common\Graphics\Icon s\Computer\Cdrom01.ico

Listbox Name:lstFont List: Arial Times New Roman Bookman Old Style Comic Sans MS

Double Click Object Red Option button Blue Option button Checkbox Display Button UpDown ListBox

Add code lblMessage.Forecolor = vbRed lblMessage.Forecolor = vbBlue lblMessage.FontBold = chkBold.Value lblMessage.Caption = dtpDate.Value & : & txtMessage.Text txtSize.Text = updSize.Value lblMessage.FontSize = updSize.Value lblMessage.FontName = lstFont.Text

13

You might also like