Hosting COM Controls


As wonderful and varied as Windows Forms controls are, especially considering the burgeoning third-party Windows Forms control market, Component Object Model (COM) controls have been around a lot longer, and you may still need to use some of them in your Windows Forms applications.[11] Windows Forms has built-in support for hosting COM controls, and VS05 makes it easy to take advantage of that support.

[11] COM controls are also known as OLE controls and ActiveX controls.

The first step is to get your COM control of choice to show up on the Toolbox so that you can drag it onto your forms. To do that, right-click on the Toolbox and click Choose Items. This action brings up the Choose Toolbox Items dialog, as shown in Figure 10.29.

Figure 10.29. Choose Toolbox Items Dialog


All the items under the COM Components tab are COM controls registered on your machine. Checking any of them and pressing OK adds the control to the Toolbox, as shown in Figure 10.30.

Figure 10.30. COM Component Added to the Toolbox


After a COM control has been added to the Toolbox, you can drop an instance onto a form, set the properties, and handle the events. Any COM control added to your Windows Forms project causes a pair of interop assemblies to be generated by VS05 and added to the project.[14] It's the code in these assemblies that you're referencing and that forwards your calls to the underlying COM control.[15]

[14] The aximp.exe command line tool generates COM control interop assemblies in the same way that VS05 does.

[15] For more information about COM interop, see Essential .NET, Volume I: The Common Language Runtime (Addison-Wesley, 2003), by Don Box, with Chris Sells.

Also, COM controls need COM initialized in a UI-friendly manner, so make sure that the STAThread attribute adorns your Main method:

// Program.cs  [STAThread]  static void Main() {    ...    Application.Run(new MainForm()); }


When you create a new Windows Forms application in VS05, this attribute is applied by default, so to hurt yourself you must actively remove it.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

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