Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms
Article

Embedding of VB6 form in .NET applications

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
12 Jul 20077 min read 173.5K   5.6K   51   35
This article explains how to open a VB6 MDI child form inside a .NET MDI form

Screenshot - VB6formsinNET_screenshot.jpg

Introduction

Typically, the available tools that provide interoperation between .NET code and VB6 code are based on proxies, wrappers or other forms of middleware that allow VB6 applications to call .NET Winforms. So far, we haven't seen a solution that allows us to do the opposite. That is, to embed VB6 forms in a .NET application. For that reason, we decided to create that solution on our own. At MasterSoft, we have many applications developed in Visual Basic 6 in which we invested several thousands of development hours. Right now we face the necessity of migrating them to .NET in a gradual way and with the least amount of effort.

To reach that goal, we envisioned a kind of interoperation between VB6 and .NET that would allow us to open VB6 forms in a .NET application. This would give us the opportunity to migrate the applications gradually, incorporating new functionality with "native" .NET code and maintaining intact – or as intact as possible – everything that's written in VB6. The main advantage of this strategy is in leveraging a .NET framework developed by ourselves, which gives us powerful tools for menu designing and security management, among many other things. All these benefits would be lost if we maintained the core of our applications in VB6.

The solution we applied is based on a .NET MDI environment from which we can open VB6 forms contained in a DLL, just as if they were MDI children of the .NET form. Instead of using toolkits or power packs for .NET-VB6 interop, we used instances of a .NET container form in which we embed the VB6 forms. By using API functions, the normal behavior of the embedded forms is simulated.

Implementation details

To use VB6 forms from within a .NET application, we opted to develop a "shell" (container) form in VB.NET with MDI child functionality. This creates an instance of the VB6 form through a specific class in the DLL ActiveX, which is ClsForms. Using API functions and communication with the DLL, the VB6 form is instanced and embedded in the container.

Within the VB6 DLL, there is a public class that does the job of instancing the forms and communicating with the .NET application. This class acts as a controller of the instantiated forms in the DLL. It provides access to them through several methods that help the container to behave as the VB6 form would in a VB6 MDI container. Due to the fact that the VB6 forms reside in a DLL ActiveX, we needed to make sure that their MDIChild property is set to False because we couldn't figure out how to change that property at runtime. In order to manage the events of the VB6 form, the .NET container uses a Timer control that constantly queries the VB6 class for messages to handle.

A big issue we needed to solve was the communication between VB6 forms. In many cases, our application forms call each other, passing parameters and results -- of searches, for instance -- between them through properties. In order to keep this behavior in the .NET application with the VB6 forms, it was necessary to give the VB6 forms the chance to create new instances of the container and embed in them other VB6 forms. This was done as explained below.

Showing the sequence

To make things a bit more encapsulated, we develop a FormController class in the .NET project which maintains the instance of the DLL and creates multiple instances of the .NET container. Thus, we can open multiple forms using the same instance of ClsForms.

VB
'We instantiate FormController by passing 
'the MDI form and the Dll ActiveX name
Dim objFormController As New FormController(objMDIForm, "FormsVB6")

'then we just call the OpenForm function passing the VB6 form name
objFormController.OpenForm("frmChildForm")

The best way to show the interaction between objects is with this UML sequence diagram:

Screenshot - VB6formsinNET_sequence.gif

The OpenForm function in the FormController creates an instance of the ClsForms class in the ActiveX DLL -- the one that contains all the VB6 forms -- and then instantiates the frmContainer form. Finally, it calls its OpenForm method. The OpenForm method in the container in turn calls the OpenForm method in ClsForms. It does this in order to create a new instance of the form's name, passed by argument using the hidden VB6 Forms.Add("formname") instruction.

When the form instance is created, the OpenForm method returns the handle of the instance to the container. With that handle and through API functions, as well as through other methods of the ClsForms class, the VB6 form is "drawn" inside the .NET container. The OpenForm container obtains the property values of the VB6 form such as Title, Width, Height, etc. in order to replicate them in the container, i.e. the GetWindowTitle in the diagram above. The container form is also responsible for checking the state of its embedded form. In this way, it can know if the VB6 form has been closed by an Unload Me instruction and thus can close the container.

Form behavior

In order to mimic the event firing from the VB6 form, the container uses a Timer control to constantly check if there are events in the VB6 form that need to be handled. We could have chosen to pass the events directly from the VB6 form to the .NET application, but that would have implied a much deeper modification of those forms' code, which was exactly what we wanted to avoid.

To give the VB6 form the ability to open "child" non-modal forms and keep its condition of MDIChild, it is necessary to use the ShowForm function in VB6 instead of the usual .Show method. The ShowForm function does all the process needed to keep the called child form in the .NET environment within its container. That is basically all the work that needs to be done with the VB6 forms in order to embed them in a .NET container.

VBScript
'Opens a new child form
ShowForm(frmChildForm, True)

'Opens a new modal form
ShowForm(frmModalForm, False)

