Welcome!

Microsoft Cloud Authors: Pat Romanski, Andreas Grabner, Nick Basinger, Kevin Benedict, Liz McMillan

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Converting VB6 to VB.NET, Part I

A Look at Your Options and When to Use Them

Bad News and Good News
While most VB6 controls convert well, there are exceptions. For example, line and shape controls and commands do not convert very well, and the DB, OLE, and UpDown counter controls do not convert at all. I'll discuss these in more detail in my next article.

Form.Print doesn't exist in VB.NET, and there is no real workaround that I know of. This has got to be the one thing I miss most from VB6. This was also true when I used C++ 6.0 instead of VB6 to write user interfaces. The general solution is to get a bitmap of the form and print that. If you do the scaling right, this can work well, but in many cases, the result is a low quality print. This can be especially true of text. Items that the programmer draws, such as user draw items and general graphics, can be drawn on the printer canvas instead of the screen canvas. Scaled right, this tends to work well, but for controls and other items that draw themselves, creating and printing a bitmap is the only option.

Another feature missing from VB.NET is control arrays. However, there is a fix for this in the Microsoft.VisualBasic.Compatablity.VB6 namespace. This little gem of a namespace has a number of classes that emulate missing VB6 features. Among other things, it contains a control array class for each of the major VB6 controls. The upgrade wizard uses these classes to seamlessly upgrade control arrays. This works really well.

Some of the other things in this namespace are classes for converting VB6 fonts to VB.NET Font objects, image format conversions, and loading resources. Also included are replacements for the VB6 Format, Equ, Imp, ValidateControls, WhatsThisMode, and SendsKeys functions. .NET uses different coordinates from VB6 for windows and graphics. This namespace includes functions to convert between the two. It also contains replacements for File and Dir listboxes, the EXEName and Hinstance functions in the VB6 App object, and some functions missing from the VB.NET Button control. The namespace also has a CopyArray function that allows assigning an array to a variant.

Timer Control
In VB6, setting an interval of 0 disables the timer. The new Timer control doesn't accept a value of 0 (it generates a runtime error) and can only be disabled by setting the Enable property to false. Code that doesn't use an interval of 0 to disable the timer will convert correctly. Code that switches between an interval of 0 (disabled) and a valid value can easily be corrected before converting. More complex code that uses both the Enable and the Interval properties in an "OR'ed" manner may be tricky to upgrade. One instance in which I dealt with this was a VB6 timer controlling a flashing error light. Any one of several subroutines could start the flashing by setting the enabled property to true, which was reset to false after user acknowledgement. The interval property was set to zero to globally disable the alarm. I rewrote the code to use an alarm object that handled all the logic. However, another option would be to write a new Timer class - derived from the .NET Timer class - that overrides the Enable and Interval properties and duplicates the behavior of the VB6 Timer.

 


Imports System
Namespace VB6Timer

Public Class VB6Timera
Inherits System.Windows.Forms.Timer
Private MyInterval As Integer
Private MyEnabled As Boolean

Public Sub New()
MyInterval = MyBase.Interval
MyEnabled = MyBase.Enabled
End Sub

Public Overloads Overrides Property Enabled() As Boolean
Get
Return MyEnabled
End Get
Set(ByVal Value As Boolean)
MyEnabled = Value
If Not Value Then
MyBase.Enabled = False
Else
If Not (MyInterval = 0) Then MyBase.Enabled = True
End If
End If
End Set
End Property

Public Shadows Property Interval() As Integer
Get
Return MyInterval
End Get
Set(ByVal Value As Integer)
If Value = 0 Then
MyBase.Enabled = False
Else
MyBase.Interval = Value
If (MyInterval = 0) And (MyEnabled) Then
MyBase.Enabled = True
End If
End If
MyInterval = Value
End Set
End Property

Protected Overloads Overrides Sub Dispose
(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
End Class
End Namespace

To use this class, compile it under .NET and add a reference to it by browsing for it from the toolbox add component dialog. After the conversion, go to the formdesigner and remove the standard Timer, then add the VB6Timer from the toolbox to the form, and set its properties and name to match the old Timer control. This technique can be used in cases where there are minor differences between a VB6 control and its VB.NET replacement.

Looking Ahead
Over the next two articles, we will finish up general conversions - ADO, ASP, and VB.NET to C# and take a quick look at the VB.NET 2005 upgrade wizard.

More Stories By Dennis Hayes

Dennis Hayes is a programmer at Georgia Tech in Atlanta Georgia where he writes software for the Adult Cognition Lab in the Psychology Department. He has been involved with the Mono project for over six years, and has been writing the Monkey Business column for over five years.

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.