visual basic new programming module 2008

18
Microsoft Visual Basic Programming Module Ismail bin Shaari Sek. Raja Perempuan Taayah Ipoh BSc. Computer Science with Education, UTM 1

Upload: nana-samsul-bahri

Post on 17-Oct-2014

32 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Visual Basic New Programming Module 2008

Microsoft Visual Basic

Programming Module

Ismail bin ShaariSek. Raja Perempuan Taayah Ipoh

BSc. Computer Science with Education, UTMH/P : 019 – 4636171

e-mail : [email protected]

1

Page 2: Visual Basic New Programming Module 2008

Microsoft Visual Basic 6.0 Integrated Development Environments (IDE)

Important Tips.

Windows Shortcut-Key Menu Toolbar

Properties Windows F4

From view menuSelect Properties Windows

Project Explorer Ctrl-R From View menuSelect Project Explorer

Toolbox Alt-VXFrom View menuSelect Toolbox

2

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

Form Windows

Project Windows Container

Toolbox

ProjectExplorer

Start button, To run the program

View Code View Object

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

button

Page 3: Visual Basic New Programming Module 2008

How to start – Microsoft Visual Basic 6.0

How to start – Microsoft Visual Basic 6.0

1. Click the Start Button.

2. 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. Click Open button.

4. Click maximize button on the container windows.

5. Resize the form to appropriate size.

3

1

2

3

4

5

2

Page 4: Visual Basic New Programming Module 2008

First Program – Hello World

Do all the steps below1. Drag textbox and command button into the form.2. Name the textbox and command button by entering value in the properties

windows.

Object Properties Value

Textbox1 Name txtHello

Command1

Name cmdHello

Caption 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

Name : txtHello

Name : cmdHelloCaption : Hello

To start coding, double click Hello button

1

2

Properties windows

Page 5: Visual Basic New Programming Module 2008

Save File

1. Hello.frm (form file)

2. Hello.vbp (Project file)

3. Hello.exe (Execution File)

5

1

2

3

4

5 6

1

2 3 4

1

2

3

4

5 6

Page 6: Visual Basic New Programming Module 2008

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

6

1

2

3

4

5

6

Page 7: Visual Basic New Programming Module 2008

4. Second program – Favourite Food I

Third program – Favourite Food II

7

txtName

txtFood

picOutput

cmdPrint

txtName

cboFood

picOutput

cmdPrint

Do all the steps below

1. Drag textbox1, textbox2, picturebox1, a command1 into the form.

2. Setting objects properties in properties windows

Object Properties Value

TextBox1 Name txtName

TextBox2 Name txtFood

Label1 Caption Name

Label2 Caption Favourite Food

Picturebox1 Name picOutput

Command1 Name cmdPrint

Caption Print

3. Double click print button to enter below statement

picOutput.Print "Thank! " ; txtNamepicOutput.Print "I will treat you "; txtFood

4. Click the start button to running the program.

Do all the steps below

1. Drag textbox1, combobox1, picturebox1, a command1 into the form.

2. Setting objects properties in properties windows

Object Properties Value

TextBox1 Name txtName

ComboBox1 Name cboFood

List

Nasi LemakFried RiceBurger

Picturebox1 Name picOutput

Command1 Name cmdPrint

Caption Print

3. Double click print button to enter below statement

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

Page 8: Visual Basic New Programming Module 2008

Fourth program – Pass & Failed

Fifth Project – Student’s Grade

8

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

Object Properties Value

Label1 Caption Name

Label2 Caption BM

TextBox1 Name txtName

TextBox2 Name txtBM

Picturebox1 Name picOutput

Command1 Name cmdCheck

Caption Check

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 :" + GradeEnd Sub

Page 9: Visual Basic New Programming Module 2008

Sixth Project – Show and Hide

Object Properties

Value

Form Name frmShowHideCaption My sixth programIcon c:\ProgramFiles\

