Sunday, October 25, 2009

Visual Basic 6 Sample

Sample Codes


We will use the following:

Button, Label, CheckBox, TextField

Problem:

A Flower shop sales 3 kinds of flowers:

Tulip, Rose, and Sun flower.

Each flower has a different price, Tulip is 3.50$ each, Rose is 5.00$ each, and Sun flower is 7.00$ each. If the customer buys more than or equal to 2 dozens (any kinds of flower), total price will be decreased by 3%, else if customer buys more than or equal to 10 dozens, the total price will be decreased by 15%, else customer buys more than or equal to100 dozens, the total price will be decreased by 40%.

Display the Number of Flowers, Additional charge, Total Cost, Discount (Percentage)

Discounted Cost

Solution:

Make a New Form, add 3 CheckBoxes, add 3 TextFields, add 7 Labels, add 1 Button, and add again 1 Checkbox.

Set the following Properties


Name

Caption

Style

Enabled

Visible

Check1

Tulip

Tulip

0-Standard

True

True

Check2

Rose

Rose

0-Standard

True

True

Check3

SunFlower

Sun Flower

0-Standard

True

True

Check4

deliver

DELIVER

1-Graphical

True

True

Text1

tuliptxt


1-Fixed Single

False

True

Text2

Rosetxt


1-Fixed Single

False

True

Text3

Sunflowertxt


1-Fixed Single

False

True

Command1

Command1

Ok


True

True

Label1

Label1

Flower Name

0-Standard

True

True

Label2

Label2

Number of Flower/s

0-Standard

True

True

Label3

nof

Number Of Flowers

0-Standard

True

True

Label4

additionalcost

Additional Charge:

0-Standard

True

True

Label5

tc

Total Cost:

0-Standard

True

True

Label6

dc

Discounted Cost:

0-Standard

True

True

Label7

discount

Discount:

0-Standard

True

True

Form1

Form1

Basic Tutorial

3-Fixed Dialog

True

True

See figure 1.1 for reference


Figure 1.1












If you find any Errors, just email me @ gilbert3852@gmail.com

Code: Copy the code and Paste to the code editor

Note: In the Code editor, you must erase all the default code first before pasting the new code below…




Dim additional1 As Double

Private Sub Command1_Click()

On Error GoTo a

Dim totalcost As Double

Dim discountedcost As Double

Dim total As Double

Dim Tulip1 As Double

Dim Rose1 As Double

Dim SunFlower1 As Double

Dim discount1 As Double

Tulip1 = Val(tuliptxt.Text)

Rose1 = Val(Rosetxt.Text)

SunFlower1 = Val(Sunflowertxt.Text)

total = Tulip1 + Rose1 + SunFlower1

Tulip1 = Tulip1 * 3.5

Rose1 = Rose1 * 5

SunFlower1 = SunFlower1 * 7

totalcost = Tulip1 + Rose1 + SunFlower1

If total >= 24 Then

discount = "Discount: 3%"

discount1 = 0.03

If total >= 120 Then

discount = "Discount: 15%"

discount1 = 0.15

If total >= 1200 Then

discount = "Discount: 40%"

discount1 = 0.4

End If

End If

End If

discountedcost = totalcost * discount1

nof = "Number of Flowers: " & total

tc = "Total Cost: " & totalcost

dc = "Discounted Cost: " & (totalcost - discountedcost) + additional1

a:

If Err.Number = 13 Then

MsgBox "ERROR"

Else

End If

End Sub

Private Sub deliver_Click()

If deliver.Value = 1 Then

additionalcost = "Additional Cost 50"

additional1 = 50

Else

additionalcost = "Additional Cost 0"

additional1 = 0

End If

End Sub

Private Sub discount_Click()

End Sub

Private Sub Rose_Click()

If Rose.Value = 1 Then

Rosetxt.Enabled = True

Rosetxt.SetFocus

Else

Rosetxt.Enabled = False

End If

End Sub

Private Sub SunFlower_Click()

If SunFlower.Value = 1 Then

Sunflowertxt.Enabled = True

Sunflowertxt.SetFocus

Else

Sunflowertxt.Enabled = False

End If

End Sub

Private Sub Tulip_Click()

If Tulip.Value = 1 Then

tuliptxt.Enabled = True

tuliptxt.SetFocus

Else

tuliptxt.Enabled = False

End If

End Sub




Press F5 to Run.....

And that's it..


For Comments and Suggestions

Just email me at gilbert3852@gmail.com

Thank you….

CREDIT TO: GILBERT, CODEGOO.BLOGSPOT.COM