visual basic practical record

46
1. Bold & Italic In this application there are 1 textbox and 2 checkboxes one denoted Bold and another denote Italic. When the user clicks Bold checkbox, the text must turn bold and by clicking Italic, the text must turn italic. Method: Create a new Standard EXE project. Design a form with the controls given in the table. Controls Properties Name Caption TextBox txtbox - CheckBoxes chkbold BOLD chkitalic ITALIC Type the following code in the code window. Private Sub chkbold_Click() If chkbold.Value = 1 Then txtbox.FontBold = True Else txtbox.FontBold = False End If End Sub Private Sub chkitalic_Click() If chkitalic.Value = 1 Then txtbox.FontItalic = True Else 1

Upload: kabeeraryan

Post on 16-Oct-2014

144 views

Category:

Documents


4 download

DESCRIPTION

Visual Basic Practical Record for Beginners.12 Simple programs illustrated in step by step manner.MG University STAS Pathanamthitta B.Sc. Computer Science Practical Record.-Jerrin

TRANSCRIPT

Page 1: Visual Basic Practical Record

1. Bold & ItalicIn this application there are 1 textbox and 2 checkboxes one denoted Bold and another denote Italic. When the user clicks Bold checkbox, the text must turn bold and by clicking Italic, the text must turn italic.

Method:

Create a new Standard EXE project. Design a form with the controls given in the table.

Controls PropertiesName Caption

TextBox txtbox -

CheckBoxeschkbold BOLD

chkitalic ITALIC

Type the following code in the code window.

Private Sub chkbold_Click()

If chkbold.Value = 1 Then

txtbox.FontBold = True

Else

txtbox.FontBold = False

End If

End Sub

Private Sub chkitalic_Click()

If chkitalic.Value = 1 Then

txtbox.FontItalic = True

Else

txtbox.FontItalic = False

End If

End Sub

1

Page 2: Visual Basic Practical Record

OUTPUT

2

Page 3: Visual Basic Practical Record

2. Arithmetic operationsThis application will do basic arithmetic operations and displays the output. The user can input two values in 2 text boxes and get the result in another textbox.

Method:

Create a new Standard EXE project. Design a form with the controls given in the table.

Controls PropertiesName Text

TextBoxes firsttxt

secondtxt

restxt

Labels

Name Caption

firstno First Number:

secno Second Number:

result Result

Option Buttons

optadd Add

optsub Subtract

optmult Multiply

optdiv Divide

optmod Modulus

Command Button calc CALCULATE!

Type the following code in the code window.

Dim firstnum As Integer

Dim secondum As Integer

Private Sub calc_Click()

If firsttxt.Text = "" Then

firsttxt.Text = "0"

End If

3

Page 4: Visual Basic Practical Record

If secondtxt.Text = "" Then

secondtxt.Text = "0"

End If

firstnum = firsttxt.Text

secondnum = secondtxt.Text

If optadd.Value = True Then

restxt.Text = firstnum + secondnum

ElseIf optsub.Value = True Then

restxt.Text = firstnum - secondnum

ElseIf optmult.Value = True Then

restxt.Text = firstnum * secondnum

ElseIf optdiv.Value = True Then

restxt.Text = firstnum / secondnum

ElseIf optmod.Value = True Then

restxt.Text = firstnum Mod secondnum

End If

End Sub

4

Page 5: Visual Basic Practical Record

OUTPUT

5

Page 6: Visual Basic Practical Record

3. CalculatorA simple math calculator with memory function. The form contains a control array of 10 command buttons, another 11 control buttons and 2 textboxes.

Method:

First we have to create a control array. For that first place a command button in the form and then copy it (Edit -> Copy) and then paste it(Edit -> Paste) 9 times to get 10 buttons with same properties but different indexes. Change the caption of these command buttons to 0,1,2,3,4,5,6,7,8,9 in the order we pasted it.

Then other controls with following properties.

Controls PropertiesName Caption

Command Buttons

cmddot .cmdback <--cmdclear clearcmdplus +cmdminus -cmdmult *cmddiv /cmdequals =mplus m+mminus m-mclear mc

TextBoxesName Textt1t2

