Sie sind auf Seite 1von 37

Visual Basic Examples

1
1. Cubic Function Graph
Plotter
This is a program that enable users to input the
coefficients of a cubic equation and draw its
graph.

The cubic equation takes the form ax
3
+bx
2
+cx+d

2
The Codes
Private Sub cmd_draw_Click()
Dim a, b, c, d As Integer
Dim w, v As Double
a = Val(txt_a.Text)
b = Val(txt_b.Text)
c = Val(txt_c.Text)
d = Val(txt_d.Text)

For w = 0 To 10 Step 0.001

v = a * (5 - w) ^ 3 + b * (5 - w) ^ 2 + c * (5 - w) + d
pic_graph.PSet (w, 5 - v)

Next w

End Sub
3

Private Sub Command1_Click()

pic_graph.Cls
txt_a.Text = ""
txt_b.Text = ""
txt_c.Text = ""
txt_d.Text = ""
End Sub


Private Sub Command2_Click()
End
End Sub
4
5
2- Traffic Light
This Traffic Light program is a Visual Basic
program. It is a nice little program. In this
program, you have to insert one timer and set its
interval according to your wish.
Here I set it to 3000, which is equivalent to 3
seconds. Next you insert three shapes and set
their shape properties to circle and fill the colors.
There it goes, your traffic light is working!

6
Private Sub Timer1_Timer()
If Shape1.Visible Then

Shape2.Visible = True
Shape1.Visible = False
Shape3.Visible = False

ElseIf Shape2.Visible Then

Shape3.Visible = True
Shape2.Visible = False
Shape1.Visible = False

Else

Shape1.Visible = True
Shape2.Visible = False
Shape3.Visible = False

End If End Sub

7



8
3-Digital Clock
Visual Basic programming is so simple that
sometime you just need to write a one line code
to create a wonderful tiny little but nice
application. For example, you can write a one-line
code to create a digital clock.
In this program, you have to insert a timer control
into the form . Then go to the properties window
to set the timer's interval value to 1000 so that
Visual Basic updates the time every 1000
milliseconds, or once a second. Other properties
that you ought to set are to change the caption
(here is My Clock) and to set Form1's MaxButton
to false (so that it cannot be maximized by the
user)

9
Now, double click the timer and enter the one line
code as follows:

Private Sub Timer1_Timer()

Label1.Caption = Time

End Sub

10
4- Calculator
This is simple calculator created by my son, Liew
Xun. This program uses a combo box which
include four operators, addition, subtraction,
multiplication and division. It can perform the
above four basic calculations by changing the
operators.









11
The Codes (Copyright Liew Xun)
Private Sub Combo1_()

Select Case Combo1.ListIndex
Case 0
Label1 = Str$(Val(Text1.Text) + Val(Text2.Text))
Case 1
Label1 = Str$(Val(Text1.Text) - Val(Text2.Text))
End Select
End Sub


12
Private Sub Command1_Click()
Select Case Combo1.ListIndex
Case 0
Label1 = Str$(Val(Text1.Text) + Val(Text2.Text))
Case 1
Label1 = Str$(Val(Text1.Text) - Val(Text2.Text))
Case 2
Label1 = Str$(Val(Text1.Text) * Val(Text2.Text))
Case 3
Label1 = Str$(Val(Text1.Text) / Val(Text2.Text))

End Select
End Sub

13
Private Sub Command2_Click()
End
End Sub

Private Sub Form_Load()
Combo1.AddItem "+"
Combo1.AddItem "-"
Combo1.AddItem "x"
Combo1.AddItem ""
Combo1.ListIndex = 1



End Sub
14
5-Picture Viewer
Lets create a program that enables the users to
open and choose files from the folders in their
PC. This can be done easily using a picture box
and a common dialog box. In this program, you
need to insert a picture box, a common dialog
box and an image. In the image properties
windows, click on the picture property and select
a picture that resembles an open file icon.
15
The procedure to open the common dialog box to
browse the picture files as well as to load the
selected picture into the picture box is
CommonDialog1.Filter =
"Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|
Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon
Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
Picture1.Picture =
LoadPicture(CommonDialog1.FileName)

16
The filter property of the common dialog box uses the
format as shown below
Bitmaps(*.BMP)|*.BMP
to specify the file type, and uses the pipe line | to
separate different file types.
Visual Basic supports most of the picture formats
namely bmp, wmf, jpg, gif, ico(icon) and
cur(cursor) files. The
command CommonDialog1.ShowOpen
is to open the common dialog box and the command
Picture1.Picture = LoadPicture
(CommonDialog1.FileName)
is to load the selected picture file into the picture box.

17
The whole program is shown below and the
output is shown in the figure below:
Private Sub Image1_Click()
CommonDialog1.Filter =
"Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|
Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon
Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
Picture1.Picture = LoadPicture
(CommonDialog1.FileName)
End Sub

18
19
6- Quadratic Functions Graph
Plotter
This is a program that can plot graphs for
quadratic functions. The general format of a
quadratic equation is f(x)= ax
2
+bx+c , where a, b
and c are constant.
This program employs a picture box as the plot
area and three text boxes to obtain the values of
the coefficients a, b, c of the quadratic equation
from the users.
We also need to modify the scale factor in the
properties windows of the picture box. I am using
a scale of 0.5 cm to represent 1 unit .


20
Besides, we need to make some transformation
as the coordinates in VB start from top left but I
want it to start from the middle. I am using the
Pset method to draw the graph using a very small
increment.

This program is useful for high school students
and mathematics teachers.

21
The Code

