chapter 2: creating activex code components by นภดล...

31
Chapter 2: Creating ActiveX Code Components By นนนน นนนนนนนนนนนนนน Dept. of Computer Engineering , Prince of Songkla Universit y Source: Mastering Visual Basi c 5, Microsoft Corporation

Upload: garry-roberts

Post on 28-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

- 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

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

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

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

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

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

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

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

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

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

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

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

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.