This topic demonstrates the steps involved in creating an Interop UserControl in Visual Studio .NET and then using it in a Visual Basic 6.0 project.

This topic requires the Interop Toolkit 2.1, Visual Basic 6.0 and a full version of Visual Studio .NET (Note: Visual Studio Add Ins are not supported in Visual C# Express Edition).

The first step is to create a project for the Interop UserControl in Visual Studio .NET.

 To create a project in Visual Studio .NET (2010)

  1. Launch Visual Studio as an administrator (the title bar will read "Microsoft Visual Studio (Administrator)").

  2. On the File menu, click New Project.
  3. In the New Project dialog box, on the Templates pane under Installed Templates select Visual C# > Windows and then click VB6 Interop UserControl.

  4. In the Name box, type NamesControl and then click OK.

    A new VB6 Interop UserControl template is added to the project, and the UserControl code opens.

  5. On the File menu, click Save All.

The next step is to design the appearance of the Interop UserControl by adding controls. For this example, you will add a Label control to display a full name, and three TextBox controls for entering first, last, and middle names.

To add controls to the Interop UserControl

  1. From the Toolbox, drag a Label control onto the designer.

  2. In the Properties window, change the Name property to FullName.

  3. From the Toolbox, drag three TextBox controls onto the designer. You can arrange them however you like.

  4. In the Properties window, change their Name properties to FirstName, MiddleName, and LastName.

  5. On the File menu, choose Save All to save your work.

Like any other UserControl, the Interop UserControl has private properties and methods that define its behavior, and public properties that are exposed when the control is used in Visual Basic 6.0. The next step is to add a private method that will update the FullName label with the contents of the FirstName, MiddleName, and LastName boxes as you type, using the TextChanged event handler.

To add private code to the control

  1. In the Designer, double-click the FirstName TextBox. Visual Studio will generate a stub for the FirstName_TextChanged event and display it in the Code Editor.
  2. In the Code Editor, add the following code for the FirstName_TextChanged event handler at the bottom of the class.

                private void FirstName_TextChanged(object sender, System.EventArgs e)
                {
                    // Display the contents of the three textboxes in the label.
     
                    FullName.Text = FirstName.Text + ' ' + MiddleName.Text + ' ' + LastName.Text;
                }
  3. On the View menu, click Designer.

  4. Select the MiddleName TextBox.

  5. In the Properties dialog, click the Events button.

  6. In the TextChanged drop down list, select FirstName_TextChanged.

  7. Repeat steps 4-6 for the LastName TextBox.

Press F5 to run the program. The UserControl TestContainer opens and displays your UserControl.

Enter your first, middle and last names in the three text boxes—as you type, your name is displayed in the FullName label.

The next step is to add code that will expose the FirstName, MiddleName, and LastName values as public properties in Visual Basic 6.0.

To add public properties

  1. In the Code Editor, expand the Interfaces region, and add the following code to the _InteropUserControl interface.
    [DispId(7)]
    string FirstNameText { [DispId(7)] get; [DispId(7)] set; }
    [DispId(8)]
    string MiddleNameText { [DispId(8)] get; [DispId(8)] set; }
    [DispId(9)]
    string LastNameText { [DispId(9)] get; [DispId(9)] set; }
    [DispId(10)]
    string FullNameText { [DispId(10)] get; }
    
  2. In the Code Editor, add the following code to expose the FirstName, MiddleName, and LastName values as properties.

        public string FirstNameText
        {
            get
            {
                return FirstName.Text;
            }
 
            set
            {
                FirstName.Text = value;
            }
        }
 
        public string MiddleNameText
        {
            get
            {
                return MiddleName.Text;
            }
            
            set
            {
                MiddleName.Text = value;
            }
        }
 
        public string LastNameText
        {
            get
            {
                return LastName.Text;
            }
 
            set
            {
                LastName.Text = value;
            }
        }
          

Add the following code to expose the value of the FullName label as a read-only property.

public string FullNameText
{
    get
    {
        return FullName.Text;
    }
}    

Press F5 to run the program.

In the UserControl TestContainer, scroll to the bottom of the Properties grid and select the FirstNameText property. Enter your name, and then select the FullNameText property—the FirstName text box should display your name, and the FullNameText property should match.

At this point the Interop UserControl is complete and tested in .NET — the next step is to create a project in Visual Basic 6.0 to consume the control.

To create a project in Visual Basic 6.0

  1. On the File menu, click New Project.

  2. In the New Project dialog box, click Standard EXE and then click OK.

  3. On the File menu, click Save Project.

  4. In the Save File As dialog box, type InteropControlTest.frm and then click Save.

  5. In the Save Project As dialog box, type InteropControlTest.vbp and then click Save.

In the next step you will add the Interop UserControl to the Visual Basic 6.0 Toolbox.

To add the Interop UserControl to the toolbox

  1. On the Project menu, click Components.

  2. In the Components dialog box, select the Controls tab.

  3. Scroll down to the NamesControl and select it. Note that the Location is shown a "mscoree.dll" — this is the file that the .NET Framework uses to load Interop UserControls.

  4. Check the box next to NamesControl and then click OK. The control should appear at the bottom of the Toolbox.

The control is now ready to use as you would any other control. In the final step you will add the control to the form, code a Validate event handler, and test the control.

To add the Interop UserControl and add Visual Basic 6.0 code

  1. In the Toolbox, select the InteropUserControl and drag it onto the form. Resize it so that the textboxes are visible. 

  2. Add a CommandButton control to the form.

  3. Double-click the InteropUserControl to open the Code Editor, and in the Events list select Validate.

  4. In the Validate event handler add the following code: 

    Private Sub InteropUserControl1_Validate(Cancel As Boolean)
        If InteropUserControl1.FirstNameText = "" Or _
        InteropUserControl1.MiddleNameText = "" Or _
        InteropUserControl1.LastNameText = "" Then
    
            MsgBox ("Please enter the complete name.")
            Cancel = True
    End If
    End Sub
  5. Press F5 to run the program. Enter your first and last name, leaving the middle name blank, and then press the Tab key. The message box should display.

 Note   If your Interop UserControl exposes custom events, there is an additional step that you must take to enable the events. For more information, see How To: Handle Interop UserControl Events.

Last edited Jan 8, 2012 at 5:56 AM by HolisticDeveloper, version 1

Comments

MichaelBA Oct 5, 2012 at 7:29 PM 
Following your steps, when I get to VB add component, VB6's open dialog box to choose the component is looking for *.OCX only. I pick the newly-created .DLL file anyways, and Windows complains that it is "not registerable as an active-X component". any idea why? I develop on a Win7 machine, but VB6 is on an XP machine.
I did not install the interop toolkits on the XP machine.
I put the newly-created .NET dll, and the Microsoft.InteropFormTools.dll into the same directory as the new VB6 application.

thanks,

Michael