Microsoft Visual Studio\Common\Graphic\Icons\Misc\eye.ico

Command Button1

Name cmdShow

Caption ShowPicture

Command Button2

Nama cmdHide

Caption HideCommand Button32

Name cmdExit

Caption ExitImage1 Name imgOne

Picture c:\ProgramFiles\Microsoft Visual Studio\Common\Metafile\Business\AnsMach.wmf

Stretch True

Seventh Program – Simple Math

9

imgOne.Visible = True

End

imgOne.Visible = False

Page 10: Visual Basic New Programming Module 2008

Eighth Program – Find the area of circle

Object Properties Value

Label1 Caption Radius

TextBox1 Name txtRadius

Picturebox1 Name picResult

Command1 Name cmdCircle

Caption Circle

Command2 Name cmdCircumference

Caption Circumference

Command3 Name cmdDiamter

Caption Diameter

Ninth Program – Introduction to User Define Function

10

Option Explicit

Const PI = 3.142

Dim Area As DoubleDim Radius As DoubleDim Circumference As DoubleDim Diameter As Double

Private Sub cmdCircle_Click() Radius = Val(txtRadius) Area = PI * Radius * Radius

picResult.Print "Area of circle is "; AreaEnd Sub

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

End

Object Properties Value

TextBox1 Name txt1

TextBox2 Name txt2

TextBox3 Name txtAns

Image1 Name imgRight

Picture Right.bmp

Visible False

Image Name imgWrong

Picture Wrong.bmp

Visible FalseCommand1

Name cmdCheck

Caption CheckCommand2

Name cmdExit

Caption Exit

Page 11: Visual Basic New Programming Module 2008

Extra Project 1 – Image Viewer

11

Option Explicit

Function Add(x As Integer, y As Integer) Add = x + yEnd Function

Private Sub cmdAdd_Click() Dim a As Integer Dim b As Integer a = Val(txt1) b = Val(txt2) ' txtResult = Add(a, b) lblOperator = "+"End Sub

Private Sub Drive1_Change() Dir1.Path = Drive1.DriveEnd Sub

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

Object Properties ValueLabel1 Name lblOperator

CaptionAlignment Center

Label2 Caption =Alignment Center

TextBox1 Name txt1TextBox2 Name txt2TextBox3 Name txtResultCommand1 Name cmdAdd

Caption AddCommand2 Name cmdMultiply

Caption MultiplyCommand3 Name cmdSubtract

Caption Subtract

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

Page 12: Visual Basic New Programming Module 2008

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. Open project menu

2. Choose component

3. Scroll down the list of the components windows until you see microsoft windows common controls-2 6.0 (SP4).

4. 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.

5. Click OK button of the Component windows.

12

UpDown Control

1

2

3

4

5

Date Time Picker Control

Page 13: Visual Basic New Programming Module 2008

Double Click Object Add code

Red Option button lblMessage.Forecolor = vbRed

Blue Option button lblMessage.Forecolor = vbBlue

Checkbox lblMessage.FontBold = chkBold.Value

Display Button lblMessage.Caption = dtpDate.Value & “ : “ & txtMessage.Text

UpDown txtSize.Text = updSize.ValuelblMessage.FontSize = updSize.Value

ListBox lblMessage.FontName = lstFont.Text

13

UpDown ControlName:updSizeValue:8Min:8Max:20Increment:2

DTPicker ControlName:dtpDate

TextboxName:txtMessage

ListboxName:lstFontList:ArialTimes New RomanBookman Old StyleComic Sans MS

LabelName:lblMessage

TextboxName:txtSizeFrame

Name:fraColorCaption:Color

FrameName:fraStyleCaption:Style

ImageName:imgBigPicture:Microsoft Visual Studio\Common\Graphics\Icons\Computer\Cdrom01.ico

Option ButtonName:optBlueCaption:Blue

CheckboxName:chkBoldCaption:Bold