Solved

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

Posted on 2015-01-09
19
446 Views
Last Modified: 2016-09-02
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?
0
Comment
Question by:npaun
  • 8
  • 6
  • 2
  • +2
19 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>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
 

Author Comment

by:npaun
Comment Utility
@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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
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
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
@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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
"MyRTBClassLibrary1.RichTextBox" should be ok. The class string should have the library, a dot, and the class name.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:npaun
Comment Utility
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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
 

Author Comment

by:npaun
Comment Utility
damn... I thought I was close... Please let me know if you maybe find a way to make it work...
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 

Expert Comment

by:Jon Bricker
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now