chapter 2: creating activex code components by นภดล...
TRANSCRIPT
![Page 1: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/1.jpg)
2Chapter :CCCCCCCC CCCCCCC CCCC CCC CCCCCCC
By นภดล กมลวิลาศเสถี�ยร Dept. of Computer Engineering, Pri
nce of Songkla University Source: Mastering Visual Basic 5, M icrosoft Corporation
![Page 2: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/2.jpg)
CCCCCCCCCC Animation: Chapter Introduction Use a class module to create an object wi
thin a Visual Basic project. Create an ActiveX code component that e
xposes properties, events, and methods. Create a client application that uses your
code component. Debug and test your code component. Raise events from your code component.
![Page 3: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/3.jpg)
Outline
Overview Creating Objects in Visual Basic Working with ActiveX Code Compon
ent Projects Testing ActiveX Code Components Using Events
![Page 4: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/4.jpg)
Overview
ActiveX technology lets you assem ble reusable components into appli
cations and services. An ActiveX component is a unit of e
xecutable code, such as an .exe, .dl l, or .ocx file, that follows the Active X specification for providing objects
. It exposes objects that can be use d by other applications.
![Page 5: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/5.jpg)
CCCCC CC CCCCCCC CCCCCCCnt s ActiveX Controls - User interface elements that allow you to
rapidly assemble reusable forms and dialo g boxes
ActiveX Documents ActiveX components that must be hosted
and activated within a client application. ActiveX Code Components Libraries of objects. Client applications cr
eate objects from classes provided by the component. Clients call the properties, me
thods, and events provided by the object.
![Page 6: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/6.jpg)
Advantages of Using Activ eX Code Comopnents
Clearly defined interfaces:
Properties, methods and events exposed by code com ponent in a standard way. Can includedescriptive info rmation and help.
Reduced complexity:
Hide programming complexity from other programmers.
Easier updating: Update things, such as business rules, just in the code
component, not all the applications.
![Page 7: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/7.jpg)
Creating Objects in VisualBasic What is a Class Module? Creating an Instance of a Class Class Module Events Creating Methods Creating Properties Using Property Procedures to Creat
e Properties
![Page 8: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/8.jpg)
CC C CCCCC CCCCCCC?
Purpose: blueprint for an object. Each cl ass module defines one type of object.
For example, you might create an Empl oyee class that has properties such as
Employee.LastName and Employee.FirstName, and methods such as Employee.Hire.
Your application can work with multiple instances of an Employee object.
![Page 9: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/9.jpg)
Creating an Instance of aClCCC
Class modules differ from standard mod ules these two ways.
Class modules must be explicitly creat ed before they can be used.
You can create multiple instances of a class module.
Dim objMyObject1 As Class1Dim objMyObject2 As Class1 Set objMyObject1 = New Class1Set obMyObject2 = New Class1 Animation: Class Instancing
![Page 10: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/10.jpg)
CCCCC CCCCCC CCCCCC
- Build in events: Initialize and Terminate
Private Sub Class_Initialize()'Initialize data.iDept = 5
End Sub
Private Sub Class_Terminate()'Any termination code.
End Sub
![Page 11: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/11.jpg)
Creating Methods
Public Sub ShowDate()MsgBox "Date is: " & Now()
End Sub
Public Function SquareIt (Num As Integer) _As IntegerSquareIt = Num * Num
End Function
Dim Demo1 As Class1Set Demo1 = New Class1Demo1.ShowDatei = Demo1.SquareIt (Num:=5)
![Page 12: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/12.jpg)
Creating Properties
Define public variables or Create public property procedures
within your class module
Using Public Variables to CreatePropertiesDim Demo1 As Class1Set Demo1 = New Class1Demo1.User = "Joe"
![Page 13: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/13.jpg)
CCCCC CCCCCCCC CCCCCCCC es t o Cr eat e Pr oper t i es
Usage– Run a procedure when a property value is changed or read.– Constrain a property to a small set of valid values.– Expose a property that is read-only.
![Page 14: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/14.jpg)
Creating a Property
Assigns a string value and retur ns the value for the User proper
ty.
Private gsUser As String
Public Property Let User (s As String)gsUser = Ucase(s)
End Property Public Property Get User () As StringUser = gsUser
End Property
![Page 15: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/15.jpg)
CCCCCCC CCC CCCCCCCCCC C CCoperty This code creates an instance of Class
1 , and sets and retrieves the User property:Dim Demo1 As Class1Set Demo1 = New Class1Demo1.User = "Joe" 'Calls Let procedure.Print Demo1.User 'Calls Get procedure.
A property that returns a standard data type
, define Property Get and Let . A property that is an Object data type , defin e Property Get and Set.
![Page 16: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/16.jpg)
Demonstration CC CCC CC C reate and Use Classes
![Page 17: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/17.jpg)
Working with ActiveX Code Component Projects
- - - In Process vs. Out of Process: DLL v s. EXE
Project Properties Class Module Properties -- Compiling a Component > .dll or .
exe file -- Registering a Component > Using
REgsvr32.exe
![Page 18: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/18.jpg)
Testing ActiveX Code Components Setting Up a Test Project: Testing an
ActiveX DLL or EXE Setting a Reference to a Type Library Debugging a Component - Raising Run Time Errors Demonstration of how to create and t
est a basic code component
![Page 19: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/19.jpg)
- Raising Run Time Errors
To pass an error back to a client ap plication, use the Raise method in
a component.
ERR.Raise (Number, Source, Description, HelpFile, HelpContext)
![Page 20: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/20.jpg)
Component Code
Dim localSalary As Single
Public Property Let Salary(ByVal newSalary As Single) If newSalary < 0 Then Err.Raise 12345 + vbObjectError, , "Salary must be positive" Else localSalary = newSalary End IfEnd Property
Public Property Get Salary() As Single Salary = localSalaryEnd Property
![Page 21: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/21.jpg)
Client Code
Private Sub Command1_Click() Dim x As Company.Employee On Error GoTo HandleError Set x = New Company.Employee x.Salary = -50 MsgBox x.Salary Exit SubHandleError: If Err.Number = 12345 + vbObjectError Then MsgBox "Salary must be positive." End IfEnd Sub
![Page 22: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/22.jpg)
Br eaki ng on Er r or s
Break on All Errors Break in Class Module Break on Unhandled Errors
![Page 23: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/23.jpg)
Using Events
Code components use events to notify c lients that an action has occurred.
For example, a client may want to be no tified when a database value has chang
ed or when a message has arrived.
This section includes the following topics: Adding Events to a Class Handling Events in a Client
![Page 24: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/24.jpg)
Adding Events to a Class
Declaring an EventPublic Event Status(ByVal StatusText As String)
Raising an EventPublic Sub SubmitOrder() 'Method to simulate processing an order. RaiseEvent Status("Checking credit...") 'Simulate credit check delay. EndTime = Timer + 2 Do While Timer < EndTime DoEvents Loop
![Page 25: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/25.jpg)
Adding Events to a Class
Raising an Event (cont’d)
RaiseEvent Status("Processing Order...") 'Simulate processing. EndTime = Timer + 2 Do While Timer < EndTime DoEvents LoopEnd Sub
![Page 26: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/26.jpg)
Handling Events in a Client Declare a WithEvents variable
• Dim WithEvents Order As Project1.Class1
Create an event procedure to handl e the event.
• Private Sub Order_Status(ByVal StatusText As String)
• Debug.Print StatusText• End Sub
Create an instance of the class that is defined with the WithEvents varia
ble.• Private Sub Command1_Click()• Set Order = New Project1.Class1• Order.SubmitOrder• End Sub
![Page 27: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/27.jpg)
Demonstration of How to C reate and Use Events
![Page 28: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/28.jpg)
Lab: Cr eat i ng Act i veX Cl i enCC Exercise 1: Creating a Code Compo
nent Exercise 2: Debugging and Error Ha
ndling Exercise 3: Defining and Using Eve
nts Exercise 4: Compiling and Registeri
ng the Component
![Page 29: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/29.jpg)
The CreditCard Object
Name TypeCardNumber PropertyExpireDate PropertyPurchaseAmount PropertyApprove Method
![Page 30: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/30.jpg)
Assessment For the first three questions, give the correct answers
with explanations.4. Explain the difference between a class and an
object.5. Think of a problem that a class module can be
used to solve it. Specify properties, methods and events that the class may contain.6. Explain the process of handling errors from an
ActiveX code component to a client application.7. Give an example of situations when an ActiveX
code component need to raise an event to its client application. Provide code samples that raise an event on the code component side and handle the event on the client side.
![Page 31: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering](https://reader036.vdocuments.net/reader036/viewer/2022062308/56649e615503460f94b5d1b7/html5/thumbnails/31.jpg)
Lab Report
Your lab report should contain the following things:
1. Answers to assessment questions. For the first three questions, give the
correct answers with explanations. 2. Complete code of exercise 1, 2 ,and 3 with explanations of the statements in the code.