|
|
Sign In Link to us |
|
Home 1. Introduction 2. Introducing VB 3. Projects 4. Containers 5. Controls 6. Code 7. Forms 8. Debugging Code 9. Error Handling 10. Objects 11. User Controls 11.1 Creating 11.2 Properties 11.3 Testing 11.4 Using a Client 12. Data Projects 13. API 14. Distribution
|
11. User ControlsUser Controls (also known as ActiveX controls) are bespoke objects that are written in a development environment, such as Visual Basic or Visual C++. Once compiled and made, these objects can then be used in any client application that supports ActiveX.ActiveX controls can be developed to take mundane or repetitive tasks from of the client application. For example, an HMI application could use an ActiveX to plot and print-out trend data. Third party ActiveXs often carry licensing requirements, so that they cannot be used in Visual Basic without an appropriate software key or license. When you develop ActiveX controls in Visual Basic, you can use the existing controls (and any other ActiveX controls that you have loaded) to create your new ActiveX. This is called 'Superclassing' because all of the classes which exist in the component controls will exist in the eventual ActiveX, albeit at a lower level. 11.1 CreatingAn ActiveX is created in a similar way as a "Standard EXE" program. The main difference is that the ActiveX cannot run on it's own - it needs a container. By default in Visual Basic, when you run an ActiveX project, it runs in Internet Explorer.You can make it run in a client Form by adding a form to the project and placing a copy of your ActiveX control onto it.
Exercise xvi: In this example, we will create an ActiveX which can browse the system to get a file name:
Note:
Notice that the User Control looks a bit like a Form - it can actually contain other controls, as we will see.
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub File1_Click()
Dim strPath As String
strPath = File1.Path
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strPath = strPath & File1.FileName
Text1.Text = strPath
End Sub
Private Sub Text1_Change()
If Text1.Text = "" Then
Command1.Enabled = False
Else
Command1.Enabled = True
End If
End Sub
Note:
The Right function is used to return a set number of characters from the right-hand side of a String type variable. In this case,
we want to check if the variable "strPath" already has a "\" character at the end. If it hasn't, we must add one before adding
the actual filename. The path should then make sense.
Event AcceptFile(Path As String)
Private Sub Command1_Click()
RaiseEvent AcceptFile(Text1.Text)
End Sub
11.2 Properties11.2.1 Property Wizard Add-inIn the previous exercise, we saw how to create an Event within an ActiveX and how to pass parameters to a client application. The new ActiveX also has some properties that can be seen in the Properties Window when an instance of it is selected in the Form. These properties are the default ones that Visual Basic assigns to every User Control that is developed. In the same way that we have 'Superclassed' some of the controls in the User Control, we can also 'Superclass' the existing properties and methods and create new ones. Properties can be created individually, using the Property Let and Property Get methods described earlier, but there is an easier automated way - using an Add-In. An Add-In is a small program that sits behind Visual Basic and when called upon, performs some tasks that speed up development in some way. Check that the Add-In is loaded:
Exercise:
In the User Control's Object View window, we can see that the ActiveX has various properties that don't appear in the instance we placed on the Form, such as "BackColor", "ForeColor" and "Font". We will add these properties and an additional custom one called "FilePath" that we will use to pre-load the TextBox on the ActiveX with an existing filename:
Exercise xvii:
Note:
Notice that the "AcceptFile" Event is already in the list, because we added it manually, however it is still a custom Event.
Note:
Notice that in the Properties Window, the "BackColor", "ForeColor" and "Font" are now available. In addition our custom property
"FilePath" is there.
Test the application. 11.2.2 Property Pages The properties that exist within a User Control can be added to a series of Property Pages. These pages are just an alternative way of accessing the object's properties during the design time environment. Customised properties can also be inserted onto a page quite easily. Once created, a property page will provide a dialogue interface which the user sees when the User Control's "Properties" option is selected from the pop-up menu that appears if you right-click on it. The easiest way of adding Property Pages is using the Wizard provided by Visual Basic, through the "Add Property Pages" option of the "Project" menu.
11.2.3 Accessing ActiveXs in the Container Since ActiveXs are objects, they can be used to declare instances in the client application. These instances can then have their properties, events and methods accessed using the dot (".") modifier, as normal. 11.3 TestingIn the exercise we have done, the User Control exists within the same project as the Form, so there is no need to make a reference to it. If we developed a totally separate ActiveX, we would need to Make it into an ".ocx" file, then refer to it from our client project.
Exercise xviii:
11.4 Using a Client11.4.1 Class IDEach time Visual Basic makes an ActiveX, it generates a random 32-digit number that is used by Windows to refer to that particular ActiveX. This unique number is called the Class Identification number or Class ID.
Exercise:
Note:
In this code, you will see that the ActiveX object is declared with an HTML "<OBJECT>" tag. Inside this object tag, the HTML code
indicates the Class ID of the ActiveX, so that the browser can refer back to Windows for the ActiveX control, so that it can be
displayed.
We need to refer back to this file later, so save the HTML file onto the Windows desktop:
11.4.2 Windows Registration 11.4.2.1 The Registry Since we have developed an ActiveX, client applications will refer to the Class ID in order for Windows to display the correct object. Windows therefore needs to have some kind of 'look-up' that cross-references the Class ID with the object. This is done inside the Windows Registry. The Registry is a file within Windows that is used to store settings and configuration data for various applications that are installed. Each time you install an ActiveX (or other objects e.g. Dynamic Link Library), an entry is made in the Registry stating the ActiveX's filename (".ocx"), path and corresponding Class ID number.
Exercise: Make sure that you have Made the ActiveX in "Exercise xviii" before starting this exercise.
Note:
The Registry structure differs between different versions of Windows, so some of the values seen below may differ slightly. These
values were taken from MicrosoftT Windows NT4.0 Workstation.
11.4.2.2 Registering Controls and Libraries One obvious problem with the system above, is that if the ActiveX (or other object) is distributed to other computers, they will not have any reference to the Class ID in their Registries. There are two ways that this can be done. When the application is being installed. Rather than simply copying the ".ocx" file across to the other computer, an installation "Setup" is generated. This "Setup" program performs all of the copying and then Registers all of the controls and libraries on the computer. If you have ever installed any large applications, you may have noticed that it says like "Setup is now updating your system" towards the end of the installation; this usually means that the application is Registrering all of it's controls and libraries. Windows contains a program called "Registry Server 32" that can Register your controls or libraries for you. The command line syntax for this program is as follows:
A control or library can also be Unregistered with Registry Server 32 as follows:
Exercise: Make sure that you have Made the ActiveX in "Exercise xviii" before starting this exercise. This is an easier way to register controls and libraries:
11.4.3 Binary Compatibility As we learned earlier, each time an ActiveX is Made, it is given a unique Class ID. When we are developing large applications that use ActiveXs, we may choose to develop the ActiveXs separately. This gives us a problem, because each time we modify the ActiveX and Make it, the main project cannot find the reference to the version of the ActiveX that it was developed with, so generates an error. In order to stop this from happening, we can force Visual Basic to use the same Class ID each time it Makes the ActiveX. This is called Binary Compatibility, as each new version is compatible with all previous versions.
Exercise xix: Make sure that you have Made the ActiveX in "Exercise xviii" before starting this exercise.
Note:
Each time you Make the ActiveX, it will re-use the Class ID as specified in the "GetFile.ocx" file that you selected in the
"Project Properties" dialogue box.
Note:
Note that the Internet Browser can still find your ActiveX, even though you have changed it because the Class ID is still the same.
<<Page 10 : Top : Page 12>> | ||