Using VB.Net controls in VB6 trough Interop: are there somewhere alreadu done and compiled such controls?

I need to use some of VB.Net controls (RichTextBox in particular) in my VB6 application, and I understood it can be done by using Microsoft InteropForms Toolkit. I’ve tried it and concluded that, although very promising, at the moment I don’t have time nor knowledge do it properly by myself and to implement all the properties and events of a VB.Net control to its VB6 usable equivalent…

Hence the question is, is there someone already done it and put on the internet to be downloaded? Enabling VB6 users to use VB.Net control by simply adding already handled and compiled ocx, with (almost) all properties, events and power of the Net control?

This seems as a such a basic task with so many possible users, that someone should already has done it, but somehow I cannot find it on the internet… All I found are examples how to use Interop to create your own UserControls exposing Net controls to VB6. Well, nice, but for instance, does everybody in need to use the VB.Net Unicode enabled Textbox or RichTextBox has to do all this work again and again by them self?! There must be someone somewhere already done it once and let others simply use it… Where?
npaunAsked:
Who is Participating?
 
louisfrCommented:
Create a COMVisible class in .NET
Public Class RichTextBox
    Inherits System.Windows.Forms.RichTextBox
End Class

Open in new window

Check the "COM Interop" checkbox in the properties of the project.

You can then use it in VB6:
Dim ext As VBControlExtender

Private Sub Form_Load()
    Dim rtb As RichTextBox.RichTextBox
    
    Set ext = Controls.Add("RichTextBox.RichTextBox", "ext", Me)
    ext.Top = 15
    ext.Left = 15
    ext.Width = Width - 30
    ext.Height = Height - 30
    ext.Visible = True
    
    Set rtb = ext.object
    rtb.Text = ChrW$(&H1D1A)
End Sub

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>I need to use some of VB.Net controls (RichTextBox in particular) in my VB6 application

I thought the RichTextBox control existed in VB 6 - no need to go to this effort.
0
 
npaunAuthor Commented:
@AndyAinscow
It does, but I need Unicode support, which VB6 RTB does not have... hence, the VB.Net need. But, the question holds for VB.Net controls in general...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
AndyAinscowFreelance programmer / ConsultantCommented:
What does the RTB stand for?
(VB6 does support UNICODE, you might want to look at: http://www.example-code.com/vb/vbUnicode1.asp).
0
 
npaunAuthor Commented:
RTB=RichTextBox.
VB6 uses Unicode strings, but VB6 control does not support Unicode. At least not natively, and without tricks such as switching code pages, taking care about languages and keyboard layouts... a simple no hassle VB.Net type Unicode support is needed...
0
 
npaunAuthor Commented:
@louisfr
Thank you for your comment.

a) Which type of .Net project I should use to create the COMVisible class?
b) What I should reference in VB6 in order to be able to access the interoped RichTextBox?

c) for a) I tried ClassLibrary template... the line Inherits System.Windows.Forms.RichTextBox provokes error "Type 'System.Windows.Forms.RichTextBox' is not defined", and if I compile the project without that line (with a blank class),  VB6 can't add reference to the ClassLibrary1.dll. I have no experience in VB.Net so I'm probably doing something very wrong....
0
 
louisfrCommented:
a) A Class library project.
b) After compiling the project with the "COM interop" checkbox checked, the assembly should appear in the references window in VB6.
c) To reference the RichTextBox class, you must add a reference to the System.Windows.Forms assembly. It's not added by default in a class library project.
Are you more familiar with C# ?
0
 
npaunAuthor Commented:
Thanks. OK, added the reference to a Class Library net project named MyRTBClassLibrary1, compiled, and added the VB6 reference to the MyRTBClassLibrary1.

In the MyRTBClassLibrary1 class library project, I have code exactly as you said:
Public Class RichTextBox
    Inherits System.Windows.Forms.RichTextBox
End Class

a) When strating VB code, at line "rtb As RichTextBox.RichTextBox" I got error "Type mismatch" error.
b) I replaced that line with       Dim rtb As MyRTBClassLibrary1 .RichTextBox and VB6 passes that line
c) Now, at line Set ext = Controls.Add("RichTextBox.RichTextBox", "ext", Me) I get error "Run-time error '711': Invalid class string. Looking for object with ProgID: RichTextBox.RichTextBox"
d) I tried to replace the line with
      Set ext = Controls.Add("MyRTBClassLibrary1.RichTextBox", "ext", Me)

