Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering,...

28
Chapter 3: Chapter 3: Creating ActiveX Contr Creating ActiveX Contr ols ols By Noppadon Kamolvilassat By Noppadon Kamolvilassat ian ian Dept. of Computer Enginee Dept. of Computer Enginee ring, Prince of Songkla U ring, Prince of Songkla U niversity niversity Source: Mastering Visual Source: Mastering Visual

Transcript of Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering,...

Page 1: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

3Chapter : 3Chapter :CCCCCCCC CCCCCCC CCCCCCCCCCCCCCCC CCCCCCC CCCCCCCC

By Noppadon Kamolvilassatia By Noppadon Kamolvilassatiann

Dept. of Computer Engineerin Dept. of Computer Engineerin g, Prince of Songkla University g, Prince of Songkla University

Source: Mastering Visual Basic Source: Mastering Visual Basic 5, Microsoft Corporation 5, Microsoft Corporation

Page 2: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

CCCCCCCCCCCCCCCCCCCC Describe the benefits of using ActiveX Describe the benefits of using ActiveXcontrols.controls.

Describe how an ActiveX control differ Describe how an ActiveX control differ s from an ActiveX Automation Server. s from an ActiveX Automation Server.

Create an ActiveX control. Create an ActiveX control. Test and debug an ActiveX control. Test and debug an ActiveX control. Expose properties, methods, and even Expose properties, methods, and even

ts of an ActiveX control. ts of an ActiveX control.

Page 3: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Overview: Introduction to Overview: Introduction toControlsControls

Controls are reusable objects that i Controls are reusable objects that i nclude visual elements and code. nclude visual elements and code.

Controls must be placed in some ty Controls must be placed in some ty pe of container, such as a form or a pe of container, such as a form or a

n application. n application.

Page 4: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Control Classes Control Classes

Control Class (.ctl) acts as a template for tha Control Class (.ctl) acts as a template for tha t control. When you place a control on a form t control. When you place a control on a form , you create an , you create an instanceinstance of that control. of that control.

Page 5: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Control Components Control Components

A control component (.ocx) can pro A control component (.ocx) can pro vide more than one kind of control. vide more than one kind of control.

Page 6: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Controls vs. Code Compon Controls vs. Code Componentsents

A code component exposes functionality and c A code component exposes functionality and c an be used by other applications through Auto an be used by other applications through Auto

mation.mation. Control components contain visual elements th Control components contain visual elements th

at can generate events based on user actions. at can generate events based on user actions.

Page 7: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Controls vs. Code Compon Controls vs. Code Componentsents

Page 8: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

CCC CCCCCCCCCCC CCCCCCCCC CCCCCCCCCCC CCCCCC A foundation for building controls. A foundation for building controls. Source code and property values of Source code and property values of

-- a UserControl object > .ctl (comp -- a UserControl object > .ctl (comp are .frm) are .frm)

-- Graphics > .ctx (compare .frx) -- Graphics > .ctx (compare .frx)

Page 9: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Options for Distributing Co Options for Distributing Controlsntrols

as compiled components as compiled components as source code as source code

Page 10: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Steps for Creating an Activ Steps for Creating an Activ eX Controls: Demonstratio eX Controls: Demonstratio

nn

1.1. CCC CCCC CCCCCCCCC CCC CCC CCCCCCCC. CCC CCCC CCCCCCCCC CCC CCC CCCCCCCC.2.2. Providet he pr oper t i es and met hods of t he Providet he pr oper t i es and met hods of t hecontrol.control.

3.3. CCC CCC CCCCCCC CCCC CCCCC CC CCCCCCC. CCC CCC CCCCCCC CCCC CCCCC CC CCCCCCC.4.4. CCCCCCCC CCCCC CCC CCC CCCCCCCC. CCCCCCCC CCCCC CCC CCC CCCCCCCC.5.5. Debug and test the control. Debug and test the control.

Page 11: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Creating a Control’s User I Creating a Control’s User Interfacenterface

Adding Constituent Controls: in the same w Adding Constituent Controls: in the same w ay as adding controls to a standard VB form. ay as adding controls to a standard VB form.

Page 12: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Testing a Control Testing a Control - You must test the control’s design time functionality. Yo - You must test the control’s design time functionality. Yo

u have to create another project that you use to test it. u have to create another project that you use to test it. Creating a test project. Debug the control in the same w Creating a test project. Debug the control in the same w

- ay as debugging an in process component. - ay as debugging an in process component.

Page 13: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

CCCCCCCC CCC CCCCCC,, CCCCCCCC CCC CCCCCC,,

Adding Properties Adding Properties Adding Methods Adding Methods Exposing Named Constants Exposing Named Constants Raising Control Events Raising Control Events Using the ActiveX Control Interface Using the ActiveX Control InterfaceWizardWizard

Page 14: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Adding Properties: Creatin Adding Properties: Creatin g a Property g a Property

