Top Banner
Practical File Visual Basic & Web Technology MT-CSE 160 Submitted To: Submitted by: 1
85

Vb Practical File

Nov 12, 2014

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Practical File Visual Basic & Web TechnologyMT-CSE 160

Submitted To:

Submitted by:

1

SAMALKHA GROUP OF INSTITUTIONS

Practical 1

LOADING CONTROLS AT RUNTIME: Write a program which demonstrates the loading of controls at runtime.

Dim i As Integer Dim lblindex As Integer

Dim btnindex As Integer Private Sub cmd_btn_Click(Index As Integer) ' first give the index =0 to command button at design time Load cmd_btn(btnindex) With cmd_btn(btnindex) .Caption = "Button " & btnindex .Left = 3800 .Top = 1200 + btnindex * 500 .Height = 300 .Visible = True End With btnindex = btnindex + 1 End Sub

2

Private Sub cmd_lbl_Click() Set lblnew = Controls.Add("VB.Label", "Labels" & lblindex) With lblnew .Caption = "Label " & lblindex .Left = 800 .Top = 1200 + lblindex * 500 .Visible = True End With lblindex = lblindex + 1 End Sub

Private Sub cmd_Remove_Button_Click() If btnindex > 1 Then btnindex = btnindex - 1 Unload cmd_btn(btnindex) Else MsgBox ("There is no Button to remove") End If End Sub

Private Sub cmd_remove_Label_Click() If lblindex > 1 Then lblindex = lblindex - 1

3

Me.Controls.Remove ("Labels" & lblindex) Else MsgBox ("There is no label to remove")

End If

End Sub

Private Sub cmd_Remove_Tbox_Click() If i > 1 Then i=i-1 Me.Controls.Remove ("TextBox" & i) Else MsgBox ("There is no TextBox to remove")

End If

End Sub

Private Sub cmd_tbox_Click() Set txtnew = Controls.Add("VB.TextBox", "TextBox" & i) txtnew.Text = "TextBox " & i ' move left, top, width , height txtnew.Move 2200, 1200 + i * 500, 1400, 20

4

txtnew.Visible = True

i=i+1 End Sub

Private Sub Form_Load() i=1 btnindex = 1 lblindex = 1

End Sub

5

Practical 2

6

DYNAMIC MENUS: Write a program which demonstrates the dynamic menus. For example the recently visited files are added in menus which changes dynamically.

Dim Index As Integer Private Sub cmd_execute_Click()

If Index < 5 Then Index = Index + 1 Load FileRecent(Index) FileRecent(Index).Enabled = True End If

Dim i As Integer i = Index Do While i > 1 FileRecent(i).Caption = FileRecent(i - 1).Caption i=i-1 Loop

FileRecent(1).Caption = tbox_command.Text

tbox_command.Text = "" tbox_command.SetFocus End Sub

7

Private Sub FileExit_Click() End End Sub

Private Sub FileRecent_Click(Index As Integer) tbox_command.Text = FileRecent(Index).Caption End Sub

Private Sub FileReset_Click() For i = 1 To Index Unload FileRecent(i) Next End Sub

Private Sub Form_Load() FileRecent(0).Enabled = False End Sub

8

9

Practical 3

ANALOG CLOCK: Write a program to create an analog clock, which shows all the three hands (Hour, Minute and second). You can add an option to change the background of the clock.

Dim min, hour As Integer Const PI = 3.14159265 'Pi constant

Dim theta As Single Dim dtheta As Single Dim second As Long Dim cx As Single Dim cy As Single Dim radius As Single Const stheta As Single = 4.7142857

Private Sub btn_image_Click() CommonDialog1.Filter = "Graphics|*.bmp;*.gif;*.jpg|Text File|*.txt|All Files|*.*" CommonDialog1.ShowOpen 10

If Not Picture1.AutoRedraw Then Picture1.AutoRedraw = True

Picture1.Picture = LoadPicture(CommonDialog1.Filename) Picture1.PaintPicture Picture1.Picture, 0, 0, 5000, 5000 gettime End Sub

Public Sub gettime() theta = stheta dtheta = 2 * PI / 60

cx = 2500 cy = 2500 radius = 2300 second = Format(Time, "s") + Format(Time, "n") * 60 + (Format(Time, "h") Mod 12) * 3600 min = second / 60 hour = Format(Time, "h") Mod 12 LineSec.X1 = 2500 LineSec.Y1 = 2500 theta = stheta + (second Mod 60) * dtheta

LineSec.x2 = cx + (radius - 100) * Cos(theta)

11

LineSec.y2 = cy + (radius - 100) * Sin(theta)

line_minute.X1 = 2500 line_minute.Y1 = 2500

line_Hour.X1 = 2500 line_Hour.Y1 = 2500

line_Hour.x2 = cx + (radius - 200) * Cos(stheta + (2 * PI / 3600) * min * 5) line_Hour.y2 = cy + (radius - 200) * Sin(stheta + (2 * PI / 3600) * min * 5) End Sub

