0% found this document useful (0 votes)
95 views35 pages

VB Practical File

The document contains a list of 10 programs to be written in Visual Basic to demonstrate various concepts. The programs include: 1. Designing a calculator application using Visual Basic. 2. Using checkboxes and option buttons in a program. 3. Performing drag and drop operations. 4. Demonstrating the use of a rich text box. 5. Using common dialog boxes. 6. Creating a program based on timers. 7. Demonstrating menu design. 8. Using multiple document interface forms. 9. Accessing child forms in an MDI form. 10. Accessing data through data controls.

Uploaded by

ashu nain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views35 pages

VB Practical File

The document contains a list of 10 programs to be written in Visual Basic to demonstrate various concepts. The programs include: 1. Designing a calculator application using Visual Basic. 2. Using checkboxes and option buttons in a program. 3. Performing drag and drop operations. 4. Demonstrating the use of a rich text box. 5. Using common dialog boxes. 6. Creating a program based on timers. 7. Demonstrating menu design. 8. Using multiple document interface forms. 9. Accessing child forms in an MDI form. 10. Accessing data through data controls.

Uploaded by

ashu nain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

PRACTICAL FILE

VISUAL BASIC PROGRAMMING


SOFTWARE LAB – II (CS DE37)

SUBMITTED TO : Directorate of Distance Education


Kurukshetra University , Kurukshetra

SUBMITTED BY : Ashu Nain, MCA II


Roll No. – 2291413101
Regn No. – 20-DE-8878
LIST OF PROGRAMS

1. Write a program to design a calculator using VB.


2. Write a program to use check box and option button.
3. Write a program to perform the Drag & Drop operation.

4. Write a program to show the use of rich text box.

5. Write a program to show the use of common dialog box.

6. Write a program based on timer.


7. Write a program to show the use of menu design.
8. Write a program to show the use of MDI (Multiple document interface)
form.

9. Write a program to access the child form in MDI form.

10. Write a program to access data through data control form design.
1. Write a program to design a calculator using VB.
Design window

Dim val1 As Double


Dim val2 As Double
Dim opt As String
Dim cleardisp As Boolean
Private Sub cmdAdd_Click()
val1 = Val(display.Text)
opt = "+"
display.Text = ""
End Sub
Private Sub cmdDiv_Click()
val1 = Val(display.Text)
opt = "/"
display.Text = ""
End Sub
Private Sub cmdDot_Click()
If InStr(display.Text, ".") Then
MsgBox "Don't Press Point Again", vbInformation, "!!Error"
Else
display.Text = display.Text + "."
End If
End Sub
Private Sub cmdEqual_Click()
Dim result As Double
val2 = Val(display.Text)
If opt = "+" Then result = val1 + val2
If opt = "-" Then result = val1 - val2
If opt = "X" Then result = val1 * val2
If opt = "/" And val2 <> 0 Then result = val1 / val2
If opt = "sqr" Then result = val1 * val1
If opt = "sqrt" Then result = Sqr(val1)
display.Text = result
End Sub
Private Sub cmdMul_Click()
val1 = Val(display.Text)
opt = "X"
display.Text = ""
End Sub
Private Sub cmdPercent_Click()
val1 = Val(display.Text)
opt = "1/X"
display.Text = ""
End Sub
Private Sub cmdSqrt_Click()
val1 = Val(display.Text)
opt = "sqr"
display.Text = ""
End Sub
Private Sub cmdsqr_Click(Index As Integer)
val1 = Val(display.Text)
opt = "sqr"
display.Text = ""
End Sub
Private Sub cmdSqroot_Click(Index As Integer)
val1 = Val(display.Text)
opt = "sqrt"
display.Text = ""
End Sub
Private Sub cmdSub_Click()
val1 = Val(display.Text)
opt = "-"
display.Text = ""
End Sub
Private Sub CommandClear_Click()
display.Text = ""
End Sub
Private Sub digit_Click(Index As Integer)
If cleardisp Then
display.Text = ""
cleardisp = False
End If
display.Text = display.Text + digit(Index).Caption
End Sub
Private Sub Form_Load()
End Sub

OUTPUT :

:
2. Write a program to use check box and option button.
Design Window

Private Sub Check1_Click()


If (Check1.Value = 1) Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
End Sub
Private Sub Check2_Click()
If (Check2.Value = 1) Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
End Sub
Private Sub Check3_Click()
If (Check3.Value = 1) Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
Private Sub Form_Load()
End Sub
Private Sub Option1_Click()
Text1.FontSize = 10
End Sub
Private Sub Option2_Click()
Text1.FontSize = 12
End Sub
Private Sub Option3_Click()
Text1.FontSize = 14
End Sub
Private Sub Option4_Click()
Text1.ForeColor = vbBlue
End Sub
Private Sub Option5_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub Option6_Click()
Text1.ForeColor = vbRed
End Sub

