ActiveX Controls

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 12.  Advanced Windows Forms


Microsoft's .NET Framework introduced the use of Windows Forms, which represents a consistent windowing framework across all .NET languages. The Windows Forms namespace includes a rich collection of controls that are available to use in your applications. As we saw in Chapters 7 and 8, these include textboxes, calendars, up/down controls, error providers, and more. However, occasionally you need a control that isn't found in the Windows Forms namespace. If so, you have three choices:

  • Build the control you need as a .NET user control.

  • Purchase the .NET control you need from a third party.

  • Use an existing ActiveX control that contains the functionality you need.

This section focuses on using ActiveX controls. ActiveX controls were the preferred way of providing custom control capabilities to applications before .NET was introduced. It defines its properties, methods , and events via a type library and is based upon the COM (Component Object Model) standard.

Although Windows Forms applications are optimized to work with Windows Forms controls, you can also use ActiveX controls. However, the following issues must be taken into consideration:

  • Applications that use Windows Forms can run in a fully trusted environment; when an ActiveX control is used, the application requires unmanaged code permission (see Chapter 17).

  • ActiveX controls must be deployed with the application.

  • Registering an ActiveX control requires writing to the registry.

In this section, we will use Microsoft's Animation ActiveX control to play .avi files. Our example is found in ActiveXControls .

Adding ActiveX Controls to the Toolbox

graphics/codeexample.gif

Before an ActiveX controls can be used on any form, it must be added to the toolbox. If you right-click on the Windows Forms tabs of the toolbox, you can choose Customize Toolbox (see Figure 12-13).

Figure 12-13. Customizing the toolbox.

graphics/12fig13.jpg

The Customize Toolbox dialog displays a list of all COM controls that are registered on your machine. To add an ActiveX control to your project, check the checkbox next to the control and choose OK. In this example, we chose the Microsoft Animation 6.0 control (see Figure 12-14).

Figure 12-14. Adding an ActiveX control to the toolbox.

graphics/12fig14.jpg

When we include an ActiveX control in a .NET application, it must use the Primary Interop Assembly (PIA). This wrapper class, which is added automatically, maps calls from managed code to the underlying unmanaged COM class.

Figure 12-15 shows that the animation control has been added to the toolbox and has been dropped on the form. The Solutions Explorer now references the DLL that contains the ActiveX control.

Figure 12-15. Using an ActiveX control.

graphics/12fig15.jpg

Using an ActiveX Control without Using Visual Studio

When you added the ActiveX control to your Windows Forms application by using the Visual Studio Customize Toolbar dialog, the ActiveX Control Importer converted the type definitions in the ActiveX control into a Windows Forms control. This wrapper control is derived from System.Windows.Forms.AxHost . The wrapper control appears as a Windows Forms control, but contains an instance of and communicates with the underlying ActiveX control.

If you are not working within Visual Studio, you must explicitly run the ActiveX Control Importer. It produces a set of assemblies that contain the appropriate metadata and wrapper control. The following command would also have generated MSComCtl2.dll and AxMSComCtl2.dll for the animation control:

 aximp c:\WINNT\System32\mscomct2.dll 

Programming with ActiveX Controls

Once the ActiveX control has been successfully added to your project, you may name it and begin working with it as you would any other control. In our ActiveXControls application, we have added a Play button. When it is pressed, the FileOpenDialog control is used to allow the user to select an .avi file. The animation control is then used to play the .avi file.

 Private Sub btnPlay_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnPlay.Click  Try   dlgOpen.Filter = "Animations*.avi"   Dim ans As DialogResult = dlgOpen.ShowDialog()  If ans = DialogResult.OK Then  axAnim.Open(dlgOpen.FileName)   axAnim.Play()   End If   Catch   MessageBox.Show("Error playing animation!", _   "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)   End Try   End Sub  

Our application is now functional. Figure 12-16 illustrates the use of the OpenFileDialog control to select the .avi file. Figure 12-17 illustrates the ActiveX control as it plays the .avi file.

Figure 12-16. Selecting the .avi file.

graphics/12fig16.jpg

Figure 12-17. Playing the animation.

graphics/12fig17.jpg


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net