Adding a ActiveX Control to a Project in Visual Studio .NET

This document walks through the process of embedding and using an ActiveX control within a C# Visual Studio .NET project. Before starting, the ActiveX control must already by registered on the computer. For this walkthrough, the Animated Gif control, called MVSGif.ocx, will be used as an example.

Step 1 - Add ActiveX Control to Toolbar

Before embedding an ActiveX control into a project, it must first be put into the toolbar. This is done by right-clicking the design toolbar and selecting "Add/Remove Items".

In the following dialog that pops up, Microsoft-specific .NET controls are listed in the immediate window. To select ActiveX controls, select the "COM Components" tab. For this example, the Animated Gif control will be selected from the ones possible.

At this point, there will be a new button added to the end of the design toolbar; specifically, the one that was just added, or in our case, the Animated Gif Control. Click on the button to select it, and then either drag or click within your C# project to embed the control. Most controls are visible when they are added to a project. Don't be shocked when nothing is immediately visible after adding Animated Gif. It is the inherent nature of the control to be invisible until an animated gif is loaded into it. In the meantime, the location of the control may be discovered by noticing the border that surrounds it.

Step 2 - Change or Set Properties

After adding the control, properties for it may be changed or set within the properties view of the project. It is usually a good idea to change the name of the control to something easily recognizable, rather than the default name Microsoft gives a new control.

Step 3 - Call Functions or Handle Events

Now that the control is added, its functions may be called or events handled in any way seen fit. For example, to tell the control to "play", the following line of code would be added to the project, somewhere in the code (perhaps in response to clicking a button).

gifControl.Play();

In addition to directly invoking the ActiveX control's methods, one may also cause it to respond to events. To view a list of possible events that the control can respond to, click on the Events button within the Properties view (the button denoted by a lightning bolt).

While in the Events view, one may choose any event to handle by selecting it and pressing the Enter key. After doing so, Visual Studio automatically generates headings for a new function that will handle the event. In this example, when the user clicks the left mouse button on the control, the ActiveX control will "Stop" playing the animated gif.

 

Moon Valley Software