Then write the following code in the code window.

Dim p As Boolean

Dim mem As Double

Dim op1, op2 As Double

Dim res As Double

Dim operator As String

Private Sub cmdback_Click()

t1.Text = Left(t1.Text, Len(t1.Text) - 1)

6

Page 7: Visual Basic Practical Record

End Sub

Private Sub cmdclear_Click()

p = False

t1.Text = ""

End Sub

Private Sub cmddiv_Click()

op1 = t1.Text

t1.Text = ""

operator = "/"

End Sub

Private Sub cmddot_Click()

If p = False Then

t1.Text = t1.Text + "."

p = True

End If

End Sub

Private Sub cmdequals_Click()

op2 = t1.Text

If operator = "+" Then

res = op1 + op2

End If

If operator = "*" Then

res = op1 * op2

End If

If operator = "-" Then

7

Page 8: Visual Basic Practical Record

res = op1 - op2

End If

If operator = "/" Then

res = op1 / op2

End If

t1.Text = res

End Sub

Private Sub cmdminus_Click()

p = False

op1 = t1.Text

t1.Text = ""

operator = "-"

End Sub

Private Sub cmdmult_Click()

p = False

op1 = t1.Text

t1.Text = ""

operator = "*"

End Sub

Private Sub cmdplus_Click()

p = False

op1 = t1.Text

t1.Text = ""

operator = "+"

End Sub

8

Page 9: Visual Basic Practical Record

Private Sub Command1_Click(Index As Integer)

t1.Text = t1.Text & Command1(Index).Caption

End Sub

Private Sub mclear_Click()

mem = 0

t1.Text = ""

t2.Text = ""

End Sub

Private Sub mminus_Click()

If mem <> 0 Then

t2.Text = "M"

End If

mem = mem - Val(t1.Text)

End Sub

Private Sub mplus_Click()

If mem > 0 Then

t2.Text = "M"

End If

t1.Text = mem

End Sub

9

Page 10: Visual Basic Practical Record

OUTPUT

10

Page 11: Visual Basic Practical Record

4. MDI FormAn application having a Multiple Document Interface (MDI) form and 3 child forms. The user can select the forms from the menu bar of the MDI form.

Method:

Add an MDI form (Project Add MDI Form). Add as many child forms (Project Add Form) as required (3 in this case). Go to Menu Editor (Tools Menu Editor). Type the name and caption for the menus to be

created and press Insert. Repeat this for each menu. To make a submenu, press .Insert 5 menu entries given in the below table.

Name forms mnuform1 mnuform2 mnuform3 mnuexitCaption Forms Form1 Form2 Form3 Exit

When it’s finished, it’ll look like this:

11

Page 12: Visual Basic Practical Record

Then type the following code in the code window.

Private Sub mnuexit_Click()

End

End Sub

Private Sub mnufrm1_Click()

Form1.Show

End Sub

12

Page 13: Visual Basic Practical Record

Private Sub mnufrm2_Click()

Form2.Show

End Sub

Private Sub mnufrm3_Click()

Form3.Show

End Sub

OUTPUT

13

Page 14: Visual Basic Practical Record

5. Check even or odd

14

Page 15: Visual Basic Practical Record

A simple application to check an entered number whether it is even or odd.

Method:

Design a form with following controls and properties.

Controls PropertiesName Caption Text

Text Box inputtxt -Labels lblmsg Entered Number is: -

lblresult -Command Button cmdcheck CHECK IT! -

Code:

Dim n As Integer

Private Sub cmdcheck_Click()

n = inputtxt.Text

If (n Mod 2) = 0 Then

lblresult.Caption = "EVEN"

Else

lblresult.Caption = "ODD"

End If

End Sub

OUTPUT

15

Page 16: Visual Basic Practical Record

16

Page 17: Visual Basic Practical Record

6. Prime or notA simple application to check whether the entered number is prime or not.

Method:

Design the form with following controls.

Controls PropertiesName Caption Text

Text Box inputtxt -Labels lblmsg Entered Number is: -

lblresult -Command Button cmdchck CHECK IT! -

Code:

Dim n As Integer

Dim f As Integer