Output
3. Write a program to perform the Drag & Drop operation.
Design Window

Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)


Picture1.BackColor = vbBlue
End Sub
Private Sub picture1_dragover(Source As Control, X As Single, Y As Single, state As
Integer)
If state = 0 Then
Picture1.BackColor = vbRed
If state = 2 Then
Form1.Caption = "source control moves over the picture box"
If state = 1 Then
Picture1.BackColor = vbGreen
Form1.Caption = "drag & drop demo"
End If
End Sub
4. Write a program to show the use of rich text box.
Design Window

Dim a As Variant
Private Sub Command1_Click()
RichTextBox1.SelBold = True
End Sub
Private Sub Command10_Click()
MsgBox (RichTextBox1.SelStart)
End Sub
Private Sub Command11_Click()
RichTextBox1.SelLength = Len(RichTextBox1.Text)
MsgBox (RichTextBox1.SelLength)
End Sub
Private Sub Command2_Click()
RichTextBox1.SelItalic = True
End Sub
Private Sub Command3_Click()
RichTextBox1.SelBold = False
RichTextBox1.SelItalic = False
End Sub
Private Sub Command4_Click()
RichTextBox1.Text = " "
End Sub
Private Sub Command5_Click()
RichTextBox1.FileName = "C:\Documents and Settings\Administrator.MCA-01\My
Documents\rr3.rtf"
End Sub
Private Sub Command6_Click()
a = RichTextBox1.SelRTF
RichTextBox1.Text = a
End Sub
Private Sub Command7_Click()
a = RichTextBox1.SelText
Print a
End Sub
Private Sub Command8_Click()
RichTextBox1.SelText = UCase(RichTextBox1.SelText)
Print a
End Sub
Private Sub Command9_Click()
RichTextBox1.SelText = "revised string"
End Sub
Output:
5. Write a program to show the use of common dialog box.
Design Window

Public newform As frmmain


