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

Slides:



Advertisements
Similar presentations
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Advertisements

Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Trapping Errors and Debugging Code. Programming errors can vary by … Type Cause Effect Severity Other factors Error types Include Syntax Logic Compile.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
OOP-Creating Object-Oriented Programs
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
VB Classes BICS546. Adding a Class to a Project Project/Add Class Class will be saved in a.CLS file Steps: –Adding properties Declare Public variables.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Module 7: Object-Oriented Programming in Visual Basic .NET
Obsydian OLE Automation Ranjit Sahota Chief Architect Obsydian Development Ranjit Sahota Chief Architect Obsydian Development.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
An Introduction to Visual Basic
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 1: Creating ActiveX Clients By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual Basic.
Using Visual Basic for Applications (VBA) – Project 8.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.
Applications Development
MS Visual Basic 6 Walter Milner. VB 6 0 Introduction –background to VB, A hello World program 1 Core language 1 –Projects, data types, variables, forms,
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CHAPTER THREE Representing Data: Constants and Variables.
Understanding Visual Basic Fundamentals CHAPTER 13 Understanding Visual Basic Fundamentals.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Representing Data: Constants and Variables
A variable is a name for a value stored in memory.
Programming in visual basic .net Visual Basic Building Blocks
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Active X EXE (Out of Process Components)
Microsoft Access Illustrated
Using Procedures and Exception Handling
PL/SQL Scripting in Oracle:
Variables and Arithmetic Operations
VISUAL BASIC.
Microsoft Visual Basic 2005 BASICS
Presentation transcript:

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

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.

Outline Overview Creating Objects in Visual Basic Working with ActiveX Code Component Projects Testing ActiveX Code Components Using Events

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.

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.

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.

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

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.

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

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

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)

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"

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.

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

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.

Demonstration of How to Create and Use Classes

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

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

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)

Component Code Dim localSalary As Single Public Property Let Salary(ByVal newSalary As Single) If newSalary < 0 Then Err.Raise vbObjectError,, "Salary must be positive" Else localSalary = newSalary End If End Property Public Property Get Salary() As Single Salary = localSalary End Property

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 = vbObjectError Then MsgBox "Salary must be positive." End If End Sub

Breaking on Errors Break on All Errors Break in Class Module Break on Unhandled Errors

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

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

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

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

Demonstration of How to Create and Use Events

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

The CreditCard Object NameType CardNumberProperty ExpireDateProperty PurchaseAmountProperty ApproveMethod

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.

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.