none
How to generate an OCX activex control using VB.NET

    Question

  •  

    My project which will generate an OCX control is writen by VB6.0 and I upgrade it to VB.NET using VBUpgrade.exe, but I found that the project type in VB.NET is class library and the file it generates is a DLL (not an OCX as VB6).

    I want to know how to generate the OCX control as VB6 in VB.NET, since I haven't found any place to set this in the project properties in VB.NET.

    Any help will be appreciate!!

     

    Thanks

    Carlos

    Wednesday, November 07, 2007 10:35 AM

Answers

  • I don't remember how it is in earli version of VB.NET but in VS 2008 you can use

     

    class library to create controls but you have inherit propertis from other controls

    exp:

    public class MyPanel

       inherits Windows.Forms.Panel

     

    or you can create Windows Control Library project to create ActiveX control

    Thursday, November 08, 2007 4:51 PM
  • Yes, TheCrash, you are right. the project should inherit from UserControl like this:

    Code Block

    Public Class MyUserCtl

    Inherits System.Windows.Forms.UserControl

     

     

    "register for COM interop" should be checked under "compile" tag in project properties, and I should add a module to implements the register/Unregister control method. In the source code of MyUserCtl, add these codes

     

    Code Block

    <ComRegisterFunctionAttribute()> Public Shared Sub _

    Register(ByVal t As Type)

    ModuleRegistration.RegisterControl(t)

    End Sub

    <ComUnregisterFunctionAttribute()> Public Shared Sub _

    UnregisterFunction(ByVal t As Type)

    ModuleRegistration.UnregisterControl(t)

    End Sub

     

     

    And here is the detail info about this:

    http://blogs.msdn.com/calvin_hsia/archive/2006/07/14/665830.aspx

     

    But the problem is the file extension is still .dll (not .ocx), but it maybe doesn't matter

    Friday, November 09, 2007 12:45 AM
  • Hi-

     

    There are many good suggestions above.  It would help to understand why you want an OCX.  Is this because you want to host the control in a VB6 app or a COM-based host? 

     

    If you do want to host in VB6 then you need to make the control com callable and follow a number of other conventions.  The easiest way to do this is using the Interop Forms Toolkit 2.0.  Basically you have tools and guidance to make any .NET user control behave like an ActiveX OCX.  Plus you can expose properties, methods, and events.  CalvinH worked on this toolkit so the guidance is consistent -- just a little easier. 

     

    Interop Forms Toolkit 2.0:

    http://msdn2.microsoft.com/en-us/bb419144.aspx

     

    The other nice thing about the user control approach is you can use it in any .NET project as well.  Just drag and drop it from the toolbox (there's no need to use the OCX here).

     

    Let us know if this solves your problem.

     

    Best,

    Paul

     

    Friday, November 09, 2007 9:10 AM
    Moderator

All replies

  • in VB.NET you can use external OCX controls but when you upgrade code from VB6 to VB.NET OCX controls will be change to ActiveX control type and compiler generate dll file.

    Wednesday, November 07, 2007 2:32 PM
  •  

     

     TheCrash wrote:

    in VB.NET you can use external OCX controls but when you upgrade code from VB6 to VB.NET OCX controls will be change to ActiveX control type and compiler generate dll file.

     

    Thanks for your reply.

    The VB6 project itself will generate an OCX control (not using other OCX controls), so I want to make the VB.NET project which is upgraded from VB6 generates an OCX file as VB6 does, but I didn't know how to do.

    Maybe VB.NET does not support OCX? Since the "application type" of VB.NET project is as following:

    windows application

    class library

    console application

    windows service

    web control library

     

    The type it uses now is class library.

    thanks

    Thursday, November 08, 2007 12:53 AM
  • I don't remember how it is in earli version of VB.NET but in VS 2008 you can use

     

    class library to create controls but you have inherit propertis from other controls

    exp:

    public class MyPanel

       inherits Windows.Forms.Panel

     

    or you can create Windows Control Library project to create ActiveX control

    Thursday, November 08, 2007 4:51 PM
  • Yes, TheCrash, you are right. the project should inherit from UserControl like this:

    Code Block

    Public Class MyUserCtl

    Inherits System.Windows.Forms.UserControl

     

     

    "register for COM interop" should be checked under "compile" tag in project properties, and I should add a module to implements the register/Unregister control method. In the source code of MyUserCtl, add these codes

     

    Code Block

    <ComRegisterFunctionAttribute()> Public Shared Sub _

    Register(ByVal t As Type)

    ModuleRegistration.RegisterControl(t)

    End Sub

    <ComUnregisterFunctionAttribute()> Public Shared Sub _

    UnregisterFunction(ByVal t As Type)

    ModuleRegistration.UnregisterControl(t)

    End Sub

     

     

    And here is the detail info about this:

    http://blogs.msdn.com/calvin_hsia/archive/2006/07/14/665830.aspx

     

    But the problem is the file extension is still .dll (not .ocx), but it maybe doesn't matter

    Friday, November 09, 2007 12:45 AM
  • Hi-

     

    There are many good suggestions above.  It would help to understand why you want an OCX.  Is this because you want to host the control in a VB6 app or a COM-based host? 

     

    If you do want to host in VB6 then you need to make the control com callable and follow a number of other conventions.  The easiest way to do this is using the Interop Forms Toolkit 2.0.  Basically you have tools and guidance to make any .NET user control behave like an ActiveX OCX.  Plus you can expose properties, methods, and events.  CalvinH worked on this toolkit so the guidance is consistent -- just a little easier. 

     

    Interop Forms Toolkit 2.0:

    http://msdn2.microsoft.com/en-us/bb419144.aspx

     

    The other nice thing about the user control approach is you can use it in any .NET project as well.  Just drag and drop it from the toolbox (there's no need to use the OCX here).

     

    Let us know if this solves your problem.

     

    Best,

    Paul

     

    Friday, November 09, 2007 9:10 AM
    Moderator
  • Thanks for your reply!

    I want to use the control in VB.NET as you said, and I will have a try on interop forms toolkit later

    Thank you very much! both TheCrash and Paul!

    Friday, November 09, 2007 9:16 AM