Tag Archives: fusion

Microsoft Interop Forms Toolkit 2.1

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

I meant to post this page along time ago and forgot I made it. So here it is, even though its quite late. Anyways, Below is a preview of the new Interop User Controls that can make it much easier to deal with and interop with the Classic versions of Visual Basic 5.0 and Visual Basic 6.0. Here is the link to the msdn page…

Bring the Power of .NET to Visual Basic 6

The Interop Forms Toolkit provides tools and components that simplify the process of building forms and controls with Visual Basic .NET that can be easily consumed from Visual Basic 6. All the required COM interop components can be created with the click of a button, allowing you to quickly create powerful hybrid applications. The Toolkit makes it easy to expose .NET form methods, properties, and events to Visual Basic 6. In addition, functionality is provided to share application state and signal application-level events.

New in Version 2.0 – Interop UserControls

Interop UserControls allow you to host any .NET control on a Visual Basic 6 form as if it was a real ActiveX control.  This means you can extend your existing Visual Basic 6 applications by taking advantage of the rich library of controls that .NET has to offer.

In the image below, you can see a standard Visual Basic 6 form hosting a .NET ToolStrip and a MenuStrip. Creating these controls in Visual Basic 6 would be extremely time-consuming, but by using .NET these controls can be added in seconds!

These controls can also be placed on MDI forms, which the previous version of the Toolkit did not support. Other new features include a redistributable package for easier deployment, RegFree COM support for Interop UserControls, and substantially improved documentation.

Interop Forms

Here we see a Visual Basic 6 Application (Orders) that launches a .NET form as a Customer lookup screen. The .NET form was quickly created using drag and drop from the Data Sources window, and can easily make calls to a Web service.

Leverage Your Visual Basic 6 Investments

The toolkit allows you to preserve your Visual Basic 6 applications and add functionality to them through additional .NET forms. For example, you could provide more dynamic content by adding a WinForm that accesses Web services or RSS feeds. You could create lookup dialogs faster using .NET’s Data Sources Window. ClickOnce Deployment allows you to deploy the entire application (both the Visual Basic 6 part and the .NET part).

Wrap Existing .NET Projects

Although the toolkit provides templates to get you started, you can also expose your existing .NET Forms to Visual Basic 6. Just load your forms into a Class Library, and click on “Generate InteropForm Wrapper Classes” and the tool will create the required COM wrappers.

Fully Extensible

With an extensible architecture and Full Source Code Available, it’s easy to customize the toolkit to your specific requirements.

  • Interfaces: All of the core toolkit services have interfaces defined which you can use to create your own implementations
  • Inheritance: All of the core toolkit services are inheritable and the behavior is overridable
  • Partial Classes: The add-in generated code can be extended via partial classes

If your interested in these Interop UserControls, then head over to the MSDN page. Have fun!

Jason

Revised: 2015

How to use Visual Basic.NET Source Code and Features in VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
 

There is no doubt the .NET Framework contains some rich and very useful objects to make many task very easy to do. Unfortunately Visual Basic 6.0 does not have direct access to these great features. BUT, all is not lost. You can create a COM Wrapper in Visual Basic.NET (I used VB 2005) to make features of the DotNETFramework available to the Legacy/Classic versions of Visual Basic. You can also use VB 2008 and Visual Basic 2010 (Express Edition should be just fine) if thats what you have.

Note: The example projects for this article has been done for a very long time and I forgot about putting a download link on this article. So here are the download links!

VB 6.0 Project with Compiled .NET Wrapper

VB 2005 Project with Compiled .NET Wrapper


Getting Started…

The first thing you would want to do is open up your Visual Basic.NET (Any version is fine as far as I know?.? I am using VB 05 in this article.) IDE and start a new Class Library project. Also, the actual Ping codes are NOT available in Visual Basic.NET 02/03. The principals in the article are the same but the actual Ping code will not work in the earlier dotnet languages. Now, Go ahead and just delete the original Class1.vb file since it is of no use for Fusion. Next goto the Project Menu and click on – Add New Item (Or you can click on ‘Add Class’ for VB 05/08) and select the ComClass Module. I named mine ‘pingWrapperForVB’. I went ahead and added a Reference to System.Windows.Forms since I wanted to use the more advanced MessageBox.Show feature instead of the legacy Msgbox. I also Imported the Namespaces below this image.

Adding the ComClass view


Add these Import Statements right above your Class.

Imports System.Net.NetworkInformation

Imports System.Windows.Forms

