Annamalai University Department Department of Computer Science Science and Engineering Engineering
M.C. M.C.A A - Th Thir ird d Semes emeste terr MCA361 MCA3610 0 – Program Programmin ming g Lab Lab VV- Visual Visual Program Programmin ming g Lab Manual 2014
List of Exercises Cycle Cycle – I (Visua (Visuall Basic) Basic)
1. Inte Intere rest st Calc Calcul ulat atio ion n 2. Fibonacci cci Series 3. Designing Designing a Scientifi Scientificc Calcula Calculator tor using Control Control Array Array 4. Stri String ng Oper Operat atio ions ns 5. Matr Matrix ix Oper Operat atio ions ns 6. Free Hand Writing 7. Simple MDI Text Editor 8. Creating and Updating a Database 9. Designing a Digital Clock 10. Horizontal and Vertical Scrolling for Changing Colors. 11. Designing a Calendar 12. Student Mark Sheet 13. Database Applications using data control. Cycl Cyclee – II (Vis (Visua uall C++) C++)
14. Palindrome Palindrome Checking Checking 15. Pattern Pattern Numbers using using For Loops 16. Minipa Minipaint int applic applicati ation on 17. File Operations Operations 18. Mouse Interface Interface 19. ActiveX ActiveX Control Control 20. Message Box Display Display 21. Creating Creating and Using Using DLLs DLLs
Lab Incharges
A1 Batch Batch – Dr. A. Geetha Geetha A2 Batc Batch h – Dr. P. P. Dhanala Dhanalaksh kshmi mi
List of Exercises Cycle Cycle – I (Visua (Visuall Basic) Basic)
1. Inte Intere rest st Calc Calcul ulat atio ion n 2. Fibonacci cci Series 3. Designing Designing a Scientifi Scientificc Calcula Calculator tor using Control Control Array Array 4. Stri String ng Oper Operat atio ions ns 5. Matr Matrix ix Oper Operat atio ions ns 6. Free Hand Writing 7. Simple MDI Text Editor 8. Creating and Updating a Database 9. Designing a Digital Clock 10. Horizontal and Vertical Scrolling for Changing Colors. 11. Designing a Calendar 12. Student Mark Sheet 13. Database Applications using data control. Cycl Cyclee – II (Vis (Visua uall C++) C++)
14. Palindrome Palindrome Checking Checking 15. Pattern Pattern Numbers using using For Loops 16. Minipa Minipaint int applic applicati ation on 17. File Operations Operations 18. Mouse Interface Interface 19. ActiveX ActiveX Control Control 20. Message Box Display Display 21. Creating Creating and Using Using DLLs DLLs
Lab Incharges
A1 Batch Batch – Dr. A. Geetha Geetha A2 Batc Batch h – Dr. P. P. Dhanala Dhanalaksh kshmi mi
Cycle – I Visual Basic
INTEREST CALCULATION Ex. No: 1 Aim:
To implement implement a Visual Visual Basic program to calculate calculate the simple simple interest interest and compound compound interest. Proce Procedur duree :
1. Start 2. Desi Design gn the the form form with with four four text text boxes boxes and and four four comma command nd butt buttons ons.. Displ Display ay appr appropr opria iate te messag messages es in label label boxes boxes 3. Write Write the scri script pt in the the click click event event on the the simple simple inter interest est and and compound compound inter interest est comman command d buttons as follows: i) Set Set p=Te p=Text1 xt1.. Text Text ii) Set n=Te xt2. Text iii) Set r=Text3.Text r=Text3.Text iv) Si=(P*n*r)/10 Si=(P*n*r)/100 0 v) Ci=p*(1+r/10 Ci=p*(1+r/100)^n 0)^n vi) Text4. Text=Si Text=Si vii) Text5.Text= Text5.Text=Ci Ci 4. Stop Property Settings: Control Label Label Label Label Text Box Text Box Text Box Text Box Comm ommand Comm ommand Command Command
Name Label1 Label2 Label3 Label4 Text1 Text2 Text3 Text4 Command1 nd1 Command2 nd2 Command3 Command3
Property Caption Caption Caption Caption Text Text Text Text Caption Caption C ap aption Caption
Value Principle Rate of interest Number of Year Result
Simpl mple Interest Compou pound Interes rest Exit Clear
Form Design:
Source Code:
Dim p, r, n As Integer Private Sub Command1_Click() p = Val(Text1.Text) r = Val(Text2.Text) n = Val(Text3.Text) Text4.Text = (p * r * n) / 100 End Sub Private Sub Command2_Click() p = Val(Text1.Text) r = Val(Text2.Text) n = Val(Text3.Text) Text4.Text = p * (1 + r / 100) ^ n
End Sub Private Sub Command3_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub Private Sub Command4_Click() End End Sub
Output :
Result : Thus the Visual Basic program to calculate the simple interest and compound interest
has been executed successfully.
FIBONACCI SERIES Ex. No : 2 Aim:
To implement a Visual Basic program to create Fibonacci series. Procedure :
1. Start 2. Design a form with all necessary controls. 3. Write code in the click event of the compute command button to print the Fibonacci series 4. Write appropriate code in the click events of exit and clear command buttons. 5. Stop Property Settings: Control Name Label Label1 Label Label2 Text Box Text1 List Box list1 Command Command1 Command Command2 Command Command3 Form Design:
Property Caption Caption Text Caption C aption C aption Caption
Value Enter the number Result
Compute Exit Clear
Source Code:
Private Sub Command1_Click() Dim f1, f2, f3, i As Integer f1 = -1 f2 = 1 n = Val(Text1.Text) For i = 1 To n f3 = f1 + f2 List1.AddItem (f3) f1 = f2 f2 = f3 Next i End Sub Private Sub Command2_Click() End End Sub Private Sub Command3_Click() Text1.Text = "" List1.cl End Sub
Result : Thus the Visual Basic program to create Fibonacci series has been implemented and
executed successfully.
DESIGNING A SCIENTIFIC CALCULATOR USING CONTROL ARRAY Ex. No: 3 Aim:
To implement a Visual Basic program to create a scientific calculator using control arrays. Procedure :
1. Start 2. Place the command buttons and text boxes as shown in the form layout. 3. Write the script in the click event on the command button 4. For creating control array, create a command button named as command, copy the button and then paste it. Answer Yes to the question, “you wish to create a control array”. Then paste necessary buttons. 5. Use the predefined function sin(), cos(), tan(), sqr() and log() for the operations sine, cosine, tangent, square root and logarithm. 6. Use the user defined function fact() to calculate the factorial of a number 7. Use the user defined function calculate() to perform all the arithmetic operations depending upon the priority in the variable ‘pre’ (pre is the operation to be performed after entering the second operand). 8. For each click on the command button with caption 0-9, display the content in the textbox2 with the corresponding caption value. 9. For each click on operator button, append the contents of textbox2 to the textbox1 with corresponding operator. 10. After entering the second operand store/display the temporary result in textbox2. 11. Stop.
Property Settings: Control Text Box Text Box Command Command
Name Text1 Text2 Command 1 Command 2
Property Text Text Caption, Index Caption, Index
Value
1, 1 2, 2
Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command Command
Form Design:
Command 3 Command4 Command 5 Command6 Command 7 Command 8 Command 9 Command 10 Command 11 Command 12 Command 13 Command 14 Command 15 Command 16 Command 17 Command 18 Command 19 Command 20 Command 21 Command 22 Command 23 Command 24 Command 25 Command 26 Command 27 Command 28 Command 29 Command 30
Caption, Index Caption, Index Caption, Index Caption, Index Caption, Index Caption, Index Caption, Index Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption
3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 . 0 = + * / x^2 x^3 x^4 sqrt Mod Sin Cos Tan 10^x 1/x x! Pi Clear Exit
Source Code:
Dim a, b, d As Integer Dim c As String Private Sub Command1_Click(Index As Integer) Select Case Index Case 0: Unload Me Case 1: Text1.Text = Text1.Text & 1 Case 2: Text1.Text = Text1.Text & 2 Case 3: Text1.Text = Text1.Text & 3 Case 4: Text1.Text = Text1.Text & 4 Case 5: Text1.Text = Text1.Text & 5 Case 6: Text1.Text = Text1.Text & 6 Case 7: Text1.Text = Text1.Text & 7 Case 8: Text1.Text = Text1.Text & 8 Case 9: Text1.Text = Text1.Text & 9 Case 10: Text1.Text = Text1.Text & "." Case 11: Text1.Text = Text1.Text & 0
Case 12: b = Val(Text1.Text) Text1.Text = "" If c = "+" Then d=a+b Text1.Text = d ElseIf c = "-" Then d=a-b Text1.Text = d ElseIf c = "*" Then d=a*b Text1.Text = d ElseIf c = "/" Then d=a/b Text1.Text = d ElseIf c = "mod" Then d = a Mod b Text1.Text = d End If Case 13: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" c = "+" End If Case 14: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" c = "-" End If Case 15: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" c = "*" End If Case 16: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" c = "/" End If Case 17: If Trim(Text1.Text) <> "" Then
a = Val(Text1.Text) Text1.Text = "" Text1.Text = a * a End If Case 18: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = a * a * a End If Case 19: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = a * a * a * a End If Case 20: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = a ^ 0.5 End If Case 21: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" c = "mod" End If Case 22: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) * (3.14 / 180) Text1.Text = "" Text1.Text = Sin(a) End If Case 23: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) * (3.18 / 180) Text1.Text = "" Text1.Text = Cos(a) End If Case 24: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) * (3.18 / 180) Text1.Text = "" Text1.Text = Tan(a) End If
Case 25: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = 10 ^ a End If Case 26: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = 1 / a End If Case 27: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" s=1 For i = 1 To n s=s*i Next i Text1.Text = s End If Case 28: If Trim(Text1.Text) <> "" Then a = Val(Text1.Text) Text1.Text = "" Text1.Text = a * 3.14 End If Case Else: MsgBox ("sorry....") End Select End Sub Private Sub Command29_Click() Text1.Text = "" End Sub Private Sub Command30_Click() End End Sub
Output :
Result : Thus the Visual Basic program to create a scientific calculator has been implemented
and executed successfully.
STRING OPERATIONS Ex. No: 4 Aim:
To implement a Visual Basic program to perform string operations based on the user choice. Procedure :
1. Start 2. Read the string 1&string 2 3. Get the choice from the user 4. If Choice=Compare, Then Invoke StrComp ( ) and Print whether the strings are equal or unequal. 5. Else If Choice=Concat , Then Use + operator to concatenate two strings and print the concatenated string. 6. Else If Choice =Replace , Then Invoke Replace( ) and Print the resultant string. 7. Else If Choice=Length , Then Invoke LEN( ) and Print the length of the given string. 8. Else If Choice=Trim, Then Invoke Trin( ) to remove spaces in the string and Print the resultant string. 9. Else If Choice=Reverse, Then Invoke StrReverse( ) to reverse the string and Print the reversed string. 10. Else If Choice=Space, Then
Invoke Space( ) to add spaces in the string and Print the resultant string. 11. Else If Choice=Conversion, Then Invoke StrConv( ) to convert the case of a given string and Print the converted string. 12. Stop. Propery Settings: Control Label Label Label Text Box Text Box Text Box Command Command Command Command Command Command Command Command Command Command
Form Design:
Name Label1 Label2 Label3 Text1 Text2 Text3 Command1 Command2 Command3 Command4 Command5 Command6 Command7 Command8 Command9 Command10
Property Caption Caption Caption Text Text Text Caption Caption Caption Caption Caption Caption Caption Caption Caption Caption
Value String1 String2 Result
Replace Length Space Conversion Concatenate Compare Reverse Trim Clear Exit
Source Code:
Private Sub Command1_Click() Dim a, b As String a = Text1.Text b = Text2.Text Text3.Text = Replace(a, "a", b, 1, 1) End Sub Private Sub Command10_Click() End End Sub Private Sub Command2_Click() Text3.Text = Len(Text1) End Sub Private Sub Command3_Click() Dim a As String a = Text1.Text Text3.Text = Space(5) + a End Sub Private Sub Command4_Click() Dim a As String a = Text1.Text Text3.Text = StrConv(a, 1) End Sub Private Sub Command5_Click() Dim a As String a = Text1.Text b = Text2.Text Text3.Text = a + b End Sub
Private Sub Command6_Click() Dim a As String a = Text1.Text b = Text2.Text Text3.Text = StrComp(a, b) End Sub Private Sub Command7_Click() Dim a As String a = Text1.Text Text3.Text = StrReverse(a) End Sub Private Sub Command8_Click() Dim a As String a = Text1.Text Text3.Text = Trim(a) End Sub Private Sub Command9_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub Output :
Result : Thus the Visual Basic program to perform string operations has been implemented and
executed successfully.
MATRIX OPERATIONS Ex. No: 5 Aim:
To implement a Visual Basic program to perform matrix operations. Procedure :
1. Start 2. Design the form with 3 label boxes, 3 list boxes and 6 command buttons. 3. Display appropriate messages in the label boxes. 4. Write the script in the click events of the matrix addition, subtraction, multiplication and Inverse, clear and exit command buttons. 6. Stop. Property Settings: Control
Name
Property
Value
Label Label Label Text Box Text Box Text Box Command Command Command Command Command Command
Label1 Label2 Label3 Text1 Text2 Text3 Command1 Command2 Command3 Command4 Command5 Command6
Caption Caption Caption Text Text Text Caption Caption Caption Caption Caption Caption
Matrix1 Matrix2 Result
Addition Subtract Multiply Inverse Clear Exit
Form Design:
Source Code:
Dim a(10, 10), b(10, 10), c(10, 10) As Integer Dim m, n, p, q, i, j, k As Integer Dim str As String Private Sub Command1_Click() List1.Clear List2.Clear List3.Clear l1: m = InputBox("Enter m value") n = InputBox("Enter n value") p = InputBox("Enter p value") q = InputBox("Enter q value") If m <> p And n <> q Then
MsgBox ("Addition ("Addition Operation Operation not possible") GoTo l1 End If ina inb For i = 1 To m str = "" For j = 1 To n c(i, j) = Val(a(i, j)) + Val(b(i, j)) str = str & c(i, j) & " " Next j List3.AddItem (str) Next i End Sub Private Sub ina() For i = 1 To m str = " " For For j = 1 To n a(i, j) = InputBox("Enter a matrix rowwise") str = str & a(i, j) & " " Next j List1.AddItem (str) Next i End Sub Private Sub inb() For i = 1 To p str = " " For j = 1 To q b(i, j) = InputBox("Enter B matrix rowwise") str = str & b(i, j) & " " Next j List2.AddItem str Next i End Sub Private Sub Command2_Click() List1.Clear List2.Clear List3.Clear l1: m = InputBox("Enter m value") n = InputBox("Enter n value") p = InputBox("Enter p value") q = InputBox("Enter q value") If m <> p And n <> q Then MsgBox (" Sub Operation Operation not possible") possible")
GoTo l1 End If ina inb For i = 1 To m str = "" For j = 1 To n c(i, c(i, j) = a(i, a(i, j) j) - b(i, b(i, j) str = str & c(i, j) & " " Next j List3.AddItem str Next i End Sub Private Sub Command3_Click() List1.Clear List2.Clear List3.Clear l1: m = InputBox("Enter m value") n = InputBox("Enter n value") p = InputBox("Enter p value") q = InputBox("Enter q value") If m <> p And n <> q Then MsgBox ("Multi operation not possible") GoTo l1: End If ina inb For i = 1 To m str = " " For j = 1 To q c(i, j) = 0 For k = 1 To p c(i, j) = c(i, j) + a(i, k) * b(k, j) Next k str = str & c(i, j) & " " Next j List3.AddItem str Next i End Sub Private Sub Command4_Click() List1.Clear List2.Clear List3.Clear
Label2.Visible = False List2.Visible = False m = InputBox("E InputBox("Enter nter m value") value") n = InputBox("Enter n value") ina For i = 1 To m str = "" For j = 1 To n c(i, j) = Val(a(j, i)) str = str & c(i, j) & " " Next j List3.AddItem str Next i End Sub Private Sub Command5_Click() List1.Clear List2.Clear List3.Clear End Sub Private Sub Command6_Click() End End Sub Output :
and Result : Thus the Visual Basic program to perform matrix operations has been implemented and executed executed successfully. successfully.
FREEHAND DRAWING Ex. Ex. No: No: 6 Aim:
To implem implement ent a Visual Visual Basic Basic prog program ram to crea create te a free hand drawing. drawing. Proce Procedur duree :
1. Start 2. Desig Design n the for form m by addi adding ng a frame, frame, 4 comm comman and d butto buttons, ns, 1 pic pictu ture re box box and and 1 combo combo box 3. Write Write the script script in the the click click events events of size size,, color color,, clear clear and and exit exit comman command d butto buttons. ns. 4. On click click and drag drag on the picture picture box lines are drawn drawn by following following mouse pointer. pointer. 5. On click click and drag drag on the the frame both both (picture (picture box box and frame) frame) are moved moved to the the mouse mouse pointer direction. 6. On clickin clicking g clear the contents contents of picture picture box box should should be cleared cleared
Property Settings : Control
Name
Command Command Command Command Picture box
Command1 Command2 Command3 Command4 Picture1
Combo bo box
Combo1
Form Design :
Property
Value
Caption Caption Caption Caption
Size Color Clear Exit
Source Code :
Dim clr As OLE_COLOR Dim i As Integer Private Sub Command1_Click() Combo1.Visible = True End Sub Private Sub Command2_Click() cd1.ShowColor clr = cd1.Color End Sub Private Sub Command3_Click() Picture1.Cls End Sub Private Sub Command4_Click() Unload Me End Sub Private Sub Form_Load() clr = vbBlack For i = 1 To 15 Combo1.AddItem (i) Next i End Sub Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Combo1.Visible = False If Button = vbLeftButton Then Picture1.PSet (x, y), clr Picture1.DrawWidth = Combo1.Text ElseIf Button = vbRightButton Then Picture1.DrawWidth = 1 Picture1.PSet (x, y), Combo1.Text * 10 End If End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbLeftButton Then Picture1.PSet (x, y), clr Picture1.DrawWidth = Combo1.Text ElseIf Button = vbRightButton Then Picture1.DrawWidth = 1 Picture1.Circle (x, y), Combo1.Text * 10 End If End Sub Output :
Result : Thus the Visual Basic program to create a free hand drawing has been implemented
and executed successfully.
SIMPLE MDI TEXT EDITOR Ex. No. 7 Aim:
To create a Visual Basic application with MDI features and text editing capabilities. Procedure :
1. Place the label, rich text box, and combo box as shown in form layout. 2. Click tools -> menu editor to open the menu editor. 3. Give the required caption name in the menu editor window and use left and right arrow buttons to place the menu item as sub – menu and use up and down arrows to arrange it in a sequence. 4. Create three menus with lists as shown below Menu Caption File
Sub-Menu Caption open Save Save as Print Exit Edit Cut Copy Paste Format Bold Italic Underline Strikethrough 5. To invoke components, click project -> components
Name
open save saveas print exit cut copy paste bold italic underline Strikethrough then select Microsoft rich text
box control, Microsoft common dialog box control from components window. 6. Load the combo boxes with some font names and size. By selecting the combo boxes the font and size of the text are altered respectively. 7. By selecting the operations on menus the corresponding tasks should be performed using common dialog box and clipboard events.
Rich textbox Form design:-
MDI Form design:-
1. Microsoft common dialog control 6.0 2. Microsoft Rich textbox control 6.0 3. Microsoft windows common control 6.0
Create the Menu edit procedure
Add image box from the left panel
Create 6 image using box paint for new, open, save, cut, copy and paste respectively
Select the image box and double click the properties present in the right pane
Select images tab
Click insert pictures command button
Select the pictures from the desired location
Click Ok
Double click toolbar icon from left pane
Select toolbar on form(below the menu bar of your form)
Double click custom from properties(on right pane)
Select button tab
Click insert button(6 times)and also number the image text field with corresponding index number
Select general tab
Select imagelist box
Select the imagelist1 that you create before Toolbor & common dialog control
Place Imagelist on MDI form
Add a common dialog box and name it as comdg
MDI Form :
Source code:MDI Form:-
Private Sub Clear_Click() rt.Clear End Sub Private Sub Copy_Click(Index As Integer) Clipboard.SetText (MDIForm1.ActiveForm.ActiveControl.SelText) End Sub Private Sub Cut_Click() Clipboard.SetText (MDIForm1.ActiveForm.ActiveControl.SelText) MDIForm1.ActiveForm.ActiveControl.SelText = " " End Sub
Private Sub Exit_Click() End End Sub Private Sub MDIForm_Load() Static n As Integer End Sub Private Sub New_Click() Static n1 As Integer filenam = " " n1 = n1 + 1 Dim nf As New Form1 nf.Show MDIForm1.ActiveForm.Caption = "untitled" + Str(n1) End Sub Private Sub open_Click() cdm1.ShowOpen filenam = "" n1 = n1 + 1 Dim nf As New Form1 nf.Show nf.rt.FileName = cdm1.FileName filenam = cdm1.FileName End Sub Private Sub past_Click() MDIForm1.ActiveForm.ActiveControl.SelText = Clipboard.GetText() End Sub Private Sub Save_Click() If Len(filenam) <> 0 Then MDIForm1.ActiveForm.ActiveControl.SaveFile (filenam) Else Call Saveas_Click End If End Sub Private Sub Saveas_Click() cdm1.ShowSave MDIForm1.ActiveForm.ActiveControl.SaveFile (cdm1.FileName) filenam = cdm1.FileName End Sub Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
If (Button.Index = 1) Then Call New_Click ElseIf (Button.Index = 2) Then Call open_Click ElseIf (Button.Index = 3) Then Call Save_Click ElseIf (Button.Index = 4) Then Call Cut_Click ElseIf (Button.Index = 5) Then Call Copy_Click ElseIf (Button.Index = 6) Then Call past_Click End If End Sub Rich text box :Private Sub rt_mousedown(Button As Integer, shift As Integer, x As Single, y As Single) If Button = 2 Then PopupMenu Edit End If End Sub
Output :
Result : Thus a Visual Basic application has been created with MDI features and text editing
capabilities.
CREATING AND UPDATING A DATABASE Ex. No: 8 Aim:
To create a Visual Basic application for performing basic functions in a database. Procedure :
Create a table as follows
Add->Ins-> Visual data manager
File->New->Microsoft access->Version7.0 MDB
Save the file
Right click on properties and select New table
Enter table name
Click Add field button
Enter the name and select the desired type in type combo box
Click Ok
Similarly enter all the field names and their corresponding type
When finished click close
Click Build the table
Click Add button
Double click the table name
Click Add and enter the corresponding fielding details and update it
Similarly enter all the records
Click utility option data form designer
Click the>>button in the middle and then click Build the form
Click the project option and select project1 properties to change the startup object
Source code:-
Private Sub cmdAdd_Click() Data1.Recordset.AddNew End Sub Private Sub cmdDelete_Click() 'this may produce an error if you delete the last
'record or the only record in the recordset Data1.Recordset.Delete Data1.Recordset.MoveNext End Sub Private Sub cmdRefresh_Click() 'this is really only needed for multi user apps Data1.Refresh End Sub Private Sub cmdUpdate_Click() Data1.UpdateRecord Data1.Recordset.Bookmark = Data1.Recordset.LastModified End Sub Private Sub cmdClose_Click() Unload Me End Sub Private Sub Data1_Error(DataErr As Integer, Response As Integer) 'This is where you would put error handling code 'If you want to ignore errors, comment out the next line 'If you want to trap them, add code here to handle them MsgBox "Data error event hit err:" & Error$(DataErr) Response = 0 'throw away the error End Sub Private Sub Data1_Reposition() Screen.MousePointer = vbDefault On Error Resume Next 'This will display the current record position 'for dynasets and snapshots Data1.Caption = "Record: " & (Data1.Recordset.AbsolutePosition + 1) 'for the table object you must set the index property when 'the recordset gets created and use the following line 'Data1.Caption = "Record: " & (Data1.Recordset.RecordCount * (Data1.Recordset.PercentPosition * 0.01)) + 1 End Sub Private Sub Data1_Validate(Action As Integer, Save As Integer)
'This is where you put validation code 'This event gets called when the following actions occur Select Case Action Case vbDataActionMoveFirst Case vbDataActionMovePrevious Case vbDataActionMoveNext Case vbDataActionMoveLast Case vbDataActionAddNew Case vbDataActionUpdate Case vbDataActionDelete Case vbDataActionFind Case vbDataActionBookmark Case vbDataActionClose End Select Screen.MousePointer = vbHourglass End Sub
Output :
Result : Thus a Visual Basic application has been created to perform basic functions in a
database.
DESIGNING A DIGITAL CLOCK Ex. No: 9 Aim:
To implement a Visual Basic program to design a digital clock. Algorithm:
1. Start 2. Design the form with 1 frame, 2 label boxes, 1 command box and 1 timer control. 3. Display appropriate messages in the label boxes. 4. Write the script for timer control and click event of the exit command button 5. Stop Control
Name
Property
Value
Label box Label box Command Frame
label1 label2 Command1 Frame1
Text Text Caption Caption
Exit Digital Clock
Timer
Timer1
Interval
1000
Form Design:
Source code:
Private Sub Command1_Click() End End Sub Private Sub Timer1_Timer() Label1.Caption = Time Label2.Caption = Date End Sub Output :
Result : Thus a Visual Basic program to design a digital clock has been implemented and executed successfully.
HORIZONTAL AND VERTICAL SCROLLING FOR CHANGING COLORS Ex. No: 10 Aim:
To implement a Visual Basic program to use the vertical and horizontal scroll bars for changing colors. Procedure :
1. Start 2. Design the form with 2 vertical, 1 horizontal scroll bar, 1 shape and 10 label boxes. 3. Set the properties as shown in the form layout. 4. Write the script for vertical and horizontal scroll bars. 5. Display appropriate messages in the label boxes. 6. Stop
Form Design:-
Source code:
Dim r, g, b As Integer Private Sub Command1_Click() End End Sub Private Sub HScroll1_Change() r = VScroll1.Value g = VScroll2.Value b = HScroll1.Value Shape1.BackColor = RGB(r, g, b) Label10.Caption = HScroll1.Value End Sub
Private Sub VScroll1_Change() r = VScroll1.Value g = VScroll2.Value b = HScroll1.Value Shape1.BackColor = RGB(r, g, b) Label6.Caption = VScroll1.Value End Sub Private Sub VScroll2_Change() r = VScroll1.Value g = VScroll2.Value b = HScroll1.Value Shape1.BackColor = RGB(r, g, b)
Label8.Caption = VScroll2.Value End Sub Output :
Result : Thus a Visual Basic program has been implemented to use horizontal and vertical bars
for changing colors and executed successfully.
DESIGNING A CALENDAR Ex. No : 11 Aim:
To implement a Visual Basic program to design a calendar. Procedure :
1. Start 2. Design the form with 1 calendar control and two label boxes. 3. Display appropriate messages in the Label boxes. 4. Stop Form design:Select the control as follows:
Project -> components (ctrl+t) ->Select microsoft calendar control 6.0
Source Code:Private Sub Timer1_Timer() Label2.Caption = DateTime.Time End Sub Output :
Result : Thus a Visual Basic program to create a calendar has been implemented and executed .
STUDENT MARK SHEET Ex. No :12 Aim:
To implement a Visual Basic program to create a student mark sheet. Procedure :
1. Start 2. Design the form with necessary label boxes, text boxes and command buttons. 3. Set the properties for all the controls. 4. Write the script in the click event of the submit, marks, average, result, overall point, reset and exit command buttons. 5. Display appropriate messages in the label boxes. 6. Stop
Form Design:
Source Code:-
Dim a, b, c, p, q, r, s, u, d, h As Integer Dim avg As Variant Private Sub Command1_Click() a = Val(Text5.Text) + Val(Text8.Text) + Val(Text11.Text) + Val(Text14.Text) + Val(Text17.Text) + Val(Text20.Text) Text26.Text = a b = Val(Text6.Text) + Val(Text9.Text) + Val(Text12.Text) + Val(Text15.Text) + Val(Text18.Text) + Val(Text21.Text) + Val(Text24.Text) Text27.Text = b Text7.Text = Val(Text5.Text) + Val(Text6.Text) p = Text7.Text If (Text7.Text >= 90 And Text7.Text <= 100) Then Text29.Text = "s" ElseIf (Text7.Text >= 80 And Text7.Text <= 89) Then Text29.Text = "A" ElseIf (Text7.Text >= 70 And Text7.Text <= 79) Then Text29.Text = "B" ElseIf (Text7.Text >= 60 And Text7.Text <= 69) Then Text29.Text = "c"
ElseIf (Text7.Text >= 55 And Text7.Text <= 59) Then Text29.Text = "D" ElseIf (Text7.Text >= 50 And Text7.Text <= 54) Then Text29.Text = "E" ElseIf (Text7.Text <= 50) Then Text29.Text = "F" End If Text10.Text = Val(Text8.Text) + Val(Text9.Text) q = Text10.Text If (Text10.Text >= 90 And Text10.Text <= 100) Then Text30.Text = "S" ElseIf (Text10.Text >= 80 And Text10.Text <= 89) Then Text30.Text = "A" ElseIf (Text10.Text >= 70 And Text10.Text <= 79) Then Text30.Text = "B" ElseIf (Text10.Text >= 60 And Text10.Text <= 69) Then Text30.Text = "c" ElseIf (Text10.Text >= 55 And Text10.Text <= 59) Then Text30.Text = "D" ElseIf (Text10.Text >= 50 And Text10.Text <= 54) Then Text30.Text = "E" ElseIf (Text10.Text <= 50) Then Text30.Text = "F" End If Text13.Text = Val(Text11.Text) + Val(Text12.Text) r = Text13.Text If (Text13.Text >= 90 And Text13.Text <= 100) Then Text31.Text = "s" ElseIf (Text13.Text >= 80 And Text13.Text <= 89) Then Text31.Text = "A" ElseIf (Text13.Text >= 70 And Text13.Text <= 79) Then Text31.Text = "B" ElseIf (Text13.Text >= 60 And Text13.Text <= 69) Then Text31.Text = "c" ElseIf (Text13.Text >= 55 And Text13.Text <= 59) Then Text31.Text = "D" ElseIf (Text13.Text >= 50 And Text13.Text <= 54) Then Text31.Text = "E" ElseIf (Text13.Text <= 50) Then Text31.Text = "F"
End If Text16.Text = Val(Text14.Text) + Val(Text15.Text) s = Text16.Text If (Text16.Text >= 90 And Text16.Text <= 100) Then Text32.Text = "s" ElseIf (Text16.Text >= 80 And Text16.Text <= 89) Then Text32.Text = "A" ElseIf (Text16.Text >= 70 And Text16.Text <= 79) Then Text32.Text = "B" ElseIf (Text16.Text >= 60 And Text16.Text <= 69) Then Text32.Text = "c" ElseIf (Text16.Text >= 55 And Text16.Text <= 59) Then Text32.Text = "D" ElseIf (Text16.Text >= 50 And Text16.Text <= 54) Then Text32.Text = "E" ElseIf (Text16.Text <= 50) Then Text32.Text = "F" End If Text19.Text = Val(Text17.Text) + Val(Text18.Text) u = Text19.Text If (Text19.Text >= 90 And Text16.Text <= 100) Then Text33.Text = "s" ElseIf (Text19.Text >= 80 And Text19.Text <= 89) Then Text33.Text = "A" ElseIf (Text19.Text >= 70 And Text19.Text <= 79) Then Text33.Text = "B" ElseIf (Text19.Text >= 60 And Text19.Text <= 69) Then Text33.Text = "c" ElseIf (Text19.Text >= 55 And Text19.Text <= 59) Then Text33.Text = "D" ElseIf (Text19.Text >= 50 And Text19.Text <= 54) Then Text33.Text = "E" ElseIf (Text19.Text <= 50) Then Text33.Text = "F" End If Text22.Text = Val(Text20.Text) + Val(Text21.Text) d = Text22.Text If (Text22.Text >= 90 And Text22.Text <= 100) Then Text34.Text = "s" ElseIf (Text22.Text >= 80 And Text22.Text <= 89) Then
Text34.Text = "A" ElseIf (Text22.Text >= 70 And Text22.Text <= 79) Then Text34.Text = "B" ElseIf (Text22.Text >= 60 And Text22.Text <= 69) Then Text34.Text = "c" ElseIf (Text22.Text >= 55 And Text22.Text <= 59) Then Text34.Text = "D" ElseIf (Text22.Text >= 50 And Text22.Text <= 54) Then Text34.Text = "E" ElseIf (Text22.Text <= 50) Then Text34.Text = "F" End If Text25.Text = Val(Text23.Text) + Val(Text24.Text) h = Text25.Text If (Text25.Text >= 90 And Text25.Text <= 100) Then Text35.Text = "s" ElseIf (Text25.Text >= 80 And Text25.Text <= 89) Then Text35.Text = "A" ElseIf (Text25.Text >= 70 And Text25.Text <= 79) Then Text35.Text = "B" ElseIf (Text25.Text >= 60 And Text25.Text <= 69) Then Text35.Text = "c" ElseIf (Text25.Text >= 55 And Text25.Text <= 59) Then Text35.Text = "D" ElseIf (Text25.Text >= 50 And Text25.Text <= 54) Then Text35.Text = "E" ElseIf (Text25.Text <= 50) Then Text35.Text = "F" End If c = Val(Text7.Text) + Val(Text10.Text) + Val(Text13.Text) + Val(Text16.Text) + Val(Text19.Text) + Val(Text22.Text) + Val(Text25.Text) Text28.Text = c Label28.Caption = Val(Text28.Text) avg = c / 7 Label30.Caption = avg If p < 50 Or q < 50 Or r < 50 Or s < 50 Or u < 50 Or d < 50 Or h < 50 Then Label29.Caption = "Fail" Else Label29.Caption = "Pass" End If
If p < 50 Or q < 50 Or r < 50 Or s < 50 Or u < 50 Or d < 50 Or h < 50 Then Label39.Caption = "re appear" ElseIf (Label30.Caption >= 90 And Label30.Caption <= 100) Then Label39.Caption = "S" ElseIf (Label30.Caption >= 80 And Label30.Caption <= 89) Then Label39.Caption = "A" ElseIf (Label30.Caption >= 70 And Label30.Caption <= 79) Then Label39.Caption = "B" ElseIf (Label30.Caption >= 60 And Label30.Caption <= 69) Then Label39.Caption = "C" ElseIf (Label30.Caption >= 55 And Label30.Caption <= 59) Then Label39.Caption = "D" ElseIf (Label30.Caption >= 50 And Label30.Caption <= 54) Then Label39.Caption = "E" ElseIf (Label30.Caption <= 50) Then Label39.Caption = "F" End If End Sub
Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" Text7.Text = "" Text8.Text = "" Text9.Text = "" Text10.Text = "" Text11.Text = "" Text12.Text = "" Text13.Text = "" Text14.Text = "" Text15.Text = "" Text16.Text = "" Text17.Text = "" Text18.Text = ""
Text19.Text = "" Text20.Text = "" Text21.Text = "" Text22.Text = "" Text23.Text = "" Text24.Text = "" Text25.Text = "" Text26.Text = "" Text27.Text = "" Text28.Text = "" Text29.Text = "" Text30.Text = "" Text31.Text = "" Text32.Text = "" Text33.Text = "" Text34.Text = "" Text35.Text = "" Label28.Caption = "" Label29.Caption = "" Label30.Caption = "" Label39.Caption = "" End Sub Private Sub Command3_Click() End End Sub Private Sub Timer1_Timer() Label9.Caption = Time Label10.Caption = Date End Sub
Output :
Result : Thus a Visual Basic program to create a student database has been implemented and
executed successfully.
DATABASE APPLICATION USING DATA CONTROL Ex. No: 13 Aim: To implement a Visual Basic program to create a database using data control and implement it in an application. Procedure :
1. Start -> programs->Microsoft Visual Studio 6.0-> Microsoft Visual Basic 2. Select File->New->Standard.exe. Give project name and then choose empty project button->finish->ok. 3. Design the form with 4 label boxes, 4 text boxes and 9 command buttons. 4. Display appropriate messages in the label boxes. 5. Write script in the Click events of the insert, edit, delete, save, exit, first, next, previous and last command buttons and set the properties. 6. Stop. Source Code :
Dim db As Database Dim rs As Recordset Dim rs1 As Recordset Dim sql As String Private Sub Command3_Click() Set rs1 = db.OpenRecordset("select count(*) from student1 where Rollno=" & Val(Text1.Text)) If Text1.Text = "" Then MsgBox "enter a valid roll number" Exit Sub End If If rs1(0) = 0 Then MsgBox ("there are no records with this roll number")
Exit Sub End If db.Execute ("delete from student1 where rollno=" &Val(Text1.Text)) MsgBox ("record deleted") End Sub Private Sub Command4_Click() If Command1.Enabled = False Then sql = " insert into student1 values(" &CInt(Text1.Text) & ",'" & Text2.Text & "','" & Text3.Text & "'," &CDbl(Text4.Text) & ")" MsgBoxsql db.Execute (sql) MsgBox ("record inserted") Command1.Enabled = True End If If Command2.Enabled = False Then sql = "update student1 set name='" & Text2.Text & " ',address='" & Text3.Text & "',mobilenumber=" &CDbl(Text4.Text) & " where (RollNO= " &CInt(Txt_ROllNo.Text) & ")" MsgBoxsql db.Execute (sql) MsgBox ("record updated") Command2.Enabled = True End If End Sub Private Sub Command6_Click() rs.MoveFirst Show_Data End Sub Private Sub Command7_Click()
rs.MoveNext Show_Data End Sub Private Sub Command8_Click() rs.MovePrevious Show_Data End Sub Private Sub Command9_Click() rs.MoveLast Show_Data End Sub Private Sub Form_Load() Set db = OpenDatabase("D:\aaa.mdb", True) Set rs = db.OpenRecordset("student", dbOpenDynaset) rs.MoveFirst End Sub Private Sub Show_Data() If rs.EOF Then rs.MoveLast End If If rs.BOF Then rs.MoveFirst End If Text1.Text = rs(0) Text2.Text = rs(1) Text3.Text = rs(2) Text4.Text = rs(3)
End Sub Private Sub Clear_Controls() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub Output :
Result : Thus the Visual Basic program to create a database using data control has been
implemented and executed successfully.
Cycle – II Visual C++
PALINDROME CHECKING Ex. No: 14 Aim:
To implement a VC++ program to find whether a given string is a palindrome or not. Procedure : 1. Start 2. Create a MFC application by selecting, File -> New -> MFC .exe -> Dialog Based -> Finish. 3. Place the Static texts, edit boxes, command Buttons as shown in the form Layout. 4. On clicking the palindrome Button, display whether the given string is palindrome or not. 5. Stop.
Property Settings Control Button Button
Name button1 button2
Member Variables: Control Category IDC_EDIT1 VALUE
Form Design :
Property caption caption
Value Is Palindrome Exit
Type Cstring
Variable Name m_edit1
Source code:-
// prog13 message handlers void prog13::OnButton1() { CString s; UpdateData(true); s=m_edit1; s.MakeReverse(); if(s==m_edit1) SetDlgItemText(IDC_EDIT2,m_edit1+"is palindrome"); else SetDlgItemText(IDC_EDIT2,m_edit1+"is not palindrome"); } void prog13::OnButton2() { exit(0);
} Output :
Result : Thus a VC++ program to check whether a given string is a palindrome or not is
implemented and executed successfully.
PATTERN NUMBERS USING FOR LOOPS Ex. No :15 Aim:
To implement a VC++ program to print pattern numbers. Procedure:
1. Start 2. Create a form with one list box and 2 command buttons 3. Write source code for the click events of compute button to print pattern numbers and appropriate code for exit button. 4. Stop Property Settings Control Button Button Member Variables: Control IDC_LIST1
Form Design :
Name button1 button2
Property caption caption
Value Compute Exit
Category Control
Type CList
Variable Name m_list1
Source code:void prog14::OnButton1() { int i,j; CString st,str; UpdateData(true); for(i=1;i<=5;i++) {
str=" "; for(j=1;j<=i;j++) { st.Format(("%d"),i); str=str+st; } m_list1.AddString(str); } } void prog14::OnButton2() { exit(0); } Output :
Result : Thus the VC++ program to print pattern numbers has been implemented and executed
successfully.
MINIPAINT APPLICATION Ex. No: 16 Aim:
To implement a VC++ program to handle menus on a window. Procedure:
1. Start 2. Create menus as follows.
3. Create a window as follows.
4. Write code for the created menu items. 5. Stop.
Source code:
// CProg15View message handlers int s,b,p; void CProg15View::OnShapesEllipse() { s=1; Invalidate(); } void CProg15View::OnShapesLine() { s=2; Invalidate(); } void CProg15View::OnShapesRectangle() { s=3; Invalidate(); } void CProg15View::OnColorPenRed() { p=1; Invalidate(); } void CProg15View::OnColorPenGreen() { p=2; Invalidate(); } void CProg15View::OnColorPenBlue() { p=3; Invalidate(); } void CProg15View::OnColorBrushRed() { b=1; Invalidate(); } void CProg15View::OnColorBrushGreen() {
b=2; Invalidate(); } void CProg15View::OnColorBrushBlue() { b=3; Invalidate(); } void CProg15View::OnPaint() { CPaintDC dc(this); // device context for painting CPen P; CBrush B; switch(p) { case 1: P.CreatePen(PS_SOLID,2,RGB(255,0,0)); break; case 2: P.CreatePen(PS_SOLID,2,RGB(0,255,0)); break; case 3: P.CreatePen(PS_SOLID,2,RGB(0,0,255)); break; } switch(b) { case 1: B.CreateSolidBrush(RGB(255,0,0)); break; case 2: B.CreateSolidBrush(RGB(0,255,0)); break; case 3: B.CreateSolidBrush(RGB(0,0,255)); break; } dc.SelectObject(&P); dc.SelectObject(&B); switch(s) { case 1: dc.MoveTo(100,100); dc.LineTo(200,200); break;
case 2: dc.Rectangle(80,80,160,160); break; case 3: dc.Ellipse(30,20,80,80); break; } } Output :
Result : Thus a Visual C++ program to handle menus in a window has been implemented and
executed successfully.
FILE OPERATIONS Ex. No: 17 Aim:
To implement a Visual C++ program to copy files. Procedure:
1. Start 2. Create a MFC application by selecting, File -> New -> MFC .exe -> Dialog Based -> Finish. 3. Place 2 label boxes, 2 edit boxes and 2 command buttons as shown in the form layout. 4. Display appropriate messages in the label boxes. 5. On clicking the copy command button, write code to copy the contents of source file to destination file. 6. Write code for the exit command button. 7. Stop.
Property Settings Control Button Button
Name button1 button2
Property caption caption
Value Copy Exit
Control IDC_EDIT1 IDC_EDIT2
Category Value Value
Type CString CString
Variable Name m_edit1 m_edit2
Form Design:
Source Code:
// prog16 message handlers void prog16::OnButton1() { char szbuffer[100]; UINT nACTUAL=0, pos=0; CFile myfile, myfile1; UpdateData(true); myfile.Open(m_edit1,CFile::modeReadWrite); myfile.Open(m_edit2,CFile::modeCreate|CFile::modeReadWrite); while(1) { myfile.Seek(pos,CFile::begin); nACTUAL=myfile.Read(szbuffer,100); myfile.Write(szbuffer,100); pos=pos+nACTUAL; if(nACTUAL==0) { MessageBox("file copied"); exit(0); } } } void prog16::OnButton2() { exit(0); }
Output :
Result : Thus a Visual C++ program to copy files has been implemented and executed
successfully.
MOUSE INTERFACE Ex. No: 18 Aim:
To implement a VC++ program for mouse interface. Procedure:
1. Create a MFC application by selecting, File -> New -> MFC .exe -> Dialog Based -> Finish. 2. Place the edit box, Static Text and command buttons as shown in the form layout. 3. Write code to count the number of mouse clicks by counting the mouse events. 4. Stop Control Button
Name button1
Property Caption
Value Exit
Events: OnRButtonDown(), OnLButtonDown, OnLButtonDblClk(), OnRButtonDblClk(). SetDlgItemInt().
Form Design :
Source code :
int count1,count2; void prog17::OnLButtonDblClk(UINT nFlags, CPoint point) { count1++; SetDlgItemInt(IDC_EDIT1,count1); CFormView::OnLButtonDblClk(nFlags, point); } void prog17::OnLButtonDown(UINT nFlags, CPoint point) { count1++; SetDlgItemInt(IDC_EDIT1,count1); CFormView::OnLButtonDown(nFlags, point); } void prog17::OnRButtonDblClk(UINT nFlags, CPoint point) { count2++; SetDlgItemInt(IDC_EDIT2,count2); CFormView::OnRButtonDblClk(nFlags, point); } void prog17::OnRButtonDown(UINT nFlags, CPoint point) { count2++; SetDlgItemInt(IDC_EDIT2,count2); CFormView::OnRButtonDown(nFlags, point); } void prog17::OnButton1() { exit(0); }
Output :
Result : Thus a Visual C++ program for mouse interface has been implemented and executed
successfully.
ACTIVEX CONTROL Ex. No: 19 Aim:
To implement a VC++ program to insert an ActiveX control in an application. Procedure:
1. Start 2. Create a MFC application by selecting, File -> New -> MFC .exe -> Dialog Based -> Finish. 3. Place the ActiveX object and buttons as shown in the form layout. 4. To insert the Active X Calendar, right Click on the form -> Insert Active X Control -> Calendar Control 12.0. 5. On clicking the OK button, display a Message box. 6. Stop Control Button Button Events: OnButton() Form Design:
Name button1 button2
Property Caption Caption
Value Message OK
Source code :
oid prog18::OnButton1() { MessageBox("ActiveX controls"); } void prog18::OnButton2() { exit(0); }
Output :
Result : Thus a Visual C++ program to insert an ActiveX control has been implemented and
executed successfully.
MESSAGE BOX DISPLAY Ex. No: 20 Aim:
To implement VC++ program to create a message box. Procedure:
1. Start
2. Create a WIN32 application by selecting, File -> New -> Win32 .exe -> Finish. 3. To include a new C File, file -> New -> C Source File. 4. Read the free Memory Available in the system. 5. Store it in a buffer. 6. Display the Message box. 7. Stop Form Design :
Source Code:-
#include "stdafx.h" int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int ncmdshow) { DWORD dwmemavail; char szbuffer[80]; dwmemavail=GetFreeSpace(0); wsprintf(szbuffer,"Memory available:%lu",dwmemavail); MessageBox(NULL,szbuffer,"GLOBEL MEM",MB_OK); return 0; } Output :
Result : Thus the Visual C++ program to display a message box has been implemented and
executed successfully.
CREATING AND USING DLLS Ex. No: 21 Aim :
To implement VC++ program to create and use DLL. Procedure : To create DLL
1. Create a project MyDll from File->New, select the MFCAppWizard(dll) and the next step select Regular DLL using shared MFC DLL 2. In the class view tab on the workspace right click on MyDll classes and choose New Class, a dialog will appear, now choose the class type as Generic class and type the class name as CMyClass and press OK. 3. In the class view tab right click on CMyClass and select Add Member Function Function type
Function Declaration
CString
SayHello(CString strName)
4. In the File view tab on the workspace, open the Header file folder and double click on the “MyClass.h” and add “__declspec(dllexport) infront of all functions that are used for external application 5. In the File view tab on the workspace, open the MyClass.cpp from source file folder and type the code. 6. Compile & Build the project To create a new application that use the DLL.
1. Create a project named TestDll from File->New , select the MFCAppwizard(exe) , select Dialog based application and accept the default values for the next step and finish it. 2. A dialog will appear.Design the dialog window using necessary controls as follows
3. Open class wizard from the view menu, click member variable tab and choose IDC_EDIT1 and click add member variable and type as follows. Variable name
Type
Category
m_edit
Value
Cstring
4. Double click the “OK” button on the dialog window, then the OnOk function will be generated. 5. In the file view tab on the work space window, open the “TestDllDlg.h” to include “MyClass.h” and to declare object of that class and add the codes and include #include “..\MyDll\MyClass.h” 6. Select the Project->Settings->Link and in the Object/library modules enter a path to the DLL library file as follows. “..\MyDll\Debug\MyDll.lib” 7. Copy the MyDll.dll file from MyDll\Debug folder and paste it into the TestDll\Debug folder. 8. Build and execute the project
Source code //MyClass.hc
lass CMyClass {public: __declspec(dl lexport)CStrin gSayHello(CS tringstrName) ; __declspec(dllexport)CMyClass(); __declspec(dllexport)virtual ~CMyClass(); }; //MyClass.cpp
CStringCMyClass::SayHello(CStringstrName) {
return "HAI "+strName;
} TESTDLL //TestDllDlg.h
#include "..\MyDll\MyClass.h" class CTestDllDlg : public CDialog {public: CMyClassobjMyClass; CTestDllDlg(CWnd* pParent = NULL); }; //TestDllDlg.cpp
voidCTestDllDlg::On OK( ) { UpdateData(true); CStringstr=objMyClass.SayHello(m_strText); AfxMessageBox(str); CDialog::OnOK(); }