Private Sub MDIForm_Initialize()
mnueditpaste.Enabled = False
mnueditcut.Enabled = False
mnueditcopy.Enabled = False
mnueditselall.Enabled = False
mnufilesaveas.Enabled = False
standard.Buttons("cut").Enabled = False
standard.Buttons("copy").Enabled = False
standard.Buttons("paste").Enabled = False
standard.Buttons("save").Enabled = False
End Sub
Private Sub MDIForm_Load()
frmmain.Hide
End Sub
Private Sub mnubold_Click()
If newform.txtpad.FontBold = True Then
newform.txtpad.FontBold = False
Else
newform.txtpad.FontBold = True
End If
End Sub
Private Sub mnubolditalic_Click()
newform.txtpad.FontBold = True
newform.txtpad.FontItalic = True
End Sub
Private Sub mnucolor_Click()
CommonDialog1.ShowColor
newform.txtpad.BackColor = CommonDialog1.Color
End Sub
Private Sub mnueditcopy_Click()
Clipboard.SetText mdiform1.ActiveForm.txtpad.SelText
mnueditpaste.Enabled = True
standard.Buttons("paste").Enabled = True
End Sub
Private Sub mnueditcut_Click()
Clipboard.SetText mdiform1.ActiveForm.txtpad.SelText
mdiform1.ActiveForm.txtpad.SelText = ""
mnueditpaste.Enabled = True
standard.Buttons("paste").Enabled = True
End Sub
Private Sub mnueditpaste_Click()
mdiform1.ActiveForm.txtpad.SelText = Clipboard.GetText
End Sub
Private Sub mnueditselall_Click()
mdiform1.ActiveForm.txtpad.SelStart = 0
mdiform1.ActiveForm.txtpad.SelLength = Len(mdiform1.ActiveForm.txtpad.Text)
mdiform1.ActiveForm.txtpad.SetFocus
End Sub
Private Sub mnufileexit_Click()
End
End Sub
Private Sub mnufilenew_Click()
Set newform = New frmmain
newform.txtpad.Text = ""
newform.Caption = "untitled" & Str(Forms.Count - 2)
mnueditcut.Enabled = True
mnueditcopy.Enabled = True
mnufilesaveas.Enabled = True
mnueditselall.Enabled = True
standard.Buttons("cut").Enabled = True
standard.Buttons("copy").Enabled = True
standard.Buttons("save").Enabled = True
sbarnpad.Panels("file").Text = mdiform1.ActiveForm.Caption
newform.Show
End Sub
Private Sub mnufileopen_Click()
CommonDialog1.ShowOpen
End Sub
Private Sub mnufilesaveas_Click()
CommonDialog1.ShowSave
End Sub
Private Sub mnuitalic_Click()
If newform.txtpad.FontItalic = True Then
newform.txtpad.FontItalic = False
Else
newform.txtpad.FontItalic = True
End If
End Sub
Private Sub mnuregular_Click()
newform.txtpad.FontBold = False
newform.txtpad.FontItalic = False
End Sub
Private Sub mnuunderline_Click()
If newform.txtpad.FontUnderline = True Then
newform.txtpad.FontUnderline = False
Else
newform.txtpad.FontUnderline = True
End If
End Sub
Private Sub mnuwincas_Click()
mdiform1.Arrange vbCascade
End Sub
Private Sub mnuwinth_Click()
mdiform1.Arrange vbTileHorizontal
End Sub
Private Sub standard_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "new"
mnufilenew_Click
Case "open"
mnufileopen_Click
Case "save"
mnufilesaveas_Click
Case "cut"
mnueditcut_Click
Case "copy"
mnueditcopy_Click
Case "paste"
mnueditpaste_Click
End Select
End Sub
Private Sub Timer1_Timer()
Dim num As Integer
num = Forms.Count - 2
If num > 0 Then
sbarnpad.Panels("file").Text = mdiform1.ActiveForm.Caption
Else
sbarnpad.Panels("file").Text = "enjoy mdi notepad"
mnueditpaste.Enabled = False
mnueditcut.Enabled = False
mnueditcopy.Enabled = False
mnueditselall.Enabled = False
mnufilesaveas.Enabled = False
mnufilesaveas.Enabled = False
standard.Buttons("cut").Enabled = False
standard.Buttons("copy").Enabled = False
standard.Buttons("paste").Enabled = False
standard.Buttons("save").Enabled = False
End If
End Sub
output:
6. Write a program based on timer.
Design Window
Option Explicit
Dim a, b, l, t, c, col As Integer
Private Sub exit_Click()
End
End Sub
Private Sub Form_Load()
l=0b=0t=0
Timer1.Enabled = False
Text1.Text = " VISUAL BASIC"
Text1.FontBold = True
Text1.BackColor = vbRed
Text1.ForeColor = vbYellow
Text1.FontSize = "20"
Form5.Caption = "timer"
Form5.WindowState = 2
End Sub
Private Sub start_Click()
Timer1.Enabled = True
End Sub
Private Sub stop_Click()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Time
Label1.Caption = "no. of records" & c
If (col < 15) Then
Text1.ForeColor = QBColor(col)
col = col + 1 Else
col = 1 End If
If (Text1.Left < 12000 And t = 0) Then
Text1.Top = 100
Text1.Left = Text1.Left + 100
ElseIf (Text1.Left > 9700 And t = 0 And Text1.Top < 9500) Then
Text1.Top = Text1.Top + 100
l=1b=0
ElseIf (Text1.Left >= 1000 And b = 0) Then
Text1.Top = 9400
Text1.Left = Text1.Left - 100
t=1
ElseIf (Text1.Top > 500 And b = 0 And t = 1) Then
Text1.Left = 50
Text1.Top = Text1.Top - 100
Else
c=c+1
t=0b=0l=0
End If End Sub
Output:
7. Write a program to show the use of menu design.
Design Window

Private Sub abcolorblue_Click()


