Add controls to a Windows form.
Instantiate and invoke an ActiveX control.
With their roots in the Visual Basic custom control standard, ActiveX controls have become a major means of delivering encapsulated functionality to Windows applications. The key advance underlying ActiveX controls is that they have a standard set of interfaces through which they communicate with the hosting form. By supporting these interfaces, any application can make use of any ActiveX control, without any knowledge of the internal workings of that control.
This concept has turned out to be so popular that thousands of ActiveX controls are now available commercially. You can find controls to display unusual graphs, controls that implement common Internet protocols, controls that emulate spreadsheets, and many more.
But in the .NET world, an ActiveX control is useless. .NET Framework Windows forms can only contain instances of classes that are derived from the System.Windows.Forms.Control class. ActiveX controls, which are built using previous technologies, do not derive from this class. So how can you possibly use an ActiveX control on a Windows form?
The answer lies in the creation of a wrapper. In programming terms, a wrapper is a layer of software whose job is to translate one set of interfaces into another. The System.Windows.Forms namespace contains a wrapper class, AxHost, whose job is to make ActiveX controls available to Windows forms. To a Windows form, this class appears to be a regular Windows forms control. To an ActiveX control, this class appears to be an ActiveX control container. When the form sends a message to the control, or vice versa, the wrapper class translates the message so that the recipient component can understand it.
The AxHost class needs to be customized to work with a particular ActiveX control. That customization is the job of the Windows Forms ActiveX Control Importer, a utility that ships with the .NET Framework.
To illustrate the Windows Forms ActiveX Control Importer (aximp.exe), Step by Step 9.1 shows you how to use the SysInfo control, which ships as a part of Visual Basic 6, on a Windows form.
STEP BY STEP9.1 Using the Windows Forms ActiveX Control Importer Tool
|
Although the Windows Forms ActiveX Control Importer does the work of building the necessary wrapper classes for you, you still have to do a lot of work to use those classes. Because the importer does not add the control to the toolbox in Visual Studio .NET, you need to write all the code to initialize the code yourself. Getting this code right can be tricky. Fortunately, there's an easier way to bring an ActiveX control into your .NET Windows application project. You'll learn about that technique in the next section.
If you're using Visual Studio .NET to build Visual C# .NET applications (and I assume that you are), you don't have to bother with the Windows Forms ActiveX Control Importer. Instead, you can use the toolbox to add any ActiveX control from your system to the .NET environment. As you'll see, this method takes care of most of the work for you.
STEP BY STEP9.2 Using the Toolbox to Add an ActiveX Control to a Windows Form
|
As you've probably guessed by now, using ActiveX controls on Windows forms is easy. Just import the ActiveX control to the .NET Framework (either by running the Windows Forms ActiveX Control Importer or by adding the control to the toolbox), and you can treat it as if it were a native .NET control. But there are a few things you should consider before you use ActiveX controls in your .NET applications.
First and foremost, you should recognize that there's an inevitable performance decrease when you use ActiveX controls on a .NET form. Although the wrapper architecture allows you to seamlessly use an ActiveX control, it imposes a performance penalty. That's because every call to the control is actually a call to the wrapper class, which then must call the control itself after suitably transforming the parameters of the call. Thus your code has to do twice as much work to interact with an ActiveX control as with a native .NET control. If you're using only a few ActiveX controls in a limited number of forms, this performance decrease might not be noticeable, but if you overuse ActiveX controls, it adds up.
When you import some ActiveX controls to the .NET Framework, their names may change to avoid conflicts with existing objects. You're most likely to see this happen if a control has a property named State; such a property is named CtlState after the import.
The code that runs under the services provided by the Common Language Runtime (CLR) is called managed code. Because ActiveX controls are not managed code, they don't get any of the protection that the CLR brings to .NET applications. An ActiveX control is free to access memory that doesn't belong to it or indulge in other buggy behavior that could crash your entire application.
Finally, using ActiveX controls makes it more difficult to deploy .NET applications. In addition to installing the .NET Framework and your own application, you need to make sure that the target machine has a copy of the ActiveX control properly installed and registered.
Because of these drawbacks, you should use ActiveX controls sparingly (if at all). Before importing an ActiveX control into a project, you should consider whether a native .NET control can fill your requirements.
3.16.66.156