Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-09
19
Medium Priority
?
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +2
19 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40540000
>>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
ID: 40540008
@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
ID: 40540035
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:npaun
ID: 40540094
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 1500 total points
ID: 40543846
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
ID: 40544329
@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
ID: 40546071
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
ID: 40546153
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
ID: 40546195
"MyRTBClassLibrary1.RichTextBox" should be ok. The class string should have the library, a dot, and the class name.
0
 

Author Comment

by:npaun
ID: 40546372
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
ID: 40546518
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
ID: 40546570
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
ID: 40546684
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
ID: 40546700
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
ID: 40546841
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
ID: 40546926
damn... I thought I was close... Please let me know if you maybe find a way to make it work...
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40588776
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
ID: 41781898
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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