Visual Basic Practical Record
Visual Basic Practical Record
Method:
Create a new Standard EXE project. Design a form with the controls given in the table. Controls Properties Name TextBox txtbox chkbold CheckBoxes chkitalic Caption BOLD ITALIC
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
1
OUTPUT
1. Arithmetic operations
This 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 Properties Name TextBoxes firsttxt secondtxt restxt Name Labels firstno secno result optadd Option Buttons optsub optmult optdiv optmod Command Button calc Caption First Number: Second Number: Result Add Subtract Multiply Divide Modulus CALCULATE! Text
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
OUTPUT
1. Calculator
A 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 6
-> 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 Properties Name cmddot cmdback cmdclear cmdplus cmdminus cmdmult cmddiv cmdequals mplus mminus mclear Name t1 t2 Caption . <-clear + * / = m+ mmc Text
Command Buttons
TextBoxes
Dim p As Boolean Dim mem As Double Dim op1, op2 As Double Dim res As Double Dim operator As String
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 res = op1 - op2 End If If operator = "/" Then res = op1 / op2
8
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
Private Sub Command1_Click(Index As Integer) t1.Text = t1.Text & Command1(Index).Caption End Sub
9
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
10
OUTPUT
11
1. MDI Form
An 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 Caption
forms Forms
mnuform1 Form1
mnuform2 Form2
mnuform3 Form3
mnuexit Exit
12
13
End Sub
OUTPUT
14
15
Method:
Design a form with following controls and properties. Controls Properties Name Text Box Labels inputtxt lblmsg lblresult cmdcheck Caption Entered Number is: CHECK IT! Text -
Command Button
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
16
OUTPUT
17
1. Prime or not
A simple application to check whether the entered number is prime or not.
Method:
Design the form with following controls. Controls Properties Name Text Box Labels inputtxt lblmsg lblresult cmdchck Caption Entered Number is: CHECK IT! Text -
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"
18
OUTPUT
19
1. Even numbers up to 50
This application will display even numbers up to 50 in a list box.
Dim i As Integer
OUTPUT
21
Method:
Design a form with following controls and properties. Controls Properties 22
Name atxt Text Boxes btxt ctxt r1txt r2txt lbla Labels lblb lblc lblsol Command Button cmdsolve
Text
Dim a As Integer Dim b As Integer Dim c As Integer Dim r1 As Double Dim r2 As Double Dim root As Integer
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
24
OUTPUT
25
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 File
New Open Save Save As Edit Select All
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
27
End If End End Sub 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|"
28
dialog1.ShowOpen If dialog1.FileName <> "" Then RichTextBox1.LoadFile (dialog1.FileName) fname = dialog1.FileName Me.Caption = fname End If 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
29
RichTextBox1.SaveFile (dialog1.FileName) fname = dialog1.FileName Me.Caption = fname savestatus = True End If End Sub
OUTPUT
30
1. Quick Sort
A simple application for quick sort.
Method:
Design a form with following controls and properties. Controls Properties 31
Name Text Box List Box Command Buttons Text1 List1 cmdsort cmdadd cmddelete cmdclear
Text -
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 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
32
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
OUTPUT
33
34
1. Database Connectivity
This 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 select database that you have created. Then click OK.
35
Start VB and create a Standard EXE project. Go to Project References. Select MicrosoftActiveX Data Objects 2.8 Library.
Controls
Properties Name
lblstud_id
Caption
Student ID Student Name Student Details ADD PREVIOUS FIND SAVE DELETE NEXT EDIT EXIT
Text
-
Labels
Text Boxes
Command Buttons
Dim cnn As New ADODB.Connection Dim rec1 As New ADODB.Recordset Dim rec2 As New ADODB.Recordset Dim s As String 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)
37
End If End Sub Private Sub edit_Click() x = Text1 y = Text2 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
38
rec1.CursorLocation = adUseClient cmd.CommandText = "select * from studtab" rec1.Open cmd, , adOpenStatic, adLockBatchOptimistic Text1 = rec1(0) Text2 = rec1(1) Text3 = rec1(2) End Sub 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")
39
Private Sub save_Click() edit.Enabled = True nxt.Enabled = True prev.Enabled = True 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
40
1. Data Report
41
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.
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.
42
Click Apply and OK. Go back to Data Report window. In the properties window, change the following properties. Data Source: DataEnvironment1 Data 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. Insert RptTextBoxes (insert as many as it according to the fields of your database table). Change their properties. Data Member: Command1 Change this property of all text boxes you place. But change their Data Field to corresponding fields.
43
Then go back to Form1, insert a command Button and give a caption Show Report. And insert the following code to it.
OUTPUT
44
jerrintv.zxq.net
45