Private Sub cmd_ForeColor_Click() CommonDialog1.ShowColor For i = 0 To Label14.Count - 1 Label14(i).ForeColor = CommonDialog1.color Next i Shape1.BorderColor = CommonDialog1.color DrawCircle 2500, 2500, 2400, 60, CommonDialog1.color End Sub

Private Sub Form_Load() 'LoadDots

12

DrawCircle 2500, 2500, 2400, 60 gettime End Sub

Private Sub Timer2_Timer() second = second + 1 theta = theta + dtheta line_minute.x2 = cx + (radius - 100) * Cos(stheta + (2 * PI / 3600) * second) line_minute.y2 = cy + (radius - 100) * Sin(stheta + (2 * PI / 3600) * second) If second Mod 60 = 0 Then theta = 4.7142857 min = min + 1

line_Hour.x2 = cx + (radius - 200) * Cos(stheta + (2 * PI / 3600) * min * 5) line_Hour.y2 = cy + (radius - 200) * Sin(stheta + (2 * PI / 3600) * min * 5)

If second = 43200 Then second = 0 End If

If min = 720 Then min = 0 End If

13

X = cx + radius * Cos(theta) Y = cy + radius * Sin(theta)

LineSec.x2 = X LineSec.y2 = Y End Sub

Private Sub DrawCircle(ByVal cx As Single, ByVal cy As Single, ByVal radius As Single, ByVal num_segments As Integer, Optional fcolor As ColorConstants) Const PI = 3.14159265 Dim X As Single Dim Y As Single Dim theta As Single Dim dtheta As Single Dim second As Integer

For i = 1 To Dot.Count - 1 Unload Dot(i) Next

dtheta = 2 * PI / num_segments theta = stheta

For seg = 1 To num_segments

14

theta = theta + dtheta

X = cx + radius * Cos(theta) Y = cy + radius * Sin(theta)

Load Dot(seg) Dot(seg).X1 = X Dot(seg).Y1 = Y Dot(seg).x2 = X + 2 Dot(seg).y2 = Y + 2 Dot(seg).BorderWidth = 3 If seg Mod 5 = 0 Then Dot(seg).BorderWidth = 5 Else Dot(seg).BorderColor = fcolor End If Dot(seg).Visible = True Next seg

End Sub

15

Practical 4

RESUME WRITER: Create an application such that it will collect the Personal, Educational, and Professional information from a candidate and creates resume in rich text format automatically.

Const RTF_NUMSTART As String = _

16

"{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;} {\f1\froman\fcharset2 Symbol;}{\f2\fnil\fprq2\fcharset2 Wingdings;} {\f3\froman\fprq2 Times New Roman;}}" & vbCrLf & _ "{\colortbl\red0\green0\blue0;}" & vbCrLf & _ "\deflang1033\pard\li720\fi-360\plain\f3\fs24" Const RTF_NUMITEM As String = _ "\par @%@.\tab " Const RTF_NUMEND As String = "\par }" Dim sex As String

Private Sub btn_more_edu_Click() Dim eindex As Integer eindex = tbox_course.Count

If eindex > 8 Then MsgBox ("Course completed") Exit Sub End If

Load tbox_course(eindex) With tbox_course(eindex) .Height = tbox_course(eindex - 1).Height 17

.Width = tbox_course(eindex - 1).Width .Left = tbox_course(eindex - 1).Left .Top = tbox_course(eindex - 1).Top + tbox_course(eindex - 1).Height + 100 .Text = "" .Visible = True End With

Load tbox_iname(eindex) With tbox_iname(eindex) .Height = tbox_iname(eindex - 1).Height .Width = tbox_iname(eindex - 1).Width .Left = tbox_iname(eindex - 1).Left .Top = tbox_iname(eindex - 1).Top + tbox_iname(eindex - 1).Height + 100 .Visible = True End With

Load tbox_stream(eindex) With tbox_stream(eindex) .Height = tbox_stream(eindex - 1).Height .Width = tbox_stream(eindex - 1).Width .Left = tbox_stream(eindex - 1).Left .Top = tbox_stream(eindex - 1).Top + tbox_stream(eindex - 1).Height + 100 .Visible = True End With

18

Load tbox_board(eindex) With tbox_board(eindex) .Height = tbox_board(eindex - 1).Height .Width = tbox_board(eindex - 1).Width .Left = tbox_board(eindex - 1).Left .Top = tbox_board(eindex - 1).Top + tbox_board(eindex - 1).Height + 100 .Visible = True End With

Load tbox_pyear(eindex) With tbox_pyear(eindex) .Height = tbox_pyear(eindex - 1).Height .Width = tbox_pyear(eindex - 1).Width .Left = tbox_pyear(eindex - 1).Left .Top = tbox_pyear(eindex - 1).Top + tbox_pyear(eindex - 1).Height + 100 .Visible = True End With