To indicate that a property value has ch To indicate that a property value has ch anged, use the anged, use the PropertyChanged PropertyChanged methmeth

od.od.Public Property Get Name()As StringName = txtName.Text

End Property Public Property Let Name(ByVal NewName As String)txtName.Text = NewNamePropertyChanged "Name"

End Property

Page 15: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Exposing Properties of Con Exposing Properties of Con stituent Controls stituent Controls

Public Property Get Caption()As StringCaption = lblName.Caption

End Property Public Property Let Caption(ByVal NewCaption As String)lblName.Caption = NewCaptionPropertyChanged "Caption"

End Property

Page 16: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Mapping a Property to Mult Mapping a Property to Mult iple Controls iple Controls

Public Property Get ForeColor() As OLE_COLORForeColor = MyControl.ForeColor

End Property

Public Property Let ForeColor(ByVal NewfColor As OLE_COLOR)Dim ctlElement As ObjectMyControl.ForeColor = NewfColorFor Each ctlElement In Controls

If(TypeOf ctlElement Is Label) _Or (TypeOf ctlElement Is CheckBox) _Then ctlElement.ForeColor = NewfColor

NextPropertyChanged "ForeColor"

End Property

Page 17: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Adding Methods: Creating Adding Methods: CreatingC C CCCCCC C CCCCC

Public Sub ShowDate()MsgBox "Date is: " & Now()

End Sub

Page 18: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Exposing Methods of Const Exposing Methods of Const ituent Controls ituent Controls

Public Sub IDFocus() txtEmpID.SetFocusEnd Sub

Private Sub cmdSetIdFocus_Click() MyControl.IDFocusEnd Sub

Page 19: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Exposing Named Constant Exposing Named Constant s (E s (Enumerations)numerations)

A convenient way to group a set of r A convenient way to group a set of r elated named constants and associ elated named constants and associ

ate them with constant values. ate them with constant values.Public Enum TempBoilCelsiusmsubWater = 100msubIron = 2750msubNitrogen = -195.8msubGold = 2807

End Enum

Page 20: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

CCCCCCC CCCCCCC CCCCCCCCCCCCC CCCCCCC CCCCCC Received Events vs. Raised Events Received Events vs. Raised Events– You use the events your control

receives to add functionality to your control.

– A developer that uses your cont rol can use events the control ra

ises to add functionality to the control.

Page 21: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

CCCCCCC CCCCCCC CCCCCCCCCCCCC CCCCCCC CCCCCC Raising a Control Event Raising a Control Event– Firstdeclaret he event , t hen use t he Rai seE Firstdeclaret he event , t hen use t he Rai seE

vent statement to call the event. vent statement to call the event.'Declare a public Click

'event with no arguments.

Public Event Click()

The following line of code raises the Click event:

RaiseEvent Click

Page 22: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Exposing Constituent Cont Exposing Constituent Cont rol Events rol Events

Declare a new event in the UserControl objectDeclare a new event in the UserControl object In the constituent control’s event, raise your In the constituent control’s event, raise your

own event.own event.

Public Event MyKeyPress(KeyAscii As Integer)

Private Sub Text1_KeyPress(KeyAscii As Integer) RaiseEvent MyKeyPress (KeyAscii)End Sub

Page 23: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Using ActiveX Control Inter Using ActiveX Control Inter face Wizard face Wizard

DemonstrationDemonstration

Page 24: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

C CCCCCCCC CCCC: C CCCCCCCC CCCC: nsing Controls nsing Controls

Purpose: prevents other developers fro Purpose: prevents other developers fro m using your control to create their ow m using your control to create their ow

n control. n control. To use lincensed constituent control as To use lincensed constituent control as

a part of a control: a part of a control:– Users of your control must have the control Users of your control must have the control

s installed or s installed or– the control vendor includes the lincensing the control vendor includes the lincensing

key with the control’s setup program. key with the control’s setup program.

Page 25: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Distributing a Control: Distributing a Control: Creating a Control Setup Pr Creating a Control Setup Programogram

Use the Application Setup Wizard Use the Application Setup Wizard (optional) Demonstration (optional) Demonstration

Page 26: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Lab: Cr eat i ng Act i veXCont r Lab: Cr eat i ng Act i veXCont rCCCC

DemonstrationDemonstration

Page 27: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

AssessmentAssessment4. What is the major benefit of using named constants (enumerations)?

5. Consider the following code:Public Property Get Caption()As String

Caption = lblName.CaptionEnd Property

What is the difference between the variable ‘Caption’ and ‘lblName.Caption’?

6.There is an important distinction between the events received by your UserControl object (or by its constituent controls) and the events your control raises. What is the difference between the uses of these two types of events?

Page 28: Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.

Lab Report (Handed in by ...)Lab Report (Handed in by ...)

Your lab report should contain the following things:

1. Answers to the 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.