abcolorblue.Checked = True
abcolorgreen.Checked = False
abcolorred.Checked = False
abcoloryellow.Checked = False
If rectangle.FillStyle = 0 Then
rectangle.FillColor = vbBlue
ElseIf cicle.FillStyle = 0 Then
cicle.FillColor = vbBlue
ElseIf Square.FillStyle = 0 Then
Square.FillColor = vbBlue
End If
End Sub
Private Sub abcolorgreen_Click()
abcolorblue.Checked = False
abcolorgreen.Checked = True
abcolorred.Checked = False
abcoloryellow.Checked = False
If rectangle.FillStyle = 0 Then
rectangle.FillColor = vbGreen
ElseIf cicle.FillStyle = 0 Then
cicle.FillColor = vbGreen
ElseIf Square.FillStyle = 0 Then
Square.FillColor = vbGreen
End If
End Sub
Private Sub abcolorred_Click()
abcolorblue.Checked = False
abcolorgreen.Checked = False
abcolorred.Checked = True
abcoloryellow.Checked = False
If rectangle.FillStyle = 0 Then
rectangle.FillColor = vbRed
ElseIf cicle.FillStyle = 0 Then
cicle.FillColor = vbRed
ElseIf Square.FillStyle = 0 Then
Square.FillColor = vbRed
End If
End Sub
Private Sub abcoloryellow_Click()
abcolorblue.Checked = False
abcolorgreen.Checked = False
abcolorred.Checked = False
abcoloryellow.Checked = True
If rectangle.FillStyle = 0 Then
rectangle.FillColor = vbYellow
ElseIf cicle.FillStyle = 0 Then
cicle.FillColor = vbYellow
ElseIf Square.FillStyle = 0 Then
Square.FillColor = vbYellow
End If
End Sub
Private Sub abfileexit_Click()
End
End Sub
Private Sub abshapecircle_Click()
abshapecircle.Checked = True
abshapesquare.Checked = False
abshaperectangle.Checked = False
cicle.FillStyle = 0
rectangle.FillStyle = 1
Square.FillStyle = 1
rectangle.FillColor = &H0&
Square.FillColor = &H0&
End Sub
Private Sub abshaperectangle_Click()
abshapecircle.Checked = False
abshapesquare.Checked = False
abshaperectangle.Checked = True
cicle.FillStyle = 1
rectangle.FillStyle = 0
Square.FillStyle = 1
rectangle.FillColor = &H0&
Square.FillColor = &H0&
End Sub
Private Sub abshapesquare_Click()
abshapecircle.Checked = False
abshapesquare.Checked = True
abshaperectangle.Checked = False
cicle.FillStyle = 1
rectangle.FillStyle = 1
Square.FillStyle = 0
rectangle.FillColor = &H0&
Square.FillColor = &H0&
End Sub
Output:
8. Write a program to show the use of MDI (Multiple document interface) form.
Design Window

Private Sub mcascade_Click()


MDIForm1.Arrange vbCascade
End Sub
Private Sub MDIForm_Load()
Form1.Show Form2.Show
Form3.Show
MDIForm1.WindowState = 2
End Sub
Private Sub mhorizontal_Click()
MDIForm1.Arrange vbHorizontal
End Sub
Private Sub mvertical_Click()
MDIForm1.Arrange vbVertical
End Sub

Output:
9. Write a program to access the child form in MDI form.
Design Window

Option Explicit
Dim documentforms(10) As New Form1
Private Sub arrange_Click()
arrange 2
End Sub
Private Sub cascade_Click()
arrange 0
End Sub
Private Sub FormOpen_Click()
Dim i As Integer
For i = 0 To 9
documentforms(i).Show
documentforms(i).Caption = "document" & Format(i)
documentforms(i).BackColor = QBColor(Rnd * 14 + 1)
Next i
End Sub
Private Sub FormsClose_Click()
Dim i As Integer
For i = 0 To 9
Unload documentforms(i)
End Sub
Private Sub horizontal_Click()
arrange 1
End Sub
OUTPUT:

WHEN WE CLICK ON CHILD FORMS AND THEN OPEN FORMS


10. Write a program to access data through data control form design.
Design Window

Option Explicit
Dim responce As Integer
Private Sub Command1_Click()
Data1.Recordset.AddNew
Text1.SetFocus
End Sub
Private Sub Command2_Click()
responce = MsgBox("do you want to delete the record", vbYesNo)
If responce = vbYes Then
Data1.Recordset.Delete
Data1.Recordset.MoveFirst
End If
End Sub
Private Sub Command3_Click()
Data1.Recordset.Edit
Data1.Recordset.Update
End Sub

Private Sub Command4_Click()


If Data1.Recordset.BOF = True And Data1.Recordset.EOF = True Then
MsgBox "the file is empty"
Else
Data1.Recordset.MoveFirst
End If
End Sub
Private Sub Command5_Click()
If Data1.Recordset.BOF = True And Data1.Recordset.EOF = True Then
MsgBox "the file is empty"
Else
Data1.Recordset.MovePrevious
If Data1.Recordset.BOF = True Then
Data1.Recordset.MoveFirst
MsgBox "you are at 1st record"
End If
End If
End Sub
Private Sub Command6_Click()
If Data1.Recordset.BOF = True And Data1.Recordset.EOF = True Then
MsgBox "the file is empty"
Else
Data1.Recordset.MoveNext
If Data1.Recordset.EOF = True Then
Data1.Recordset.MoveLast
MsgBox " you are at last record"
End If
End If
End Sub
Private Sub Command7_Click()
If Data1.Recordset.BOF = True And Data1.Recordset.EOF = True Then
MsgBox "the file is empty"
Else
Data1.Recordset.MoveLast
End If
End Sub
Private Sub Command8_Click()
End
End Sub
Output:

You might also like