Load tbox_per(eindex) With tbox_per(eindex) .Height = tbox_per(eindex - 1).Height .Width = tbox_per(eindex - 1).Width .Left = tbox_per(eindex - 1).Left

19

.Top = tbox_per(eindex - 1).Top + tbox_per(eindex - 1).Height + 100 .Visible = True End With

If eindex > 4 Then Load Label9(eindex) With Label9(eindex) .Height = Label9(eindex - 1).Height .Width = Label9(eindex - 1).Width .Left = Label9(eindex - 1).Left .Top = tbox_course(eindex).Top .Caption = "Other" .Visible = True End With End If End Sub

Private Sub calendar_dob_DblClick() calendar_dob.Visible = False tbox_DOB.Text = calendar_dob.Value End Sub

Private Sub cmd_dob_Click() calendar_dob.Visible = Not calendar_dob.Visible

20

End Sub

Private Sub cmd_enext_Click() SSTab1.Tab = 2 End Sub

Private Sub cmd_more_Click() Dim pindex As Integer pindex = tbox_exper.Count

If pindex > 8 Then MsgBox ("Experience done") Exit Sub End If

Load tbox_exper(pindex) With tbox_exper(pindex) .Height = tbox_exper(pindex - 1).Height .Width = tbox_exper(pindex - 1).Width .Left = tbox_exper(pindex - 1).Left .Top = tbox_exper(pindex - 1).Top + tbox_exper(pindex - 1).Height + 100 .Visible = True End With

21

Load tbox_company(pindex) With tbox_company(pindex) .Height = tbox_company(pindex - 1).Height .Width = tbox_company(pindex - 1).Width .Left = tbox_company(pindex - 1).Left .Top = tbox_company(pindex - 1).Top + tbox_company(pindex - 1).Height + 100 .Visible = True End With

Load tbox_from(pindex) With tbox_from(pindex) .Height = tbox_from(pindex - 1).Height .Width = tbox_from(pindex - 1).Width .Left = tbox_from(pindex - 1).Left .Top = tbox_from(pindex - 1).Top + tbox_from(pindex - 1).Height + 100 .Visible = True End With

Load tbox_to(pindex) With tbox_to(pindex) .Height = tbox_to(pindex - 1).Height .Width = tbox_to(pindex - 1).Width .Left = tbox_to(pindex - 1).Left .Top = tbox_to(pindex - 1).Top + tbox_to(pindex - 1).Height + 100

22

.Visible = True End With

Load tbox_desig(pindex) With tbox_desig(pindex) .Height = tbox_desig(pindex - 1).Height .Width = tbox_desig(pindex - 1).Width .Left = tbox_desig(pindex - 1).Left .Top = tbox_desig(pindex - 1).Top + tbox_desig(pindex - 1).Height + 100 .Visible = True End With

Load tbox_tech(pindex) With tbox_tech(pindex) .Height = tbox_tech(pindex - 1).Height .Width = tbox_tech(pindex - 1).Width .Left = tbox_tech(pindex - 1).Left .Top = tbox_tech(pindex - 1).Top + tbox_tech(pindex - 1).Height + 100 .Visible = True End With

End Sub

Private Sub cmd_pnext_Click()

23

SSTab1.Tab = 1 End Sub

Private Sub cmd_pronext_Click() SSTab1.Tab = 3 build_resume End Sub

Function build_resume()

Dim str As String str = "" str = RTF_NUMSTART & " \qc \b " & tbox_name.Text & "\b0 \par " str = str & " \qc " & tbox_city.Text & ", " & tbox_state.Text & " \par " & "\b Mobile : " & tbox_mobile & " \par " & tbox_email.Text & " \par "

str = str & "\ql Summary" & " \b0 \par \bullet \tab " & "Quick Learner with the ability to work under pressure & willingness to enhance knowledge." & " \par \bullet \tab " &_ "Proficiency at grasping technical concept quickly and implementing them in an effective manner." & " \par \bullet \tab " & _ "Hard Working, Time Management, Punctuality & having good attitude." & " \par \par "

str = str & " \b Education \b0 \par " Dim i As Integer

24

Dim education As String i = tbox_course.Count While i > 0 i=i-1

If tbox_course(i).Text "" Then education = education & " \bullet\tab " & tbox_course(i).Text If tbox_stream(i).Text "" Then education = education & " in " & tbox_stream(i).Text End If education = education & " from " & tbox_iname(i).Text & " (" & tbox_board(i).Text & ") in " & tbox_pyear(i).Text & " with scoring " & tbox_per(i).Text & "%. \par " End If

Wend str = str & education Dim prof As String If tbox_exper(0).Text "" Then prof = prof & " \par\par \b Experience \b0\par"

i = tbox_exper.Count While i > 0 i=i-1 If tbox_exper(i).Text "" Then

25

prof = prof & " \bullet\tab " & tbox_exper(i).Text & " in " & tbox_tech(i).Text & ", from " & tbox_from(i).Text & " to " & tbox_to(i).Text prof = prof & " in " & tbox_company(i).Text & " at Designation " & tbox_desig(i).Text & " \par" End If