Dim i As Integer

Private Sub cmdchk_Click()

n = inputtxt.Text

i = 2

f = 0

While i < n

If (n Mod i) = 0 Then

f = 1

lblresult.Caption = "NOT PRIME"

End If

i = i + 1

Wend

If (f = 0) Then

lblresult.Caption = "PRIME"

End If

End Sub

17

Page 18: Visual Basic Practical Record

OUTPUT

18

Page 19: Visual Basic Practical Record

7. Even numbers up to 50This application will display even numbers up to 50 in a list box.

Method:

Design a form with following controls and properties.

Controls PropertiesName Caption

List Box List1 -Command Button cmddisp DISPLAY

Insert this code:

Dim i As Integer

Private Sub cmddisp_Click()

i = 2

While i <= 50

List1.AddItem i

i = i + 2

Wend

End Sub

19

Page 20: Visual Basic Practical Record

OUTPUT

20

Page 21: Visual Basic Practical Record

8. Quadratic Equation Solver

This application accepts coefficients of quadratic equation from the user and solve it. Results are displayed in text boxes.

Method:

Design a form with following controls and properties.

Controls PropertiesName Caption Text

Text Boxes

atxt -btxt -ctxt -r1txt -r2txt -

Labelslbla A -lblb B -lblc C -lblsol Solution -

Command Button cmdsolve SOLVE! -

Write the following code in the code window.

Dim a As Integer

Dim b As Integer

Dim c As Integer

Dim r1 As Double

Dim r2 As Double

Dim root As Integer

Private Sub cmdsolve_Click()

21

Page 22: Visual Basic Practical Record

a = atxt.Text

b = btxt.Text

c = ctxt.Text

d = b * b - 4 * a * c

If d < 0 Then

r1txt.Text = "Roots are imaginary"

r2txt.Text = "Roots are imaginary"

ElseIf d = 0 Then

root = -b / 2 * a

r1txt.Text = "Roots are real and Same"

r2txt.Text = root

Else

r1 = (-b + Sqr(b * b - 4 * a * c)) / (2 * a)

r2 = (-b - Sqr(b * b - 4 * a * c)) / (2 * a)

r1txt.Text = r1

r2txt.Text = r2

End If

End Sub

22

Page 23: Visual Basic Practical Record

OUTPUT

23

Page 24: Visual Basic Practical Record

9. NOTEPAD (Without OLE control)

A Single Document Interface(SDI) NOTEPAD application in which a user can type, save and open text files.

Method:

Insert Microsoft Rich Textbox Control 6.0 and Microsoft Common Dialogue Control 6.0 from project components(ProjectComponents).

And place them in the form.

Design a menu using following information.

Menu Caption NameFile File file····New New new····Open Open Open····Save Save save····Save As Save As saveasEdit Edit edit····Select All Select All select····Cut Cut cut····Copy Copy copy

24

Page 25: Visual Basic Practical Record

····Paste Paste paste····Delete Delete deleteExit Exit exit

Write the following code.

Dim savestatus As Boolean

Dim fname As String

Private Sub copy_Click()

Clipboard.Clear

Clipboard.SetText RichTextBox1.SelText

End Sub

Private Sub cut_Click()

Clipboard.Clear

Clipboard.SetText RichTextBox1.SelText

RichTextBox1.SelText = ""

End Sub

Private Sub delete_Click()

RichTextBox1.SelText = ""

End Sub

Private Sub exit_Click()

Open_Click

If savestatus = False Then

If MsgBox("Do you want to save current file?", vbYesNo, "Save") = vbYes Then

save_Click

End If

End If

End

End Sub

25

Page 26: Visual Basic Practical Record

Private Sub Form_Load()

savestatus = True

End Sub

Private Sub new_Click()

If savestatus = False Then

If MsgBox("Do you want to save current file?", vbYesNo, "Save") = vbYes Then

save_Click

End If

End If

RichTextBox1.Text = ""

fname = ""

Me.Caption = fname

End Sub

Private Sub Open_Click()

If savestatus = False Then

If MsgBox("Do you want to save current file?", vbYesNo, "Save") = vbYes Then

