Welcome!

Microsoft Cloud Authors: Andreas Grabner, Nick Basinger, Kevin Benedict, Pat Romanski, 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
Today's workforce is trading their cubicles and corporate desktops in favor of an any-location, any-device work style. And as digital natives make up more and more of the modern workforce, the appetite for user-friendly, cloud-based services grows. The center of work is shifting to the user and to the cloud. But managing a proliferation of SaaS, web, and mobile apps running on any number of clouds and devices is unwieldy and increases security risks. Steve Wilson, Citrix Vice President of Cloud,...
Artificial intelligence, machine learning, neural networks. We're in the midst of a wave of excitement around AI such as hasn't been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. This time is (mostly) different. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Pattern recognition can equal or exceed the ability of human experts in some domains. It's devel...
The term "digital transformation" (DX) is being used by everyone for just about any company initiative that involves technology, the web, ecommerce, software, or even customer experience. While the term has certainly turned into a buzzword with a lot of hype, the transition to a more connected, digital world is real and comes with real challenges. In his opening keynote, Four Essentials To Become DX Hero Status Now, Jonathan Hoppe, Co-Founder and CTO of Total Uptime Technologies, shared that ...
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility. As they do so, IT professionals are also embr...
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...
As the fourth industrial revolution continues to march forward, key questions remain related to the protection of software, cloud, AI, and automation intellectual property. Recent developments in Supreme Court and lower court case law will be reviewed to explain the intricacies of what inventions are eligible for patent protection, how copyright law may be used to protect application programming interfaces (APIs), and the extent to which trademark and trade secret law may have expanded relev...
When Enterprises started adopting Hadoop-based Big Data environments over the last ten years, they were mainly on-premise deployments. Organizations would spin up and manage large Hadoop clusters, where they would funnel exabytes or petabytes of unstructured data.However, over the last few years the economics of maintaining this enormous infrastructure compared with the elastic scalability of viable cloud options has changed this equation. The growth of cloud storage, cloud-managed big data e...
Your applications have evolved, your computing needs are changing, and your servers have become more and more dense. But your data center hasn't changed so you can't get the benefits of cheaper, better, smaller, faster... until now. Colovore is Silicon Valley's premier provider of high-density colocation solutions that are a perfect fit for companies operating modern, high-performance hardware. No other Bay Area colo provider can match our density, operating efficiency, and ease of scalability.
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...