Wend End If str = str & prof str = str & "\par \b " & "Personal Detail" & " \b0\par" & " Father's Name :" & tbox_fname.Text & "\par" & " Date of Birth :" & tbox_DOB.Text & "\par Gender :" & sex & " \par" & " Marital Status :" & cbox_status.SelText & " \par " & " Address :" & tbox_address.Text & " \par " str = str & " \par\b Declaration:\b0\par " & " I here by declare that the above information are true to the best of my knowledge and belief and can be supported by reliable documents when needed." str = str & "\par\par Date: \tab\tab\tab\tab\tab\tab\tab\tab\tab (" & UCase(tbox_name.Text) & ")"

'rtb_resume.SelAlignment = vbLeftJustify rtb_resume.TextRTF = str & RTF_NUMEND End Function

Private Sub Form_Load() sex = "Male" cbox_status.ListIndex = 0

26

End Sub

Private Sub opt_sexFemale_Click() sex = "Female" End Sub

Private Sub opt_sexMale_Click() sex = "Male" End Sub

27

Practical 5

PICTURE VIEWER: Create a program that enables the users to open and choose files from the folders in their PC. The picture viewer provides the functionality to move to the NEXT and PREVIOUS photos, ZOOM IN & ZOOM OUT 28

Dim Index As Integer Dim path As String Dim X, Y As Integer

Private Sub bnt_prev_Click() If Index > 0 Then Index = Index - 1 End If

path = File1.path If Right(path, Len(path) - 2) "\" Then path = path & "\" End If PictureViewer.Picture1.Picture = LoadPicture(path & File1.List(Index)) PictureViewer.Picture1.PaintPicture PictureViewer.Picture1.Picture, 0, 0, 800, 600 End Sub

Private Sub btn_next_Click() If Index < File1.ListCount - 1 Then Index = Index + 1

path = File1.path If Right(path, Len(path) - 2) "\" Then

29

path = path & "\" End If Picture1.Picture = LoadPicture(path & File1.List(Index)) Picture1.PaintPicture Picture1.Picture, 0, 0, 800, 600 Picture2.Picture = LoadPicture(path & File1.List(Index)) End If

End Sub

Private Sub btn_open_Click() openFolderDialog.Show 1 End Sub

Private Sub btn_zoomin_Click()

If Picture1.Picture = 0 Then Exit Sub End If

X = X + 80 Y = Y + 60 Picture1.Height = Y Picture1.Width = X Picture1.PaintPicture Picture1.Picture, 0, 0, X, Y

30

If X > Pic_holder.ScaleWidth Then

HScroll1.Max = X - Pic_holder.ScaleWidth VScroll1.Max = Y - Pic_holder.ScaleHeight Else HScroll1.Max = 0 VScroll1.Max = 0 End If

End Sub

Private Sub btn_zoomout_Click() If Picture1.Picture = 0 Then Exit Sub End If

If X > 160 Then X = X - 80 Y = Y - 60 End If Picture1.Height = Y Picture1.Width = X Picture1.PaintPicture Picture1.Picture, 0, 0, X, Y

31

If Picture1.ScaleWidth - Pic_holder.ScaleWidth > 0 Then HScroll1.Max = Picture1.ScaleWidth - Pic_holder.ScaleWidth VScroll1.Max = Picture1.ScaleHeight - Pic_holder.ScaleHeight Else HScroll1.Max = 0 VScroll1.Max = 0 End If End Sub

Private Sub Form_Load() X = 800 Y = 600

'Set up picturebox With Picture1 .ScaleMode = vbPixels .AutoRedraw = True

.DrawMode = vbInvert .DrawStyle = SelDrawType .BackColor = vbWhite .MousePointer = vbCrosshair

.Left = 0

32

.Top = 0 .ScaleHeight = Pic_holder.ScaleHeight .ScaleWidth = Pic_holder.ScaleWidth End With Picture2.ScaleHeight = Picture1.ScaleHeight Picture2.ScaleWidth = Picture1.ScaleWidth

HScroll1.Max = Picture1.ScaleWidth - Pic_holder.ScaleWidth HScroll1.min = 0

VScroll1.Max = Picture1.ScaleHeight - Pic_holder.ScaleHeight HScroll1.min = 0 End Sub

Private Sub HScroll1_Change() Picture1.Left = -HScroll1.Value

End Sub

Private Sub VScroll1_Change() Picture1.Top = -VScroll1.Value End Sub

33

34

Practical 6

TIC- TAC- TOE: Develop a program Tic Tac Toe in VB so that you can play the game virtually. First of all, you need to draw the interface with four straight lines, then insert nine image controls and make them an array, Image1 (0) to Image1 (8). Secondly, insert two pictures, one is a circle and the other one is a cross to represent player 1 and player2. Dim sym As String Dim winr(9) As String Private Sub Form_Load() sym = "X" Dim i As Integer For i = 0 To 8 winr(i) = i Next i End Sub

Private Sub Label1_Change() If Label1.Caption = "Player 1" Then

35

sym = "X" Else sym = "0" End If End Sub

Private Sub lbl_gstatus_Click(Index As Integer) If lbl_gstatus(Index).Caption = "" Then lbl_gstatus(Index).Caption = sym winr(Index) = sym If Label1.Caption = "Player 1" Then Label1.Caption = "Player 2" Else Label1.Caption = "Player 1" End If check End If End Sub

Function check() Dim i As Integer Dim won As Boolean Dim psymbol As String If winr(0) = winr(1) And winr(1) = winr(2) Then

36

won = True psymbol = winr(0) ElseIf winr(2) = winr(4) And winr(4) = winr(6) Then won = True psymbol = winr(2) ElseIf winr(0) = winr(3) And winr(3) = winr(6) Then won = True psymbol = winr(0) ElseIf winr(1) = winr(4) And winr(4) = winr(7) Then won = True psymbol = winr(1) ElseIf winr(2) = winr(5) And winr(5) = winr(8) Then won = True psymbol = winr(2) ElseIf winr(0) = winr(1) And winr(1) = winr(2) Then won = True psymbol = winr(0) ElseIf winr(3) = winr(4) And winr(4) = winr(5) Then won = True psymbol = winr(3) ElseIf winr(6) = winr(7) And winr(7) = winr(8) Then won = True psymbol = winr(6) End If

37

If won = True Then Dim pno As Integer If psymbol = "X" Then pno = 1 Else pno = 2 End If MsgBox (" Player " & pno & " won") won = False For i = 0 To 8 winr(i) = i lbl_gstatus(i).Caption = "" Label1.Caption = "Player 1" Next i End If End Function

38

39

Practical 7

A DRAG & DROP RECYCLE_BIN: Make a drag and drop program that resembles a recycle bin. Drag and drop is a common windows application where you can drag and drop an object such as a file into a folder or into a recycle bin. This capability can be easily programmed in visual basic

Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single) Source.Visible = False End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then PopupMenu RestoreMenu, vbPopupMenuRightAlign End If

40

End Sub

Private Sub Restore_Click() Image1.Visible = True Image2.Visible = True Image3.Visible = True Image4.Visible = True End Sub

41

Practical 8

TRAFFIC LIGHT: Create a Traffic light program in which, one can insert one timer and sets its interval according to its desire. Suppose 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.

42

Dim status(4) As Boolean Const interval As Integer = 1000 Dim wait As Integer Dim waittime(3) As Integer Dim stage, t1, t2, t3, t4 As Integer

Private Sub Command1_Click() Me.FillColor = vbRed Me.FillStyle = vbSolid

Call light(250, 50, 1) Call light(100, 200, 1) Call light(400, 200, 1) Call light(250, 300, 1) waittime(0) = 10 waittime(1) = 4 waittime(2) = 8 Timer2.Enabled = True Timer1.Enabled = True t1 = waittime(0) + waittime(1) t2 = (waittime(1) + waittime(2)) * 2 + 1 t3 = (waittime(1) + waittime(2)) * 3 + 1 t4 = (waittime(1) + waittime(2)) * 4 + 1

43

Label1.Left = 320 Label1.Top = 50 Label2.Left = 320 Label2.Top = 300 Label3.Left = 170 Label3.Top = 200 Label4.Left = 470 Label4.Top = 200 wait = 10 stage = 0 End Sub Function light(X As Integer, Y As Integer, Optional c As Integer)

Call rectangle(X, Y, X + 50, Y + 100) Call fillcircle(X + 25, Y + 20, vbWhite) Call fillcircle(X + 25, Y + 50, vbWhite) Call fillcircle(X + 25, Y + 80, vbWhite)

Select Case c Case 1: Call fillcircle(X + 25, Y + 20, vbRed) Case 2: Call fillcircle(X + 25, Y + 50, vbYellow) Case 3:

44

Call fillcircle(X + 25, Y + 80, vbGreen) End Select End Function

Function rectangle(X As Integer, Y As Integer, x2 As Integer, y2 As Integer) Me.Line (X, Y)-(X, y2), vbRed Me.Line (x2, Y)-(x2, y2), vbRed Me.Line (X, Y)-(x2, Y), vbRed Me.Line (X, y2)-(x2, y2), vbRed

End Function Function fillcircle(X As Integer, Y As Integer, color As ColorConstants) Me.FillColor = color Me.Circle (X, Y), 10, vbBlack End Function

Private Sub Timer1_Timer()

t1 = t1 - 1 t2 = t2 - 1 t3 = t3 - 1 t4 = t4 - 1

45

Label1.Caption = t1 Label2.Caption = t2 Label3.Caption = t3 Label4.Caption = t4

End Sub

