Hot File

Handling .NET forms and their events in VB6 main application

View: 3373    Dowload: 1   Comment: 0   Post by: naruto  
Author: none   Category: Form - Instructions   Fields: Other

0 point/6 review File has been tested

Download   handling-net-forms-and-their.zip (12.19 KB)

You need to Sign In to download the file handling-net-forms-and-their.zip
If you do not have an account then Sign up for free here

Download error   Report copyright violation

I always wanted to post an article in CodeProject. I was always "taking" not "giving" anything back to this community and that made me feel guilty. I think its time to "give" back something useful to this community

Introduction

 

Throughout my humble experience with COM and .NET interoperability (let's narrow it to VB6, and further to COM client and .NET objects!), I have only found too few resources on how to handle forms between the two technologies…rather than methods and properties. Issues like accessing the database through different forms…causing .NET forms to be closed upon closing the main form in VB6 (.NET forms won't close upon closing the main form in VB6 in the ordinary state!)… and handling the events raised in .NET forms with methods in VB6… all these consumed considerable time and effort with some irritating errors!

In this article, I will try to explain how to handle the last two issues and leave the first one for the readers (maybe only suggest making the .NET form have its own DB connection rather passing the data from VB6 to .NET form).

First of all, we need to understand how exactly we can hook VB6 objects (forms here) to events raised in .NET forms, and then we can solve the issue of closing .NET forms when we close the VB6 main form.

Hooking VB6 forms to events raised in .NET forms

The main idea

Note: The credit for handling events this way goes to Adam Nathan's book ".NET and COM the Complete Interoperability Guide". We will not be using the connection point VB6 provides for automatic event handling. To handle events this way we need to declare the .NET forms in VB6 code with the "WithEvents" keyword. As you know, this keyword can’t be used within methods; only in the declaration part, and many of you, I bet, will need to declare and create .NET forms within methods at runtime whenever you need one, rather than have fixed number of .NET forms in the declaration part of VB6.

The main idea is to create in each .NET form a Hashtable. This Hashtable will hold references to every VB6 object (specifically Form) you wish to handle events for. Now when the .NET event is raised a method within the .NET form (an event handler) will loop through each item in the Hashtable (which represents the reference to the VB6 form) and call the proper method (the VB6 handler) through the reference.

An interface containing event handlers

Of course, by now you will conclude that each COM object (VB6 form in our case) in the Hashtable should have some methods and these methods should have the same name and same signature as each of the objects in the Hashtable. OK, now we can make an interface containing handlers (some methods) for the .NET Form’s events, which each COM object wishes to handle.

//Interface with "event handlers" for COM objects to implement
public interface IDotNetEventsHandler
{
    void NewFormCreated( ref DotNetForm sender );
}

As the name of the event handler implies, the VB6 form (the COM client, the main form) wants to be informed about each DotNetForm newly created (of course, we can make it a "Form" rather than "DotNetForm", but then we need to reference the .NET Framework main DLL in the VB6 application. For the sake of simplicity, we will stick to DotNetForm). The parameter is a reference to the newly created DotNetForm object.

Hooking the VB6 form to the event handlers

Now, to hook the VB6 form to the .NET form events, we define an interface for the .NET form to implement. The interface will contain two methods: Add and Remove.

public interface IDotNetEventsHookup
{
    int Add(IDotNetEventsHandler comObject);
    void Remove(int ComObjectIndex);
}

The Add method will take an object that implements the interface IDotNetEventHandler, in our case it will be the VB6 form. In this method, a reference of the VB6 form will be added to the Hashtable, to be notified of any new DotNetForm created in this .NET form. The reference will be assigned to a key to distinguish it. The Remove method simply removes the reference from the Hashtable.

This code in the VB6 application shows how these methods are used:

    Dim netForm As NetEvents.DotNetForm
    Set netForm = New NetEvents.DotNetForm
    
    Dim Ihookup As NetEvents.IDotNetEventsHookup
    Set Ihookup = netForm
    Dim cookie As Long
    cookie = Ihookup.Add(Me)