save_Click

End If

End If

dialog1.Filter = "Textfiles(*.txt)|*.txt|"

dialog1.ShowOpen

If dialog1.FileName <> "" Then

RichTextBox1.LoadFile (dialog1.FileName)

fname = dialog1.FileName

26

Page 27: Visual Basic Practical Record

Me.Caption = fname

End If

End Sub

Private Sub paste_Click()

RichTextBox1.SelText = Clipboard.GetText

End Sub

Private Sub save_Click()

If fname = "" Then

saveas_Click

Else

RichTextBox1.SaveFile (fname)

savestatus = True

End If

End Sub

Private Sub saveas_Click()

dialog1.DefaultExt = "txt"

dialog1.DialogTitle = "Save As"

dialog1.ShowSave

If dialog1.FileName <> "" Then

RichTextBox1.SaveFile (dialog1.FileName)

fname = dialog1.FileName

Me.Caption = fname

savestatus = True

End If

27

Page 28: Visual Basic Practical Record

End Sub

Private Sub select_Click()

RichTextBox1.SelStart = 0

RichTextBox1.SelLength = Len(RichTextBox1.Text)

End Sub

OUTPUT

28

Page 29: Visual Basic Practical Record

10. Quick SortA simple application for quick sort.

Method:

Design a form with following controls and properties.

Controls PropertiesName Caption Text

Text Box Text1 -List Box List1 - -

Command Buttonscmdsort SORT -cmdadd ADD -cmddelete DELETE -cmdclear CLEAR -

Insert the following code in the code window.

Function sort(ByVal l As Long, ByVal r As Long)

Dim i As Long, j As Long

Dim x As Long, w As Long

i = l

j = r

x = CLng(List1.List((l + r) / 2))

Do

While CLng(List1.List(i)) < x

i = i + 1

29

Page 30: Visual Basic Practical Record

Wend

While x < CLng(List1.List(j))

j = j - 1

Wend

If i <= j Then

w = CLng(List1.List(i))

List1.List(i) = List1.List(j)

List1.List(j) = CStr(w)

i = i + 1

j = j - 1

End If

Loop Until i > j

If l < j Then sort l, j

If i < r Then sort i, r

End Function

Private Sub cmdadd_Click()

List1.AddItem Text1.Text

Text1.Text = " "

Text1.SetFocus

End Sub

Private Sub cmdclear_Click()

List1.Clear

End Sub

Private Sub cmddelete_Click()

30

Page 31: Visual Basic Practical Record

List1.RemoveItem List1.ListIndex

End Sub

Private Sub cmdsort_Click()

sort 0, List1.ListCount - 1

End Sub

OUTPUT

31

Page 32: Visual Basic Practical Record

11. Database ConnectivityThis application connects a Microsoft Access Database file (*.mdb) to the visual basic project. This program display the details of a Student database (Student.mdb) and allows the user to add, delete, edit and save records to the database.

Method:

Create a database file in MS Access and save it in the VB installation directory (C:\Program Files\Microsoft Visual Studio\VB98\). File should be in (*.mdb) format.

Go to Control Panel Administrative Tools Data Sources (ODBC). Click on System DSN tab.

Then click Add. In the dialogue box then appears, select Driver do Microsoft Access (*.mdb) and click Finish. In the window that appears, enter StudentDB for Data Source Name. Then

32

Page 33: Visual Basic Practical Record

select database that you have created. Then click OK.

Start VB and create a Standard EXE project. Go to Project References. Select MicrosoftActiveX Data Objects 2.8 Library.

Now, design a form with following properties.

Controls PropertiesName Caption Text

Labelslblstud_id Student ID -lbls_name Student Name -lbls_details Student Details -

Text Boxestext1 -text2 -text3 -

Command Buttons

add ADD -prev PREVIOUS -find FIND -save SAVE -delete DELETE -nxt NEXT -edit EDIT -exit EXIT -

Then type the following code in the code window.

Dim cmd As New ADODB.Command

Dim cnn As New ADODB.Connection

Dim rec1 As New ADODB.Recordset

Dim rec2 As New ADODB.Recordset

Dim s As String

33

