Sunday, June 13, 2010

Simple example of how to expose .net controls to VB6

There's already a lot of discussions on iNet on this theme i'll just show you on simple
example how to expose your .Net Controls as COM to other development
environment (in our case VB6 as example).

Create your control library and make it com visible as in picture below

Because we do not have setup for now and for test we should register our library as COM
check the register for COM option to register it

Create some control and make it com visible also give it a ProgId to expose it to out world


Build the project and let test our control in VB6
Create new vb6 exe project . To add our control dynamically onto the form we will use
the VBControlExtender and code looks like that :


       Option Explicit
       Dim WithEvents objExt As VBControlExtender

       Private Sub Form_Load()
          'adding our control licens to licences collection
           Licenses.Add "InteropControlLib.DotNetControl", "ewrinvcmcoe"
           'adding the control to Form
            Set objExt = Me.Controls.Add("InteropControlLib.DotNetControl", "DotNetControl1", Me)
            objExt.Visible = True
       End Sub

Run the test project and wuala, you got .net control on vb6 form :


Those of you who found this article interesting and want to find more
check out links below :
 COM Interoperability in .NET Framework Applications
 COM Interop



2 comments: