Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual."— Presentation transcript:

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

2 Objectives Describe the benefits of using ActiveX controls. Describe the benefits of using ActiveX controls. Describe how an ActiveX control differs from an ActiveX Automation Server. Describe how an ActiveX control differs 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 events of an ActiveX control. Expose properties, methods, and events of an ActiveX control.

3 Overview: Introduction to Controls Controls are reusable objects that include visual elements and code. Controls are reusable objects that include visual elements and code. Controls must be placed in some type of container, such as a form or an application. Controls must be placed in some type of container, such as a form or an application.

4 Control Classes Control Class (.ctl) acts as a template for that control. When you place a control on a form, you create an instance of that control. Control Class (.ctl) acts as a template for that control. When you place a control on a form, you create an instance of that control.

5 Control Components A control component (.ocx) can provide more than one kind of control. A control component (.ocx) can provide more than one kind of control.

6 Controls vs. Code Components A code component exposes functionality and can be used by other applications through Automation. A code component exposes functionality and can be used by other applications through Automation. Control components contain visual elements that can generate events based on user actions. Control components contain visual elements that can generate events based on user actions.

7 Controls vs. Code Components

8 The UserControl Object A foundation for building controls. A foundation for building controls. Source code and property values of a UserControl object -->.ctl (compare.frm) Source code and property values of a UserControl object -->.ctl (compare.frm) Graphics -->.ctx (compare.frx) Graphics -->.ctx (compare.frx)

9 Options for Distributing Controls as compiled components as compiled components as source code as source code

10 Steps for Creating an ActiveX Controls: Demonstration 1.Create the user interface for the control. 2.Provide the properties and methods of the control. 3.Define how the control will react to events. 4.Add property pages for the control. 5.Debug and test the control.

11 Creating a Control’s User Interface Adding Constituent Controls: in the same way as adding controls to a standard VB form. Adding Constituent Controls: in the same way as adding controls to a standard VB form.

12 Testing a Control You must test the control’s design-time functionality. You have to create another project that you use to test it. You must test the control’s design-time functionality. You have to create another project that you use to test it. Creating a test project. Debug the control in the same way as debugging an in-process component. Creating a test project. Debug the control in the same way as debugging an in-process component.

13 Exposing Properties, Methods, and Events 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 Wizard Using the ActiveX Control Interface Wizard

14 Adding Properties: Creating a Property To indicate that a property value has changed, use the PropertyChanged method. To indicate that a property value has changed, use the PropertyChanged method. Public Property Get Name()As String Name = txtName.Text End Property Public Property Let Name(ByVal NewName As String) txtName.Text = NewName PropertyChanged "Name" End Property

15 Exposing Properties of Constituent Controls Public Property Get Caption()As String Caption = lblName.Caption End Property Public Property Let Caption(ByVal NewCaption As String) lblName.Caption = NewCaption PropertyChanged "Caption" End Property

16 Mapping a Property to Multiple Controls Public Property Get ForeColor() As OLE_COLOR ForeColor = MyControl.ForeColor End Property Public Property Let ForeColor(ByVal NewfColor As OLE_COLOR) Dim ctlElement As Object MyControl.ForeColor = NewfColor For Each ctlElement In Controls If(TypeOf ctlElement Is Label) _ Or (TypeOf ctlElement Is CheckBox) _ Then ctlElement.ForeColor = NewfColor Next PropertyChanged "ForeColor" End Property

17 Adding Methods: Creating a Method Public Sub ShowDate() MsgBox "Date is: " & Now() End Sub

18 Exposing Methods of Constituent Controls Public Sub IDFocus() txtEmpID.SetFocus End Sub Private Sub cmdSetIdFocus_Click() MyControl.IDFocus End Sub

19 Exposing Named Constants (Enumerations) A convenient way to group a set of related named constants and associate them with constant values. A convenient way to group a set of related named constants and associate them with constant values. Public Enum TempBoilCelsius msubWater = 100 msubIron = 2750 msubNitrogen = -195.8 msubGold = 2807 End Enum

20 Raising Control Events 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 control can use events the control raises to add functionality to the control.

21 Raising Control Events Raising a Control Event Raising a Control Event –First declare the event, then use the RaiseEvent 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

22 Exposing Constituent Control Events n Declare a new event in the UserControl object n In the constituent control’s event, raise your own event. Public Event MyKeyPress(KeyAscii As Integer) Private Sub Text1_KeyPress(KeyAscii As Integer) RaiseEvent MyKeyPress (KeyAscii) End Sub

23 Using ActiveX Control Interface Wizard Demonstration Demonstration

24 Distributing a Control: Licensing Controls Purpose: prevents other developers from using your control to create their own control. Purpose: prevents other developers from using your control to create their own control. To use lincensed constituent control as a part of a control: To use lincensed constituent control as a part of a control: –Users of your control must have the controls installed or –the control vendor includes the lincensing key with the control’s setup program.

25 Distributing a Control: Creating a Control Setup Program Use the Application Setup Wizard Use the Application Setup Wizard (optional) Demonstration (optional) Demonstration

26 Lab: Creating ActiveX Control Demonstration Demonstration

27 Assessment 4. What is the major benefit of using named constants (enumerations)? 5. Consider the following code: Public Property Get Caption()As String Caption = lblName.Caption End 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?

28 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.


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

Similar presentations


Ads by Google