We now have the VB6 form hooked to the DotNetForm events (it's being referenced in the Hashtable).

This code will be executed when the event is raised in .NET; its main goal is to traverse all references of VB6 forms in the Hashtable to call the proper methods for this event (the event handler):

private void OnNewFormCreated()
{
    for(int counter=1; counter<=this.comObjects.Count; counter++)
         {
        IDotNetEventsHandler obj = 
            (IDotNetEventsHandler)comObjects[counter];
        DotNetForm form = new DotNetForm();
        form.Show();
        obj.NewFormCreated( ref form );
    }
}

Note: Of course, the new DotNetForms should be created outside of the event handler and then passed to the event handler, but if we do this the VB6 event handler will confuse the reader so, we will keep it like this and leave the alteration process to the reader.

Closing .NET forms by closing the main VB6 form

Now after learning (hopefully) how the events are handled, the rest is really simple. All we need is a dynamic array in VB6 form to hold DotNetForm references passed to the event handlers (that are newly created), and then hook this object to the passed DotNetForms.

Private Sub IDotNetEventsHandler_NewFormCreated(_
                                sender As NetEvents.DotNetForm)

  FormsCounter = FormsCounter + 1 'FormsCounter is declared 
                                 'previously in the declaration 
                                 'part
  ReDim Preserve DotNetForms(FormsCounter)
  Set DotNetForms(FormsCounter) = sender
 
  Dim Ihookup As NetEvents.IDotNetEventsHookup
  Set Ihookup = sender
 
  Dim cookie As Long
  cookie = Ihookup.Add(Me)
 
  MsgBox ("Created " + sender.Text)
End Sub

And then traverse these references to close them as needed (usually in the QueryUnload method in VB6 form):

 Dim tempForm As NetEvents.DotNetForm
    While FormsCounter >= 1
      Set tempForm = DotNetForms(FormsCounter)
      If Not tempForm Is Nothing Then
        tempForm.Close
      End If
      FormsCounter = FormsCounter - 1
    Wend

Handling .NET forms and their events in VB6 main application

Handling .NET forms and their events in VB6 main application Posted on 09-05-2014  I always wanted to post an article in CodeProject. I was always "taking" not "giving" anything back to this community and that made me feel guilty. I think its time to "give" back something useful to this community 5/10 3373

Comment:

To comment you must be logged in members.

Files with category

  • Form Management in MOSS 2007

    View: 2864    Download: 0   Comment: 0   Author: none  

    Form Management in MOSS 2007

    Category: Form - Instructions
    Fields: Other

    0/7 review
    The purpose of this document is to give an overview of Form Management in Microsoft Office SharePoint Server 2007. This article explains how to create and publish forms using Microsoft Office InfoPath 2007 in to SharePoint site.

  • Mac OS X Theme for Windows Forms

    View: 3348    Download: 2   Comment: 0   Author: none  

    Mac OS X Theme for Windows Forms

    Category: Form - Instructions
    Fields: Other

    0/9 review
    Some programmers might want to apply some themes or skins to their forms so that it will look cool, sophisticated, and attractive. I have created a Mac OS X open source theme library for everyone and the usage of it is indicated on this article.

  • Those Crazy Popups

    View: 1792    Download: 0   Comment: 0   Author: none  

    Those Crazy Popups

    Category: Form - Instructions
    Fields: Other

    0/1 review
    Ever tried to create a pop-up in ASP.NET? Well, in this article, we will see different ways to create them. We'll also see a few issues surrounding the different ways.

  • Handling .NET forms and their events in VB6 main application

    View: 3373    Download: 1   Comment: 0   Author: none  

    Handling .NET forms and their events in VB6 main application

    Category: Form - Instructions
    Fields: Other

    0/6 review
    I always wanted to post an article in CodeProject. I was always "taking" not "giving" anything back to this community and that made me feel guilty. I think its time to "give" back something useful to this community

  • Beginners’ Guide To Web Design

    View: 489    Download: 0   Comment: 0   Author: none  

    Beginners’ Guide To Web Design

    Category: Form - Instructions
    Fields: Other

    0/2 review
    If you’re a total newbie to the whole web designing business, you’ve come to the right place. Web design is a tricky thing to deal with, and with that being said there are many things that you have to keep in mind. Enumerated below are the things...

  • GraphicRiver.net – A Review

    View: 682    Download: 0   Comment: 0   Author: none  

    GraphicRiver.net – A Review

    Category: Form - Instructions
    Fields: none

    0/1 review
    Designers are always looking for resources to maximize their revenue. Whether it is a useful freelancing website, a blog or a marketplace, they never fail to entertain a designer’s desire to earn and learn, too. In this article I am going to review...

  • How to Establish a Dreamweaver Website on Localhost – EasyPhp

    View: 1025    Download: 0   Comment: 0   Author: none  

    How to Establish a Dreamweaver Website on Localhost – EasyPhp

    Category: Form - Instructions
    Fields: none

    0/4 review
    While developing a website, it is sometimes quite annoying and repetitive to edit the code in the editor and then test it on the browser, especially when working with WordPress. So, to ensure smooth workflow and faster development, we use a feature...

  • Top 15 Websites to learn Web Design and Development

    View: 720    Download: 0   Comment: 0   Author: none  

    Top 15 Websites to learn Web Design and Development

    Category: Form - Instructions
    Fields: none

    0/1 review
    People are, at all times looking to enhance their skills at Designing and Developing websites. Formal education in this field is quite rare so in this article, I’m going to share the 15 Top Websites to Learn Web Design and Development from, more...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd