Welcome!

Microsoft Cloud Authors: Pat Romanski, Liz McMillan, Lori MacVittie, Elizabeth White, Yeshim Deniz

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
The deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
Machine learning has taken residence at our cities' cores and now we can finally have "smart cities." Cities are a collection of buildings made to provide the structure and safety necessary for people to function, create and survive. Buildings are a pool of ever-changing performance data from large automated systems such as heating and cooling to the people that live and work within them. Through machine learning, buildings can optimize performance, reduce costs, and improve occupant comfort by ...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Poor data quality and analytics drive down business value. In fact, Gartner estimated that the average financial impact of poor data quality on organizations is $9.7 million per year. But bad data is much more than a cost center. By eroding trust in information, analytics and the business decisions based on these, it is a serious impediment to digital transformation.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Predicting the future has never been more challenging - not because of the lack of data but because of the flood of ungoverned and risk laden information. Microsoft states that 2.5 exabytes of data are created every day. Expectations and reliance on data are being pushed to the limits, as demands around hybrid options continue to grow.
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...