Microsoft KB Archive/814361

= Visual Basic .NET or Visual Basic 2005 does not support adding ActiveX controls and licenses at run time =

Article ID: 814361

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
Your Microsoft Visual Basic 6.0 Standard EXE project adds a control, and then adds a license to the control at run time by using the Licenses.Add method. When you upgrade the project to Visual Basic .NET or Visual Basic 2005, you may receive the following errors:

Name 'Licenses' is not declared.

Value of type 'String' cannot be converted to 'System.Windows.Forms.Control'.

Too many arguments to 'Public Overridable Overloads Sub Add(value As System.Windows.Forms.Control)'.



CAUSE
Visual Basic 6.0 uses the Licenses.Add method to add licenses to the controls at run time. Visual Basic .NET or Visual Basic 2005 does not support adding licenses and ActiveX controls at run time that are not added to the project at design time. Therefore, you receive errors when you upgrade the project to Visual Basic .NET or Visual Basic 2005.



WORKAROUND
To work around this problem, add the licensed ActiveX control to a Windows form. This creates an OcxState object that contains the license key and wrapper classes for the ActiveX controls. Use the generated OcxState with any controls that you create at run time. To do this, follow these steps:  In Visual Basic 6.0, start a new ActiveX Control project.

By default, UserControl1 is created. On the Properties window, locate BackColor, and then click to select Highlight from the list. On the Project menu, click Project1Properties. On Project1Properties, click to select the Require License Key option. On the File menu, click Make Project1.ocx. In Visual Studio .NET, start a new Windows application by using Visual Basic.

By default, Form1 is created.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 Click Customize Toolbox on the Tools menu.

Note If you are using Visual Studio .NET (2003), click Add/Remove Toolbox Items on the Tools menu.

Note If you are using Visual Studio 2005, click Choose Toolbox Items on the Tools menu.</li> In Customize Toolbox, click the COM Components tab, and then click Browse.</li> Locate the Project1.ocx ActiveX control file, and then click Open.</li> From the toolbox, drag UserControl1 to Form1.</li> On the Project menu, click Add Windows Form.</li> From the toolbox, drag a Button control to Form2.</li> On the View menu, click Code.</li>  Add the following code as the first line of the code: Imports System.Resources </li>  Add the following code to the Button1_Click event handler: Dim resources As ResourceManager = New ResourceManager(GetType(Form1)) 'create the instance of the ActiveX control Dim AxLicCtl1 As New AxProject1.AxUserControl1 'use the OcxState generated AxLicCtl1.OcxState = resources.GetObject(&quot;AxLicCtl1.OcxState&quot;) 'add the control to the form at run time Me.Controls.Add(AxLicCtl1) </li> In Solution Explorer, right-click WindowsApplication1, and then click Properties.</li> Under Startup object, click to select Form2.</li> On the Debug menu, click Start.</li> Click Button1 to add the ActiveX control.</li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> In Visual Basic 6.0, start a new Standard EXE project.

By default, Form1 is created.</li> On the toolbox, double-click the CommandButton control.</li>  Add the following code to the Click event of Command1: Dim c As Control 'add the license to the control at runtime Licenses.Add &quot;MSComctlLib.Slider&quot;, &quot;546-2137-wdfj-dhf&quot; 'add the control to the form Set c = Me.Controls.Add(&quot;MSComctlLib.Slider&quot;, &quot;MyControl&quot;) c.Visible = True </li> <li>Save the project.</li> <li>Open Visual Studio .NET or Visual Studio 2005.</li> <li>On the File menu, point to Open, and then click Project.</li> <li>Locate the  file in the Visual Basic project, and then click Open.</li> <li>In Visual Basic Upgrade Wizard, click Next three times. In Warning, click Yes.</li> <li>Click Next to upgrade the project.

You receive the errors mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">