Page 34: Visual Basic Practical Record

Dim b As Integer

Dim x, y As Variant

Private Sub add_Click()

Text1 = ""

Text2 = ""

Text3 = ""

If add.Enabled = True Then

Text1.SetFocus

save.Enabled = True

prev.Enabled = False

nxt.Enabled = False

find.Enabled = False

End If

b = 0

End Sub

Private Sub delete_Click()

If MsgBox("Do you want to delete?", vbCritical + vbYesNo) = vbYes Then

cmd.CommandText = s

s = "delete from studtab where s_name='" & Text2 & "'and stud_id=" & Text1 & ""

cnn.Execute (s)

End If

End Sub

Private Sub edit_Click()

x = Text1

y = Text2

34

Page 35: Visual Basic Practical Record

b = 1

End Sub

Private Sub exit_Click()

End

End Sub

Private Sub find_Click()

cmd.CommandText = "select * from studtab where name='" & InputBox("Enter Name") & "'"

cmd.Execute

rec2.Open cmd, , adOpenDynamic, adLockBatchOptimistic

If rec2.EOF = True Then

MsgBox "No Records"

Else

Text1 = rec2(0)

Text2 = rec2(1)

Text3 = rec2(2)

End If

End Sub

Private Sub Form_Load()

cnn.Open "StudentDB", "", ""

cmd.ActiveConnection = cnn

rec1.CursorLocation = adUseClient

cmd.CommandText = "select * from studtab"

rec1.Open cmd, , adOpenStatic, adLockBatchOptimistic

Text1 = rec1(0)

Text2 = rec1(1)

Text3 = rec1(2)

End Sub

35

Page 36: Visual Basic Practical Record

Private Sub nxt_Click()

If rec1.EOF = True Then

MsgBox "No Records!"

rec1.MoveLast

Else

Text1 = rec1("stud_id")

Text2 = rec1("s_name")

Text3 = rec1("s_details")

rec1.MoveNext

End If

End Sub

Private Sub prev_Click()

If rec1.BOF = True Then

MsgBox "No Records!"

rec1.MoveFirst

Else

Text1 = rec1("stud_id")

Text2 = rec1("s_name")

Text3 = rec1("s_details")

rec1.MovePrevious

End If

End Sub

Private Sub save_Click()

edit.Enabled = True

nxt.Enabled = True

prev.Enabled = True

36

Page 37: Visual Basic Practical Record

find.Enabled = True

If b = 0 Then

s = "insert into studtab values(" & Text1 & ",'" & Text2 & "'," & Text3 & ")"

cmd.CommandText = s

cmd.Execute

Else

s = "update studtab set stud_id=" & Text1 & ",s_name='" & Text2 & "',s_details=" & Text3 & " where s_name='" & y & "'"

MsgBox s

cmd.CommandText = s

cmd.Execute

End If

End Sub

OUTPUT

37

Page 38: Visual Basic Practical Record

12. Data Report Create a Standard EXE project. Add a Data Report and Data Environment from Project menu. In the Data Environment window, right click connection1 and select properties.

38

Page 39: Visual Basic Practical Record

In the window that appears, select Microsoft Jet 4.0 OLE DB Provider and click Next. Then select database and click Test Connection. You will get a message “Test connection

successful”. Click OK and return to Data Environment. Right click connection1 and select Add Command. Right click on command1 and select

Properties. The following window will appear. Fill the window as in picture.

Click Apply and OK.

Go back to Data Report window. In the properties window, change the following properties.Data Source: DataEnvironment1Data Member: Command1

Insert two RptLabels from Data report toolbox and place it in Report Header and Page Header. Change their caption property to title and date respectively.

39

Page 40: Visual Basic Practical Record

Insert RptTextBoxes (insert as many as it according to the fields of your database table). Change their properties.Data Member: Command1Change this property of all text boxes you place.But change their Data Field to corresponding fields.

Then go back to Form1, insert a command Button and give a caption “Show Report”. And insert the following code to it.

Private Sub Command1_Click()DataReport1.ShowEnd Sub

OUTPUT

40

Page 41: Visual Basic Practical Record

41©jerrintv.zxq.net