Download presentation
Presentation is loading. Please wait.
Published byGarry Roberts Modified over 9 years ago
1
Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual Basic 5, Microsoft Corporation
2
Objectives Animation: Chapter Introduction Use a class module to create an object within a Visual Basic project. Create an ActiveX code component that exposes 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.
3
Outline Overview Creating Objects in Visual Basic Working with ActiveX Code Component Projects Testing ActiveX Code Components Using Events
4
Overview ActiveX technology lets you assemble reusable components into applications and services. An ActiveX component is a unit of executable code, such as an.exe,.dll, or.ocx file, that follows the ActiveX specification for providing objects. It exposes objects that can be used by other applications.
5
Types of ActiveX Components ActiveX Controls User-interface elements that allow you to rapidly assemble reusable forms and dialog boxes ActiveX Documents ActiveX components that must be hosted and activated within a client application. ActiveX Code Components Libraries of objects. Client applications create objects from classes provided by the component. Clients call the properties, methods, and events provided by the object.
6
Advantages of Using ActiveX Code Comopnents Clearly defined interfaces: Properties, methods and events exposed by code component in a standard way. Can includedescriptive information 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.
7
Creating Objects in Visual Basic What is a Class Module? Creating an Instance of a Class Class Module Events Creating Methods Creating Properties Using Property Procedures to Create Properties
8
What is a Class Module? Purpose: blueprint for an object. Each class module defines one type of object. For example, you might create an Employee 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.
9
Creating an Instance of aClass Class modules differ from standard modules these two ways. Class modules must be explicitly created before they can be used. You can create multiple instances of a class module. Dim objMyObject1 As Class1 Dim objMyObject2 As Class1 Set objMyObject1 = New Class1 Set obMyObject2 = New Class1 Animation: Class Instancing
10
Class Module Events 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
11
Creating Methods Public Sub ShowDate() MsgBox "Date is: " & Now() End Sub Public Function SquareIt (Num As Integer) _ As Integer SquareIt = Num * Num End Function Dim Demo1 As Class1 Set Demo1 = New Class1 Demo1.ShowDate i = Demo1.SquareIt (Num:=5)
12
Creating Properties Define public variables or Create public property procedures within your class module Using Public Variables to Create Properties Dim Demo1 As Class1 Set Demo1 = New Class1 Demo1.User = "Joe"
13
Using Property Procedures to Create Properties 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.
14
Creating a Property Assigns a string value and returns the value for the User property. Private gsUser As String Public Property Let User (s As String) gsUser = Ucase(s) End Property Public Property Get User () As String User = gsUser End Property
15
Setting and Retrieving a Property This code creates an instance of Class1, and sets and retrieves the User property: Dim Demo1 As Class1 Set Demo1 = New Class1 Demo1.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, define Property Get and Set.
16
Demonstration of How to Create and Use Classes
17
Working with ActiveX Code Component Projects In-Process vs. Out-of-Process: DLL vs. EXE Project Properties Class Module Properties Compiling a Component -->.dll or.exe file Registering a Component --> Using REgsvr32.exe
18
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 test a basic code component
19
Raising Run-Time Errors To pass an error back to a client application, use the Raise method in a component. ERR.Raise (Number, Source, Description, HelpFile, HelpContext)
20
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 If End Property Public Property Get Salary() As Single Salary = localSalary End Property
21
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 Sub HandleError: If Err.Number = 12345 + vbObjectError Then MsgBox "Salary must be positive." End If End Sub
22
Breaking on Errors Break on All Errors Break in Class Module Break on Unhandled Errors
23
Using Events Code components use events to notify clients that an action has occurred. For example, a client may want to be notified when a database value has changed or when a message has arrived. This section includes the following topics: Adding Events to a Class Handling Events in a Client
24
Adding Events to a Class Declaring an Event Public Event Status(ByVal StatusText As String) Raising an Event Public 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
25
Adding Events to a Class Raising an Event (cont’d) RaiseEvent Status("Processing Order...") 'Simulate processing. EndTime = Timer + 2 Do While Timer < EndTime DoEvents Loop End Sub
26
Handling Events in a Client Declare a WithEvents variable Dim WithEvents Order As Project1.Class1 Create an event procedure to handle 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 variable. Private Sub Command1_Click() Set Order = New Project1.Class1 Order.SubmitOrder End Sub
27
Demonstration of How to Create and Use Events
28
Lab: Creating ActiveX Clients Exercise 1: Creating a Code Component Exercise 2: Debugging and Error Handling Exercise 3: Defining and Using Events Exercise 4: Compiling and Registering the Component
29
The CreditCard Object NameType CardNumberProperty ExpireDateProperty PurchaseAmountProperty ApproveMethod
30
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.
31
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.