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 TextBox txtbox chkbold CheckBoxes chkitalic Caption BOLD 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
1
End If End Sub
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
Type the following code in the code window.
Dim firstnum As Integer Dim secondum As Integer
3
Private Sub calc_Click() If firsttxt.Text = "" Then firsttxt.Text = "0" End If
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
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) End Sub
Private Sub cmdclear_Click() p = False t1.Text = ""
7
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
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
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
When its finished, itll look like this:
12
Then type the following code in the code window.
Private Sub mnuexit_Click() End End Sub
Private Sub mnufrm1_Click() Form1.Show
13
End Sub
Private Sub mnufrm2_Click() Form2.Show End Sub
Private Sub mnufrm3_Click() Form3.Show End Sub
OUTPUT
14
15
1. Check even or odd
A simple application to check an entered number whether it is even or odd.
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 -
Command Button 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"
18
End If i = i + 1 Wend If (f = 0) Then lblresult.Caption = "PRIME" End If End Sub
OUTPUT
19
1. Even numbers up to 50
This application will display even numbers up to 50 in a list box.
Method: Design a form with following controls and properties.
Controls Properties Name List Box Command Button Insert this code: List1 cmddisp Caption DISPLAY
Dim i As Integer
Private Sub cmddisp_Click() i = 2 While i <= 50
20
List1.AddItem i i = i + 2 Wend End Sub
OUTPUT
21
1. 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 Properties 22
Name atxt Text Boxes btxt ctxt r1txt r2txt lbla Labels lblb lblc lblsol Command Button cmdsolve
Caption A B C Solution SOLVE!
Text
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() a = atxt.Text b = btxt.Text c = ctxt.Text d = b * b - 4 * a * c If d < 0 Then
23
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
1. 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 File
New Open Save Save As Edit Select All
Name file new Open save saveas edit select
File New Open Save Save As Edit Select All 26
Cut Copy Paste Delete Exit
Cut Copy Paste Delete Exit
cut copy paste delete 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
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 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
29
RichTextBox1.SaveFile (dialog1.FileName) fname = dialog1.FileName Me.Caption = fname savestatus = True End If End Sub
Private Sub select_Click() RichTextBox1.SelStart = 0 RichTextBox1.SelLength = Len(RichTextBox1.Text) 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
Caption SORT ADD DELETE CLEAR
Text -
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 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
Private Sub cmdclear_Click() List1.Clear End Sub
Private Sub cmddelete_Click() List1.RemoveItem List1.ListIndex End Sub
Private Sub cmdsort_Click() sort 0, List1.ListCount - 1 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.
Now, design a form with following properties.
Controls
Properties Name
lblstud_id
Caption
Student ID Student Name Student Details ADD PREVIOUS FIND SAVE DELETE NEXT EDIT EXIT
Text
-
Labels
lbls_name lbls_details text1
Text Boxes
text2 text3 add prev
Command Buttons
find save delete nxt edit exit
Then type the following code in the code window.
Dim cmd As New ADODB.Command
36
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
rec1.MovePrevious End If End Sub
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.
Private Sub Command1_Click() DataReport1.Show End Sub
OUTPUT
44
jerrintv.zxq.net
45