So we need only to replace the Show calls in the code to ShowForm(), passing the form we want to show and a second boolean argument indicating if you want it to be an MDI child or not. If so, it calls the OpenForm in the container by passing a message "OpenForm" to it. If not, it just opens the form as a regular modal form.

To preserve the normal behavior of the VB6 forms and their respective containers as MDI child forms – for instance, the normal use of Ctrl-Tab, the focus change between forms or the fact that the .NET MDI Form keeps active to open other .NET or VB6 forms – we needed to use various API functions like SendMessage, SetForegroundWindow and LockWindow.

To enhance this technique, we are planning to add code to the intermediate class that sits between the .NET MDI form and the container. This would allow event firing from VB6 that affects the .NET environment. Examples would be modifying the Caption property of the MDI Form, opening .NET "native" forms, etc.

References

We use a function from Stephen Kent to change the form border style at runtime.

Using the code

The code is pretty simple and is commented where I think it's needed. We didn't use advanced programming techniques, only a few API calls to do the "magic."

Conclusion

This way of interoperating between .NET and VB6 allows us to start a gradual migration path to .NET for our applications that won't force us to make immediate modifications to them. It also instantly adds many of the benefits of the .NET framework we developed to the applications. As an additional feature, this gradual migration path allows the end users to learn gradually how to use the new features that the .NET version of the application offers, without being forced to learn all the changes at once.

Special thanks go to Gustavo Du Mortier and Mariano Aranda from MasterSoft; they really help and colaborate with this.

History

  • 27 June, 2007 -- Original version posted
  • 12 July, 2007 -- Article edited and moved to the main CodeProject.com article base

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 845416125-Oct-13 4:16
Member 845416125-Oct-13 4:16 
GeneralRe: My vote of 1 Pin
AbTak 1237-May-22 23:16
AbTak 1237-May-22 23:16 
QuestionVery good! Pin
Ross Brigoli14-Mar-13 0:27
Ross Brigoli14-Mar-13 0:27 
GeneralMy vote of 3 Pin
dhavalmore139864-Jun-12 2:29
dhavalmore139864-Jun-12 2:29 
QuestionNavigate to other vb6 forms Pin
Member 279775029-Sep-11 20:30
Member 279775029-Sep-11 20:30 
QuestionGrandchild form Pin
Diego Resnik15-May-11 5:55
Diego Resnik15-May-11 5:55 
QuestionVB6 rendering issue with an Radio/Option Button Pin
lavvgiver12-Feb-10 9:22
lavvgiver12-Feb-10 9:22 
AnswerRe: VB6 rendering issue with an Radio/Option Button Pin
Giandroid21-Feb-12 11:01
Giandroid21-Feb-12 11:01 
GeneralSet frmContrainer.vb top of all but as modeless Pin
abdh27-Oct-09 3:12
abdh27-Oct-09 3:12 
Generalactive x error Pin
tenro14-Aug-08 5:52
tenro14-Aug-08 5:52 
GeneralRe: active x error Pin
sisdevar14-Aug-08 9:15
sisdevar14-Aug-08 9:15 
General[Message Removed] Pin
immetoz4-Oct-08 15:08
immetoz4-Oct-08 15:08 
GeneralRe: active x error Pin
Tamires3-Dec-08 6:42
Tamires3-Dec-08 6:42 
GeneralRe: active x error Pin
sisdevar3-Dec-08 6:57
sisdevar3-Dec-08 6:57 
GeneralRe: active x error Pin
gary hagerty6-Jan-21 4:08
gary hagerty6-Jan-21 4:08 
GeneralRe: active x error Pin
Member 140341705-Jul-21 0:34
Member 140341705-Jul-21 0:34 
GeneralAccessViolations and Application-Hangs Pin
Geert Teugels12-Jun-08 21:52
Geert Teugels12-Jun-08 21:52 
GeneralVB6 interop in CAB and SCSF screens Pin
RathiV15-May-08 19:28
RathiV15-May-08 19:28 
GeneralRe: VB6 interop in CAB and SCSF screens Pin
Eduardo Campano18-May-08 3:00
Eduardo Campano18-May-08 3:00 
GeneralRe: VB6 interop in CAB and SCSF screens Pin
RathiV18-May-08 18:37
RathiV18-May-08 18:37 
QuestionWhy I can't create a child form which is not midchild in the FormVB? Pin
Dick Mei19-Sep-07 19:41
Dick Mei19-Sep-07 19:41 
AnswerRe: Why I can't create a child form which is not midchild in the FormVB? Pin
Eduardo Campano20-Sep-07 2:58
Eduardo Campano20-Sep-07 2:58 
AnswerRe: Why I can't create a child form which is not midchild in the FormVB? Pin
Dick Mei20-Sep-07 15:25
Dick Mei20-Sep-07 15:25 
GeneralRe: Why I can't create a child form which is not midchild in the FormVB? Pin
Eduardo Campano18-May-08 2:55
Eduardo Campano18-May-08 2:55 
QuestionWhen you press the TAB key, the focus does not change from one control to another control. Pin
victorleo1-Sep-07 6:52
victorleo1-Sep-07 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.