Link to home
Start Free TrialLog in
Avatar of npaun
npaun

asked on

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?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>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.
Avatar of npaun
npaun

ASKER

@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...
What does the RTB stand for?
(VB6 does support UNICODE, you might want to look at: http://www.example-code.com/vb/vbUnicode1.asp).
Avatar of npaun

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of npaun

ASKER

@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....
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# ?
Avatar of npaun

ASKER

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?
"MyRTBClassLibrary1.RichTextBox" should be ok. The class string should have the library, a dot, and the class name.
Avatar of npaun

ASKER

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.
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.
Avatar of npaun

ASKER

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...
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.
Avatar of npaun

ASKER

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.
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.
Avatar of npaun

ASKER

damn... I thought I was close... Please let me know if you maybe find a way to make it work...
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".
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.
//////