Listados de la Creación de un Control ActiveX Paso a Paso
Listado 1 | ||||||||||||
|
Listado 2 |
Below is a To Do list of
items required to complete the testing, debugging, and
bullet-proofing of your UserControl. A) Create a test program for your UserControl There are two ways to set up a test program for your UserControl, depending on whether you inserted the control in a Standard EXE, or created an ActiveX Control project for it. If you created an ActiveX Control project, the following steps will set up a test program: 1) Save your UserControl. 2) Close your UserControl's designer, to put the control in run mode. 3) If you haven't already created a test project, add a Standard EXE project by selecting Add Project from the File menu. 4) In the Toolbox, double-click your UserControl's icon to place an instance of your UserControl on Form1, in the Standard EXE project. You can move and resize the control as needed. 5) Save the project group. In later development and test sessions, you can open both projects at once by opening the project group. If you inserted the UserControl in an existing Standard EXE project, follow these steps: 1) Save your UserControl. 2) Close your UserControl's designer, to put the control in run mode. 3) In the Project window, double-click Form1 in the Standard EXE project, to open its designer. 4) In the Toolbox, double-click your UserControl's icon to place an instance of your UserControl on Form1. You can move and resize the control as needed. B) Test your control's behavior at design time, and at run time. 1) Select the control you placed on Form1 in the test project, and press F4 to open the Properties window. Verify that you can see and change the properties you added to your control. 2) Close Form1 and reopen it, and verify that the property values for your control were correctly saved and retrieved. 3) Double-click the control you placed on Form1, and verify that the appropriate events appear in the right-hand (Procedure) drop down of the code window. 4) Add test code to the event procedures for your control. 5) Add other controls, and put code in their event procedures to test the run-time behavior of your control's properties and methods. 6) Press F5 to run the test project, and test your control's run-time behavior. C) Making a Bullet-Proof, Full-Featured Control (Adding code details the Wizard doesn't provide) 1) If you have constituent controls on your form, some events and properties may need to be mapped to multiple constituent controls. For example, a BackColor property probably should map to the BackColor properties of the UserControl AND any label controls. The MouseMove event needs to map to the MouseMove events of ALL constituent controls. 2) Add coordinate translation to all events (such as MouseMove) that specify X and Y coordinates. 3) For all properties that have enumerations (for example, MousePointer and BorderStyle), change the data type of the property to the appropriate enumeration name (in this case, MousePointerConstants and BorderStyleConstants) so that enumeration elements will show up in the Properties window. 4) Add any custom enumerations you need for your own properties, plus code to validate them. 5) Add error trapping to the ReadProperties event, to protect against invalid values or bad data types that might be manually edited into an .frm file. For each property, add code to switch to the default setting when such an error occurs. (See "Saving the Properties of Your Control" and "Creating Design-Time-Only, Run-Time-Only, or Read-Only Run-Time Properties" in Books Online.) 6) If you have constituent controls, add code to UserControl_Resize to resize them when the control is resized. 7) Set the Procedure ID for the Enabled property, so your control will behave the same as other ActiveX controls when enabled and disabled. 8) The wizard maps properties of your control to constituent control (or UserControl) properties of like name. In some cases, you may want to map a property to a property with a different name (for example, ShapeLabel maps its BackColor to the constituent Shape control's FillColor); this remapping must be done manually. 9) Any properties that might affect the size of your control (such as font size, in a control that has an AutoSize property) should call your resize code from the Property Let. 10) For user-drawn controls, add code to the UserControl's Paint event to paint your control's appearance. (See "User-Drawn Controls" and "How to Handle Focus in Your Control" in Books Online.) 11) If one or more properties of your control will be data-bound, see "Binding a Control to a Data Source," in Books Online. 12) Add additional features to your control. You may find it useful to peruse the topics in "Visual Basic ActiveX Control Features," in Books Online. (See the CtlPlus.vbg sample application for examples of these work items.) You can modify your control by running the Wizard again, and selecting your UserControl. Use the Property Page Wizard to create Property Pages for your UserControls. For more information on creating and testing ActiveX Controls, please read Chapter 4, "Creating an ActiveX Control," and Chapter 9, "Building ActiveX Controls." Chapter 6, "General Principles of Component Design," and Chapter 7, "Debugging, Testing, and Deploying ActiveX Components," contain additional information you may find useful. |
Listado 3 |
Private Sub Text1_Change() 'Por si el número es "exagerado" On Local Error Resume Next VScroll1.Value = Val(Text1) If Err Then Text1 = VScroll1.Min Err = 0 End If On Local Error GoTo 0 RaiseEvent Cambiado End Sub Private Sub VScroll1_Change() Text1.Text = VScroll1.Value RaiseEvent Change End Sub |
Listado 4 |
Public Property Let Max(ByVal New_Max As Integer) VScroll1.Max() = New_Max PropertyChanged "Max" End Property Public Property Let Máximo(ByVal New_Máximo As Integer) VScroll1.Max() = New_Máximo PropertyChanged "Máximo" End Property Public Property Let Min(ByVal New_Min As Integer) VScroll1.Min() = New_Min PropertyChanged "Min" End Property Public Property Let Mínimo(ByVal New_Mínimo As Integer) VScroll1.Min() = New_Mínimo PropertyChanged "Mínimo" End Property Public Property Let Valor(ByVal New_Valor As Integer) VScroll1.Value() = New_Valor PropertyChanged "Valor" End Property Public Property Let Value(ByVal New_Value As Integer) VScroll1.Value() = New_Value PropertyChanged "Value" End Property |
Listado 5 |
Public Property Let Max(ByVal New_Max As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Max() = New_Max PropertyChanged "Max" Me.Máximo = New_Max YaEstoy = False End Property Public Property Let Máximo(ByVal New_Máximo As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Max() = New_Máximo PropertyChanged "Máximo" Me.Max = New_Máximo YaEstoy = False End Property Public Property Let Min(ByVal New_Min As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Min() = New_Min PropertyChanged "Min" Me.Mínimo = New_Min YaEstoy = False End Property Public Property Let Mínimo(ByVal New_Mínimo As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Min() = New_Mínimo PropertyChanged "Mínimo" Me.Min = New_Mínimo YaEstoy = False End Property Public Property Let Valor(ByVal New_Valor As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Value() = New_Valor PropertyChanged "Valor" Me.Value = New_Valor YaEstoy = False End Property Public Property Let Value(ByVal New_Value As Integer) Static YaEstoy As Boolean If YaEstoy Then Exit Property YaEstoy = True VScroll1.Value() = New_Value PropertyChanged "Value" Me.Valor = New_Value YaEstoy = False End Property |
Listado 6 |
Private Sub UserControl_Resize() 'Adaptar los controles al nuevo tamaño With UserControl Text1.Top = 0 VScroll1.Top = 30 Text1.Width = .ScaleWidth Text1.Height = .ScaleHeight VScroll1.Left = .ScaleWidth - VScroll1.Width - 30 VScroll1.Height = .ScaleHeight - 60 End With End Sub |
Listado 7 |
'Form de ejemplo para el control personalizado Option Explicit Private Sub cmdSalir_Click() Unload Me End End Sub Private Sub Form_Load() End Sub Private Sub Form_Unload(Cancel As Integer) Set Form1 = Nothing End Sub Private Sub TextSpin1_Change(Index As Integer) Dim LBack As Long Dim sHex As String Dim iValor As Integer iValor = TextSpin1(Index).Value Select Case Index Case 0 'Rojo sHex = "0000" & Hex$(iValor) Case 1 'Verde sHex = "00" & Hex$(iValor) & "00" Case 2 'Azul sHex = Hex$(iValor) & "0000" End Select Label2(Index) = CStr(iValor) & ", " & sHex Label1(Index).BackColor = Abs(Val("&H" & sHex & "&")) End Sub |
Listado 8 |
Property Page Wizard
Instructions: The Property Page Wizard has finished creating and modifying property pages. Below is a To Do list of items required to complete the testing, debugging, and bullet-proofing of your property page(s). A) Testing and Debugging Property Pages. 1) Property pages are always tested in design mode. Add an instance of your control to a test form, as described in "Debugging Controls," in Books Online. 2) Select the instance of your control, and press Shift+F4 (or right-click the control, and select Properties from the context menu) to open the Property Pages dialog box. 3) The property pages for your control will appear in the Property Pages dialog box. You can tab between pages, and set the various properties each page displays. Make sure that properties are updated in the Properties window when you move off a property page. 4) You should also test your property pages with multiple controls selected; you can do this by placing multiple instances of your control on the test form. 5) The Property Pages dialog box is modeless, so the selection can be changed while your property page is running. You can test this by selecting and unselecting additional instances of your control. (Hold down the Ctrl key and click on a control to add or remove it from the selection list.) 6) If you've used the Procedure Attributes dialog box to associate a property page with a property in the Properties window, be sure to test the property page by clicking the ellipsis button the Properties window displays beside the property. B) Making Bullet-Proof, Full-Featured Property Pages For properties that are Enums (including VB-provided Enums, like MousePointerConstants), change the text box the wizard provides to a drop down list; create a list of enum element names, and add code to the SelectionChanged event to load the list and select the current value (see Ch. 10 and the CtlPlus.vbg sample application). For numeric properties, optionally add code to the KeyPress event to prevent invalid values from being entered. If you've declared a color property as Long, you'll have to do a lot of work to validate a color entered in the text box. A much better strategy is to change the property type to OLE_COLOR; that way, it will hook up automatically to the standard Color page. Determine which of your properties users would want to set to the same value for ALL of the controls in a multiple selection (as opposed to those that should only be set for the first selected control). See the CtlPlus.vbg sample application. If there are fields where it's possible for the user to enter an invalid value, add error-trapping and validation code to the ApplyChanges event. (In the case of invalid data, set Changed = True to prevent the property page from closing. See "How Property Pages Work," in Books Online.) Add a module-level flag to prevent the initial property value load (in the SelectionChanged event) from marking all properties as 'dirty.' (See CtlPlus.vbg sample application.) Property pages are covered in depth in "Creating Property Pages for ActiveX Controls," in Books Online. A simple example is shown in the step-by-step procedures in "Creating an ActiveX Control." A slightly more complex example can be found in the CtlPlus.vbg sample application. You can save this report by clicking the "Save" button, or you can discard it by clicking "Close". |
Listado 9 |
Sub AjustarValores(Index As Integer) Dim LBack As Long Dim sHex As String Dim iValor As Integer iValor = TextSpin1(Index).Value Select Case Index Case 0 'Rojo sHex = "0000" & Hex$(iValor) Case 1 'Verde sHex = "00" & Hex$(iValor) & "00" Case 2 'Azul sHex = Hex$(iValor) & "0000" End Select Label2(Index) = CStr(iValor) & ", " & sHex Label1(Index).BackColor = Abs(Val("&H" & sHex & "&")) End Sub |
Listado 10 |
Private Sub TextSpin1_Change(Index As Integer) AjustarValores Index End Sub |
Listado 11 |
Private Sub Form_Load() Dim i As Integer For i = 0 To 2 AjustarValores i Next End Sub |
Última Actualización: 29/Mar/97