Private Sub Timer2_Timer() stage = stage + 1 Select Case stage Case 1: Call light(250, 50, 2) Timer2.interval = interval * waittime(1) Case 2: Call light(250, 50, 3) Call light(400, 200, 1) t1 = waittime(2) Timer2.interval = interval * waittime(2) Case 3: Call light(250, 50, 2) Call light(250, 300, 2) t1 = (waittime(1) + waittime(2)) * 3 + waittime(1) Timer2.interval = interval * waittime(1) Case 4:

46

Call light(250, 50, 1) Call light(250, 300, 3) t2 = waittime(2) Timer2.interval = interval * waittime(2) Case 5: Call light(100, 200, 2) Call light(250, 300, 2) t2 = (waittime(1) + waittime(2)) * 3 + waittime(1) Timer2.interval = interval * waittime(1) Case 6: Call light(100, 200, 3) Call light(250, 300, 1) t3 = waittime(2) Timer2.interval = interval * waittime(2) Case 7: Call light(100, 200, 2) Call light(400, 200, 2) t3 = (waittime(1) + waittime(2)) * 3 + waittime(1) Timer2.interval = interval * waittime(1) Case 8: Call light(100, 200, 1) Call light(400, 200, 3) t4 = waittime(2) Timer2.interval = interval * waittime(2)

47

Case 9: Call light(250, 50, 2) Call light(400, 200, 2) t4 = (waittime(1) + waittime(2)) * 3 + waittime(1) Timer2.interval = interval * waittime(1) stage = 1 End Select

End Sub

48

Practical 9

CUBIC FUNCTION GRAPH PLOTTER: Make a program that enables users to input the coefficients of a cubic equation and draw its graph. The cubic equation takes the form ax3+bx2+cx+d.

Private Sub btn_cls_Click() pic_graph.Cls End Sub

Private Sub cmd_draw_Click() Dim a, b, c, d As Integer Dim w, v As Double a = Val(tbox_a.Text) b = Val(tbox_b.Text) c = Val(tbox_c.Text) d = Val(tbox_d.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 49

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

Private Sub Form_Load() Line_x.X1 = 0 Line_x.x2 = pic_graph.ScaleWidth Line_x.Y1 = pic_graph.ScaleHeight / 2 Line_x.y2 = pic_graph.ScaleHeight / 2

Line_y.X1 = pic_graph.ScaleWidth / 2 Line_y.x2 = pic_graph.ScaleWidth / 2 Line_y.Y1 = 0 Line_y.y2 = pic_graph.ScaleHeight End Sub

50

51

Practical 10

GEOMETRIC PROGRESSION: Make a Visual Basic program that can compute a geometric progression and display the results in a list box.

Private Sub btn_sum_Click() Dim a, n As Integer Dim r As Single If check() = False Then MsgBox ("Required fields are empty") Exit Sub End If If opt_inputbox.Value Then a = InputBox("First Term", "Geometric Progression", 1)

52

r = InputBox("Common Ratio", "Geometric Progression", 1) n = InputBox("How many terms", "Geometric Progression", 1) Else a = tbox_fn.Text r = tbox_r.Text n = tbox_terms.Text End If Dim s As Single If r > 1 Then s = (a * ((r ^ n) - 1)) / (r - 1) Else s = (a * (1 - (r ^ n))) / (1 - r) End If MsgBox ("The sum is " & s) End Sub

Private Sub btn_term_Click() Dim a, n As Integer Dim r As Single If check() = False Then MsgBox ("Required fields are empty") Exit Sub End If If opt_inputbox.Value Then

53

a = InputBox("First Term", "Geometric Progression", 1) r = InputBox("Common Ratio", "Geometric Progression", 1) n = InputBox("Which Term you want to display", "Geometric Progression", 1) Else a = tbox_fn.Text r = tbox_r.Text n = tbox_terms.Text End If MsgBox ("The " & n & "th term is " & a * (r ^ (n - 1))) End Sub

Private Sub cmd_Clear_Click() List1.Clear End Sub

Private Sub cmd_series_Click() If check() = False Then MsgBox ("Required fields are empty") Exit Sub End If

cmd_Clear_Click Dim f, n, terms As Integer Dim r As Single

54

f = tbox_fn.Text r = tbox_r.Text terms = tbox_terms.Text List1.AddItem "Term" & vbTab & "Value" List1.AddItem "-----------------------" n=1 Do List1.AddItem n & vbTab & f * (r ^ (n - 1)) n=n+1 Loop While n = File1.ListCount Then i=0

76

End If End Sub

Private Sub btn_Stop_Click() If btn_Stop.Caption = "Resume" Then MMControl1.Command = "Play" btn_Stop.Caption = "Pause" Else MMControl1.Command = "Pause" btn_Stop.Caption = "Resume" End If End Sub

Private Sub cbox_pattern_Click() If cbox_pattern.ListIndex = 0 Then File1.Pattern = "*.wav;*.midi" Else File1.Pattern = "*.mp3;*.wav;*.mp4;*.mpeg;*.avi;*.dat" End If End Sub

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

77

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

Private Sub File1_Click() i = File1.ListIndex btn_Play_Click End Sub

Private Sub Form_Load() File1.Pattern = "*.midi;*.wav" MMControl1.PlayEnabled = True MMControl1.AutoEnable = True End Sub

Private Sub Timer1_Timer() Slider1.Value = Round(MMControl1.Position / MMControl1.Length * 100) End Sub

78

79

Practical 15

MDI FORM: Write a program to demonstrate the MDI forms application. The parent form contains the menu links to all the above programs and calls them as a child form.

Private Sub AnalogClock_Click() Dim ob As New Clock ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub AudioPlayer_Click() Dim ob As New AudioPlayer

80

ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub DragDrop_Click() Dim ob As New Drag_Drop ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub DragDrop2_Click() Dim ob As New draganddrop2 ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub DynamicControl_Click() Dim ob As New DynamicControl ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub DynamicMenu_Click()

81

Dim ob As New DynamicMenu ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub GeometricProgression_Click() 'Dim ob As New Geometric_Progression 'ob.BorderStyle = vbFixedDialog 'ob.Show Geometric_Progression.Show 1, Me End Sub

Private Sub PCubic_Click() Dim ob As New CubicFunction ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub PDB_app_Click() Dim ob As New DB_Application ob.BorderStyle = vbFixedDialog ob.Show

82

End Sub

Private Sub PDrawing_Click() Dim ob As New DrawProg ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub PictureViewer_Click() Dim ob As New PictureViewer ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub PIncomeTax_Click() Dim ob As New IncomeTax ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub PSDBSystem_Click() Dim ob As New Db_System

83

ob.BorderStyle = vbFixedDialog ob.Show

End Sub

Private Sub ResumeWriter_Click() Dim ob As New ResumeWriter ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub TicTacToe_Click() Dim ob As New TicTacToe ob.BorderStyle = vbFixedDialog ob.Show End Sub

Private Sub TraficLight_Click() Dim ob As New Trafic_Light ob.BorderStyle = vbFixedDialog ob.Show End Sub

84

85

Practical 16

Develop a program which accepts total taxable income of a person compute income tax as per rules given below:The income tax calculation sheet for salaried person is Taxable income Rs. rate% Up to 110000 nil Up to 145000 women nil 110000 to 150000 10 150000 to 250000 20 250000 to 1000000 30 1000000 upwards 30* * a surcharge of 10% of the total tax liability is applicable. Here the total income exceeds rate 1000000. Eduction cess is applicable at the rate 3% on income tax, inclusive of such, age if there is any. Dim sex As String Private Sub btn_exit_Click() Unload Me End 86

End Sub Private Sub opt_female_Click() sex = "female" End Sub Private Sub btn_tax_Click() Dim income, tax As Double income = Val(tbox_income.Text) tax = 0 If income 150000 Then If sex = "female" Then tax = 500 Else tax = 4000 End If End If Select Case income Case Is 10 and deptnum=5)); b.) Find the names of employees who are directly supervised by supervisor (say Neetu).