Important Note: When adding a COM Class it should automatically check the ‘Register for COM Interop‘ box. If it didn’t then you should go to the ‘Project’ menu and click on the ‘Project Properties‘ item. Click on the Compile option and scroll to the bottom of the page. You should see the ‘Register for Interop‘ checkbox. Make sure that checkbox is checked.

Important Note #2: Just remember if your program is going to be used on a computer other than the Developer computer, then you will need to register the .NET Managed wrapper as a COM object in the Windows registry. Click this link for more information.

Then I created a Public Function named: ping_AddressReply with Two (2x) parameters named (URL as String and TimeoutInMS as Integer) and it returns a Boolean type. I made the TimeoutInMS Parameter a Optional value with a default of 10 Seconds. This Function will be used to send a ping request and get the return value of the request, either ‘True’ that the Ping was successful or ‘False’ if it was not successful.

I then added just a basic Try/Catch/End Try feature that will throw a Ping Exception in a Messagebox if something didn’t go quite right. You can do your own error handling though. Within the Try/Catch structure I added the small piece of code below…

        'Simply sends a Ping request using the URL Parameter and the Timeout Parameter. I made the
        'timeoutInMS Parameter optional with a default value of 10 seconds.
        Return My.Computer.Network.Ping(url, timeoutInMS)

Setup the next Function the same as the first. Except making the Function type a Integer. Remember that VB 6.0 will see the .NET Integer as a Long. I named the Function ‘New ping_AddressTimeItTakes‘ as Integer. Then simple add the codes below inside the Try/Catch structure…

        Dim p As New Ping

        Dim reply As PingReplyreply = p.Send(url, timeoutInMS)

        Return CInt(reply.RoundtripTime)

You are now done with the wrapper. Look over your codes one last time to make sure they are correct and then compile the class library. You are now ready to use the wrapper in Visual Basic 5.0/6.0. Now load your VB 5.0/6.0 IDE and create a new .EXE Project type. I named mine: ‘PingAddressWrapperEx‘. I likewise I added 2x Labels named ‘lblReply’ and ‘lblTime’, a Textbox for the url I named: ‘txtAddress’, and 2x command buttons.  Name the buttons whatever you want to. Now goto the ‘Project’ menu and click on the ‘References’ menu item. Click on the Browse button and locate your .NET Project and goto the ‘Bin’ Directory (or where the compiled class is). Add a reference to the ‘TLB’ file and NOT the .DLL file. VB 6 cannot use the .DLL class directly and thus you need to reference the TLB file to get access to the .DLL in VB 5.0 and VB 6.

A reference view in VB 6.0

Once you create a reference to the TLB file, then add the line of code below in the declaration section…

        'I’m using ping to refer to the Wrapper.
        Dim ping As pingWrapperForVB6.pingWrapperForVB6

Once you take care of that, put the codes below in your command buttons…

    Private Sub cmdPing_Click()

        'Check if a instance of the class already exists, and if it isn’t then create a new instance of the ping wrapper.
        If ping Is Nothing Then

            'Create a new instance of the wrapper.
            ping = New pingWrapperForVB6.pingWrapperForVB6

        End If

        'Send the ping request and get the True or False messages.
        lblReply.Caption = "Replied: " & ping.ping_AddressReply(txtAddress.Text)

    End Sub

Likewise, put the codes below in the other command button…

    Private Sub cmdSendTime_Click()

        'Check if a instance of the class already exists, and if it isn't  then create a new instance of
        'the ping wrapper.
        If Ping Is Nothing Then

            'Create a new instance of the wrapper.
            Ping = New pingWrapperForVB6.pingWrapperForVB6

        End If

        'Send the ping request and display the time it takes to get a reply.
        lblTime.Caption = "Round Trip Time: " & Ping.ping_AddressTimeItTakes(txtAddress.Text, 10000) & "ms"

    End Sub

You can now run your project and put in the URL you want to ping. I put in: vbcodesource.com but you can put the url you want. But Do Not include the http:// portion of the url, it will cause the ping to fail. Now just click on the command buttons that has the code and you should see the status of the pings in the label controls…

Image of the final product :)


Hopefully you had no errors popping up. My example returned True that the url was available and a round trip time of: 42ms.  Thats all there is to it! You now have basic Ping capabilites in VB 6.0. Remember though, the target computer MUST have the version of the DotNET Framework you made the wrapper with. IF not then the Ping Wrapper will fail and you won’t have no ping capabilities. I will eventually add the actual projects for the .NET Wrapper and the VB 6 example when I get the time on my other site at: http://www.vbcodesource.com Anyways, have fun 🙂

               Jason