How do I make a C# UserControl into an OCX file for VB6 use?

  • Thread starter Thread starter Dennis Burdett
  • Start date Start date
D

Dennis Burdett

I have a pretty complex control which use to be a C# windows form that I am
trying to make into a control that can be dragged onto a VB6 form.

I was able to make it into a com class that I could then go to VB6 and add
the reference and call the object there without any problems.

When I try to make it into a UserControl, I get it to build and it registers
itself during the build with no errors BUT I can not add the dll to the vb6
project. I can add the dll to another C# project and all the form
(usercontrol) works as it should with the grid and toolbar buttons doing
their thing as planned.

below is the top section of the class diff.

The contructor doesn't do anything but the InitializeComponent() call.

thanks for any assistance.
Dennis

using interop = System.Runtime.InteropServices;

namespace TASAMS

{

[interop.ProgId("TimeLineControl"),

interop.Guid("124B9B07-35A7-4c72-B04B-3A04E42F1E07"),

interop.ClassInterface(interop.ClassInterfaceType.AutoDispatch),

interop.ComVisible(true)]

public class TimeLineControl : System.Windows.Forms.UserControl

{

....

}
 
Hi Dennis,

You cannot turn a C# user control to an OCX - this possibility, as far as I
remember, was considered and partially implemented in early beta versions of
the Framework, but later Microsoft decided not to support it in the release
version. Still, you have a workaround (BTW it is widely used by developers
writing add-ins for VS .NET) - the so-called shim control. It is a free
small ActiveX control provided by Microsoft (not sure where to look for it
on the Microsoft website but I'm sure there are even several versions in the
"Files" section of this group: http://groups.yahoo.com/groups/vsnetaddin)

The ActiveX has a couple of public methods designed to tell the ActiveX
which class from which assembly to host inside it. So you drag the ActiveX
to a VB6 form and then invoke its HostUserControl method - and voila, you
have a .NET control in your VB6 app.
 
thanks Dmitriy,

I have been trying to figure out how to do this most of the day and can get
it to build (at least the shim part) and I am then able to add the new dll
to the VB6 project form BUT I guess I am just not understanding what or how
to make the changes my code to get it to actually add my UserControl to the
host shim dll so it will run my stuff within vb.

Dennis


Dmitriy Lapshin said:
Hi Dennis,

You cannot turn a C# user control to an OCX - this possibility, as far as I
remember, was considered and partially implemented in early beta versions of
the Framework, but later Microsoft decided not to support it in the release
version. Still, you have a workaround (BTW it is widely used by developers
writing add-ins for VS .NET) - the so-called shim control. It is a free
small ActiveX control provided by Microsoft (not sure where to look for it
on the Microsoft website but I'm sure there are even several versions in the
"Files" section of this group: http://groups.yahoo.com/groups/vsnetaddin)

The ActiveX has a couple of public methods designed to tell the ActiveX
which class from which assembly to host inside it. So you drag the ActiveX
to a VB6 form and then invoke its HostUserControl method - and voila, you
have a .NET control in your VB6 app.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Dennis Burdett said:
I have a pretty complex control which use to be a C# windows form that I am
trying to make into a control that can be dragged onto a VB6 form.

I was able to make it into a com class that I could then go to VB6 and add
the reference and call the object there without any problems.

When I try to make it into a UserControl, I get it to build and it
registers
itself during the build with no errors BUT I can not add the dll to the
vb6
project. I can add the dll to another C# project and all the form
(usercontrol) works as it should with the grid and toolbar buttons doing
their thing as planned.

below is the top section of the class diff.

The contructor doesn't do anything but the InitializeComponent() call.

thanks for any assistance.
Dennis

using interop = System.Runtime.InteropServices;

namespace TASAMS

{

[interop.ProgId("TimeLineControl"),

interop.Guid("124B9B07-35A7-4c72-B04B-3A04E42F1E07"),

interop.ClassInterface(interop.ClassInterfaceType.AutoDispatch),

interop.ComVisible(true)]

public class TimeLineControl : System.Windows.Forms.UserControl

{

....

}
 
Dennis,

As far as I remember, this should look like something like this:

Sub Form_Load
Dim objUserControl As Object
Set objUserControl =
objShimControl.HostUserControl("MyNamespace.MyControl", "MyAssembly,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null")
End Sub

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Dennis Burdett said:
thanks Dmitriy,

I have been trying to figure out how to do this most of the day and can
get
it to build (at least the shim part) and I am then able to add the new dll
to the VB6 project form BUT I guess I am just not understanding what or
how
to make the changes my code to get it to actually add my UserControl to
the
host shim dll so it will run my stuff within vb.

Dennis


Dmitriy Lapshin said:
Hi Dennis,

You cannot turn a C# user control to an OCX - this possibility, as far as I
remember, was considered and partially implemented in early beta versions of
the Framework, but later Microsoft decided not to support it in the release
version. Still, you have a workaround (BTW it is widely used by
developers
writing add-ins for VS .NET) - the so-called shim control. It is a free
small ActiveX control provided by Microsoft (not sure where to look for
it
on the Microsoft website but I'm sure there are even several versions in the
"Files" section of this group: http://groups.yahoo.com/groups/vsnetaddin)

The ActiveX has a couple of public methods designed to tell the ActiveX
which class from which assembly to host inside it. So you drag the
ActiveX
to a VB6 form and then invoke its HostUserControl method - and voila, you
have a .NET control in your VB6 app.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Dennis Burdett said:
I have a pretty complex control which use to be a C# windows form that I am
trying to make into a control that can be dragged onto a VB6 form.

I was able to make it into a com class that I could then go to VB6 and add
the reference and call the object there without any problems.

When I try to make it into a UserControl, I get it to build and it
registers
itself during the build with no errors BUT I can not add the dll to the
vb6
project. I can add the dll to another C# project and all the form
(usercontrol) works as it should with the grid and toolbar buttons
doing
their thing as planned.

below is the top section of the class diff.

The contructor doesn't do anything but the InitializeComponent() call.

thanks for any assistance.
Dennis

using interop = System.Runtime.InteropServices;

namespace TASAMS

{

[interop.ProgId("TimeLineControl"),

interop.Guid("124B9B07-35A7-4c72-B04B-3A04E42F1E07"),

interop.ClassInterface(interop.ClassInterfaceType.AutoDispatch),

interop.ComVisible(true)]

public class TimeLineControl : System.Windows.Forms.UserControl

{

....

}
 
Thanks Dmitriy



Dmitriy Lapshin said:
Dennis,

As far as I remember, this should look like something like this:

Sub Form_Load
Dim objUserControl As Object
Set objUserControl =
objShimControl.HostUserControl("MyNamespace.MyControl", "MyAssembly,
Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null")
End Sub

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Dennis Burdett said:
thanks Dmitriy,

I have been trying to figure out how to do this most of the day and can
get
it to build (at least the shim part) and I am then able to add the new dll
to the VB6 project form BUT I guess I am just not understanding what or
how
to make the changes my code to get it to actually add my UserControl to
the
host shim dll so it will run my stuff within vb.

Dennis


in message news:[email protected]...
Hi Dennis,

You cannot turn a C# user control to an OCX - this possibility, as far
as
I
remember, was considered and partially implemented in early beta
versions
of
the Framework, but later Microsoft decided not to support it in the release
version. Still, you have a workaround (BTW it is widely used by
developers
writing add-ins for VS .NET) - the so-called shim control. It is a free
small ActiveX control provided by Microsoft (not sure where to look for
it
on the Microsoft website but I'm sure there are even several versions
in
the
"Files" section of this group: http://groups.yahoo.com/groups/vsnetaddin)

The ActiveX has a couple of public methods designed to tell the ActiveX
which class from which assembly to host inside it. So you drag the
ActiveX
to a VB6 form and then invoke its HostUserControl method - and voila, you
have a .NET control in your VB6 app.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

I have a pretty complex control which use to be a C# windows form that
I
am
trying to make into a control that can be dragged onto a VB6 form.

I was able to make it into a com class that I could then go to VB6
and
add
the reference and call the object there without any problems.

When I try to make it into a UserControl, I get it to build and it
registers
itself during the build with no errors BUT I can not add the dll to the
vb6
project. I can add the dll to another C# project and all the form
(usercontrol) works as it should with the grid and toolbar buttons
doing
their thing as planned.

below is the top section of the class diff.

The contructor doesn't do anything but the InitializeComponent() call.

thanks for any assistance.
Dennis

using interop = System.Runtime.InteropServices;

namespace TASAMS

{

[interop.ProgId("TimeLineControl"),

interop.Guid("124B9B07-35A7-4c72-B04B-3A04E42F1E07"),

interop.ClassInterface(interop.ClassInterfaceType.AutoDispatch),

interop.ComVisible(true)]

public class TimeLineControl : System.Windows.Forms.UserControl

{

....

}
 
hi,

I have the similar task as mentioned in this thread.
I am new to the VB so dont know much about that. Can anyone elborate the process of adding Toolitem(developed in C#) in VB6 IDE environment.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top