93

Select fname from employee where supno in (select eno from employee where fname='neethu'); c.) Retrieve the names of employee who work on every project controlled by a department (say deptno=5).

select fname, lname from employee where not exists (select * from works_on w where (w.pno IN (select pno from project WHERE Deptnum = 5)) AND NOT EXISTS (select * from works_on x where x.eno = employee.eno AND x.PNO = w.PNO)); d.) Retrieve the names of each employee who has a dependent with the same first name of employee. Select fname , lname as 'Name' from employee where eno in (select eno from dependent where fname=deptname); e.) Retrieve the name of each employ who have no dependents. Select fname,lname from employee where NOT EXISTS(select * from dependent where employee.eno=dependent.eno); f.) Retrieve the name of each employ who work on the projects say 1,2,3 Select distinct works_on.eno, fname from works_on,employee works_on.eno=employee.eno; where pno in(1,2,3) and

94

Practical 3 Consider the following database maintained by a book dealer Author(authorid,name,city,country) Publisher(publisherid,name,city,country) Catalog(bookid, title,authorid, publisherid, categoryid, year,price)

Category(categoryid, description) Orderdetails(orderno, bookid, quantity) Create the above tables properly by specifying the primary keys and foreign keys. Enter altleast 4 to 5 tuples for each table. Now construct the following SQL queries for this relational database. create table author( authorid int primary key, name varchar(30), city varchar(30), country varchar(30) );

95

create table publisher( publisherid int primary key, name varchar(30), city varchar(30), country varchar(30) ); create table catalogs( bookid int primary key, title varchar(20), authorid int, publisherid int, categoryid int, year int, price numeric(8,2) foreign key(authorid) references author(authorid), foreign key (publisherid) references publisher(publisherid), foreign key(categoryid) references category(categoryid) ); create table category( categoryid int primary key, description varchar(20) ); create table orderdetails( orderno int primary key, bookid int references catalogs(bookid), quantity int ); a) Give the details of the author who have two or more books in the catalog and the price of the books is greater than the average price of the books in the catalog and the year of publication is after 2000. select clog.Authorid,A.name from Catalogs clog,Author A where A.Authorid=Clog.Authorid and Clog.Year > 2000 and Clog.Price >(Select Avg(Price) from Catalogs) group by clog.Authorid,A.name having count(clog.Authorid)>=2 b) Find the author of the book which has maximum sales. create view salesdetails as( Select OD.Bookid as Book#,C.Price as Cost,Sum(OD.quantity) as Qty,

96

sum(OD.quantity*C.price) as sales from Orderdetails OD,Catalogs C,Author A where OD.Bookid=C.Bookid and C.Authorid=A.Authorid group by OD.Bookid,C.Price); select A.Authorid,A.name,S.Book#,S.Sales from Author A,Catalogs C,Salesdetails S where A.Authorid=C.Authorid and S.Book#=C.Bookid and sales=( select Max(Sales) from Salesdetails);

c) Give the details of the top 5 authors who have published maximum number of books. create view booksnum as(select count(*) as nbooks,authorid from catalogs group by authorid ) select author.authorid,author.name,author.city,author.country from author where authorid in (select top 5 authorid from booksnum order by nbooks desc) d) Give the details of the author who have published books of a particular category (say Economics). select author.authorid,author.name,author.city,author.country from author,catalogs,category where description='Economics' and category.categoryid=catalogs.categoryid and author.authorid=catalogs.authorid Practical 4 Consider the following database Employee(empno,name,office,age) Books(isbn, title,authors,publisher) Loan(empno,isbn,date) Now construct the following SQL queries for this relational database. create table employee( empno int primary key, name varchar(30), office varchar(20), age int ); create table books( isbn int primary key, title varchar(30),

97

authors varchar(20), publisher varchar(30) ); create table loan( empno int references employee(empno), isbn int references books(isbn) , date smalldatetime); a.) Find the names of the employee who have borrowed a book published by a particular publisher (say McGraw-Hill). select name from employee,books,loan where publisher='McGraw-Hill' and books.isbn=loan.isbn and loan.isbn=employee.empno

b.) Find the names of employee who have borrowed all the books published by a particular publisher( say McGraw-Hill) select name from employee e join loan l on e.empno=l.empno join (select isbn from books where authors = 'Smith') x on l.isbn=x.isbn group by e.empno,name having count(*)= (select count(*) from books where authors = 'Smith') c.) Find the names of employees who have borrowed more than five different books published by a particular publisher( say McGraw-Hill) select name from employee,loan where employee.empno=loan.empno and isbn in ( select distinct isbn from books where publisher='McGraw-Hill') group by employee.empno,name having count(isbn) >=5 d.) For each publisher, find the names of employees who have borrowed more than five books of that publisher. select name from employee,loan,books where employee.empno=loan.empno and books.isbn=loan.isbn group by employee.empno,name,books.publisher having count(loan.isbn) >=5

98

Practical 5 Consider the following database which stores the student and course information Student(name,student_id,class,major) Course(course_name,course_id,credit_hours,department) Section(section_id, course_id,Semester,year,instructor) Grade_report(student_id,section_id,grade) Prerequisite(course_id,prerequisite_number) Now construct the following queries in SQL on the database tables create table student( student_id int primary key,

99

name varchar(20), class varchar(20), major varchar(20) ); create table course ( course_id varchar(20) primary key, course_name varchar(30), credit_hours int, department varchar(20) ); create table section( section_id int, course_id varchar(20), semester int, year int, instructor varchar(30), primary key(section_id), foreign key (course_id) references course(course_id) ); create table grade_report( student_id int, section_id int, grade varchar(10) primary key(section_id,student_id), foreign key (student_id) references student(student_id), foreign key (section_id) references section(section_id) ); create table prerequisite ( course_id varchar(20) references course(course_id), prerequisite_number varchar(20) references course(course_id), primary key (course_id,prerequisite_number) ); a.) Retrieve the names of all senior students majoring in CS. select * from student where major='CS'

100

b.) Retrieve the name of all courses taught by particular instructor (say Dr. Rajender Nath) in particular year (say 2010). select course_name from course,section where course.course_id=section.course_id and year=2010 and instructor='Dr. Rajendar Nath' c.) For each section taught by particular instructor (say Dr. Rajender Nath) retrieve the course_id, semester, year, and number of students who took the section. select section.course_id,semester,year, count(*) from course,section where course.course_id=section.course_id and instructor='Dr. Rajendar Nath' group by section_id d.) Retrieve the name and major departments of all straight A students (students who have a grade of A in all their courses). select name,major,department from student,course,grade_report,section where student.student_id=grade_report.student_id and section.section_id=grade_report.section_id and course.course_id=section.course_id and grade='A' e.) Retrieve the names and major department of all students who do not have a grade of A in any of their courses. select name,major,department from student,course,grade_report,section where student.student_id=grade_report.student_id and section.section_id=grade_report.section_id and course.course_id=section.course_id and grade 'A'

101