Private Sub cmd_draw_Click()
Dim a, b, c As Integer
Dim w, v As Single
a = Val(txt_a.Text)
b = Val(txt_b.Text)
c = Val(txt_c.Text)

'Using a scale of 0.5 cm to represent i unit to draw the
graph
' Need to make some transformation as the
coordinates in VB start from top left
For w = 0 To 10 Step 0.001


22
v = a * (5 - w) ^ 2 - b * (5 - w) + c
pic_graph.PSet (w, 5 - v)

Next w

End Sub


Private Sub Command1_Click()
pic_graph.Cls

txt_a.Text = ""
txt_b.Text = ""
txt_c.Text = ""

End Sub
23
24
7- Multimedia Player
I have created a multimedia player that can play all
kinds of media files such as wav, midi, mp3, mpeg
video, avi video and so on. When you launch the
program, you can select files of different types from
different drives. After you have selected a particular
files, you can play it using the customized button or
you can use the buttons of the multimedia control
In this program, you need to insert the Microsoft
Multimedia Control, a combo box, a dirListBox, a
DriveListBox, a fileListbox, a picture box(to play the
video) , a text box and four command buttons which
you label as "Play", "Open","Stop" and "Exit".One of
the most important procedures is to open various
audio files, the codes are as follow:

25
Private Sub Open_Click()
If Combo1.ListIndex = 0 Then
MMControl1.DeviceType = "WaveAudio"
End If

If Combo1.ListIndex = 1 Then
MMControl1.DeviceType = "Sequencer"
End If

If Combo1.ListIndex = 2 Then
MMControl1.DeviceType = "AVIVideo"
End If

If Combo1.ListIndex = 3 Then
MMControl1.DeviceType = ""

End If

26
27
8-Simultaneous Equations
Solver
It has been sometimes we have not deal with
mathematical problems, so why not focus on this
subject once again. Actually the thought of
programming a mathematical tool came across
my mind when my son asked me some questions
associated with simultaneous equations.

Then It dawned on me that why not I program a
simultaneous equation solver so that it can solve
my son's problems as well as my problems too.
There you are, the program is ready and I am
showing the codes here:

28
Private Sub Command1_Click()
Dim a, b, c, d, m, n As Integer
Dim x, y As Double

a = Val(Txt_a.Text)
b = Val(Txt_b.Text)
m = Val(Txt_m.Text)
c = Val(Txt_c.Text)
d = Val(Txt_d.Text)
n = Val(Txt_n.Text)
x = (b * n - d * m) / (b * c - a * d)
y = (a * n - c * m) / (a * d - b * c)
Lbl_x.Caption = Round(x, 2)
Lbl_y.Caption = Round(y, 2)

End Sub

29
Private Sub Command2_Click()
Txt_a.Text = ""
Txt_b.Text = ""
Txt_m.Text = ""
Txt_c.Text = ""
Txt_d.Text = ""
Txt_n.Text = ""
Lbl_x.Caption = ""
Lbl_y.Caption = ""

End Sub

30
Explanation:
Linear simultaneous equations take the following forms:

ax+by=m
cx+dy=n

Simultaneous equations can normally be solved by the
substitution or elimination methods. In this program, I
employed the substitution method. So, I obtained the
following formulae:

x = (b * n - d * m) / (b * c - a * d)
y = (a * n - c * m) / (a * d - b * c)

To limit the answers to two decimal places, I used the
round function.

31
32
9-Simultaneous Equations
Solver 2
The coming example will show you how to design a
program that can solve mixed simultaneous equations, that
is , one linear equation and one quadratic equation. Here
is the program:

Private Sub Command1_Click()
Dim a, b, c, d, m, n As Integer
Dim x1, x2, y1, y2 As Double

a = Val(Txt_a.Text)
b = Val(Txt_b.Text)
m = Val(Txt_m.Text)
c = Val(Txt_c.Text)
d = Val(Txt_d.Text)
n = Val(Txt_n.Text)

33
x1 = (m * a * d + Sqr(m ^ 2 * a ^ 2 * d ^ 2 - (b ^ 2 * c +
a ^ 2 * d) * (d * m ^ 2 - b ^ 2 * n))) / (b ^ 2 * c + a ^ 2 *
d)

x2 = (m * a * d - Sqr(m ^ 2 * a ^ 2 * d ^ 2 - (b ^ 2 * c +
a ^ 2 * d) * (d * m ^ 2 - b ^ 2 * n))) / (b ^ 2 * c + a ^ 2 *
d)

y1 = (m - a * x1) / b
y2 = (m - a * x2) / b
Lbl_x1.Caption = Round(x1, 2)
Lbl_y1.Caption = Round(y1, 2)
Lbl_x2.Caption = Round(x2, 2)
Lbl_y2.Caption = Round(y2, 2)

End Sub

34
Mixed simultaneous equations take the following
forms:
ax+by=m
cx
2
+dy
2
=n
Simultaneous equations can normally be solved
by the substitution or elimination methods. In this
program, I employed the substitution method. So,
I obtained the following formulae:

35
Explanation:
x1 = (m a d + Sqr(m
2
a
2
d
2
- (b
2
c + a
2
d) (d m
2
- b
2
n))) / (b
2
c + a
2
d)

x2 = (m a d +-Sqr(m
2
a
2
d
2
- (b
2
c + a
2
d) (d m
2
- b
2
n))) / (b
2
c + a
2
d)

y1 = (m - a x1) / b
y2 = (m - a x2) / b
To limit the answers to two decimal places, I used
the round function.

36
37

Das könnte Ihnen auch gefallen