and also (in desperation) with
       Set ext = Controls.Add("MyRTBClassLibrary1.MyRTBClassLibrary1", "ext", Me)
       Set ext = Controls.Add("RichTextBox.MyRTBClassLibrary1", "ext", Me)
but I still get the same error...

 I've checked in the VB6 Object Browser, and there is RichTextBox class inside the MyRTBClassLibrary1 library.

What should I do?
0
 
louisfrCommented:
"MyRTBClassLibrary1.RichTextBox" should be ok. The class string should have the library, a dot, and the class name.
0
 
npaunAuthor Commented:
ok. I still get the error. Do you know why?

If you wish/can check it your self for the cause/solution, I uploaded source code of my Net/VB6 test projects here: http://www.hydramouse.com/MyRTBClassLibrary1.zip
(here, as I had troubles with EE file extension checking when tried to upload on EE directly...)

Ths.
0
 
louisfrCommented:
Found it. The class string should be the fully qualified class name with its namespace. In your MyRTBClassLibrary1, you use the ClassLibrary1 namespace.
"ClassLibrary1.RichTextBox" should work.
0
 
npaunAuthor Commented:
It works, finally :)

a) Is there maybe a way to change the "ClassLibrary1" into something more meaningful and specific (e.g. MyCustomNetRTBClass))? In my MyRTBClassLibrary1, there is "Class1" class of which is the RichTextBox is part of, so I don't see where is the "ClassLibrary1" and what could I rename to achieve this, if possible. Not extremely necessarily, but it would be convenient...

b) Is there a way to have the properties and methods of the Net RichTextBox exposed and visible in VB6? In the maner usual to Vb6, e.g. when typing "rtb." you would get a list of avialable properties/methods. At the moment, rtb works kind a late bound object, which I suppose it is... I suppose it can be don with some encapsulation there in Net class libray, but I was hopping that exposing RichtextBox to Vb6 would also autocratically expose its properties/events to VB6 directly...

c) Does this approach you suggested use the Interop at all? In order to know, when deploying the VB6 app, should I need to worry about Microsoft InteropForms Toolkit presence and installation, or just about Net Framework...
0
 
louisfrCommented:
a) You can change the namespace in the properties of the project > Application > Root namespace

b) You need to change the interface exposed by the class using the ClassInterface attribute. Using AutoDual, an interface is generated automatically to expose all public methods and properties.
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class RichTextBox
    Inherits System.Windows.Forms.RichTextBox
End Class

Open in new window

My usual approach is to use ClassInterfaceType.None, and provide a custom interface to expose only what I intend to expose. Here is an example: http://stackoverflow.com/a/2733880

c) I never used that Toolkit. That approach is COM interop which is included in the Framework.
0
 
npaunAuthor Commented:
ok, thanks I'll try.
Meanwhile, I've noticed a strange thing: when I select text on the created RTB in VB6 form, and try to access it, for instance as
Debug.Print rtb.SelectionStart, rtb.SelectionLength
or simply by shifting focus to another control on form, the rtb goes completely gray without visible text, until the the rtbis double clicked... Tried with rtb.Focus but that does not help, actualy it also provokes such effect... and this completely impairs my use of this control... Do you know why  this happens and how to prevent? Ths.
0
 
louisfrCommented:
I didn't find any workaround.

We're in the process of updating a VB6 project to .NET but we replace whole forms not just parts of them, so we didn't face that problem.
0
 
npaunAuthor Commented:
damn... I thought I was close... Please let me know if you maybe find a way to make it work...
0
 
Martin LissOlder than dirtCommented:
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
Jon BrickerCommented:
Haven't found the answer yet.  Having the same issue with these interop controls.  
/////
ok, thanks I'll try.
Meanwhile, I've noticed a strange thing: when I select text on the created RTB in VB6 form, and try to access it, for instance as
Debug.Print rtb.SelectionStart, rtb.SelectionLength
or simply by shifting focus to another control on form, the rtb goes completely gray without visible text, until the the rtbis double clicked... Tried with rtb.Focus but that does not help, actualy it also provokes such effect... and this completely impairs my use of this control... Do you know why  this happens and how to prevent? Ths.
//////
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.