example of visual basic projects

Post on 08-Feb-2016

46 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

EXAMPLE OF VISUAL BASIC PROJECTS

TRANSCRIPT

Visual Basic Examples

1

1. Cubic Function Graph PlotterThis is a program that enable users to input

the coefficients of a cubic equation and draw its graph.

The cubic equation takes the form  ax3+bx2+cx+d

2

3

4

5

2- Traffic LightThis 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 ThenShape2.Visible = TrueShape1.Visible = FalseShape3.Visible = FalseElseIf Shape2.Visible ThenShape3.Visible = TrueShape2.Visible = FalseShape1.Visible = FalseElseShape1.Visible = TrueShape2.Visible = FalseShape3.Visible = FalseEnd If End Sub

7

   

8

3-Digital ClockVisual 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- CalculatorThis 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.ListIndexCase 0Label1 = Str$(Val(Text1.Text) + Val(Text2.Text))Case 1Label1 = Str$(Val(Text1.Text) - Val(Text2.Text))End SelectEnd Sub

12

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

13

Private Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()Combo1.AddItem "+"Combo1.AddItem "-"Combo1.AddItem "x"Combo1.AddItem "÷"Combo1.ListIndex = 1

End Sub

14

5-Picture ViewerLet’s 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.ShowOpenPicture1.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.ShowOpenis 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.ShowOpenPicture1.Picture = LoadPicture

(CommonDialog1.FileName)End Sub

18

19

6- Quadratic Functions Graph PlotterThis is a program that can plot graphs for

quadratic functions. The general format of a quadratic equation is f(x)= ax2+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 IntegerDim w, v As Singlea = 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 leftFor w = 0 To 10 Step 0.001

22

v = a * (5 - w) ^ 2 - b * (5 - w) + cpic_graph.PSet (w, 5 - v)Next wEnd Sub

Private Sub Command1_Click()pic_graph.Clstxt_a.Text = ""txt_b.Text = ""txt_c.Text = ""End Sub

23

24

7- Multimedia PlayerI 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 ThenMMControl1.DeviceType = "WaveAudio"End If

If Combo1.ListIndex = 1 ThenMMControl1.DeviceType = "Sequencer"End If

If Combo1.ListIndex = 2 ThenMMControl1.DeviceType = "AVIVideo"End If

If Combo1.ListIndex = 3 ThenMMControl1.DeviceType = ""End If

26

27

8-Simultaneous Equations SolverIt 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 IntegerDim x, y As Doublea = 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=mcx+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 2The 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 IntegerDim x1, x2, y1, y2 As Doublea = 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) / by2 = (m - a * x2) / bLbl_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=mcx2+dy2=nSimultaneous 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) / by2 = (m - a x2) / b

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

36

37

top related