Changes in Visual Basic .NET have resulted in the obsolescence of several objects from previous versions of the language, making a Visual Basic 6.0 object upgrade to .NET the logical way to go. This chapter describes the most commonly used Visual Basic 6.0 objects that are no longer supported in Visual Basic .NET and how to replace them with Visual Basic .NET technologies. In other words, you’ll learn all about the process behind a VB screen object upgrade, a VB clipboard object upgrade or a VB licenses collections upgrade, using the Visual Basic Upgrade Wizard.
Additionally, the Visual Basic Upgrade Companion includes automated support for the following components and technologies, among many others:
Plus conversion to C# (the Upgrade Wizard only migrates to VB.NET), type inference, code refactoring, error handling replacement, use of native .NET libraries, etc. Beyond that, the Visual Basic Upgrade Companion can be extended to support the user’s specific migration needs.
The App object in Visual Basic 6.0 is a global object that is used to set or retrieve information about the application. Though there is no direct equivalent in Visual Basic .NET, most of the properties can be mapped to equivalent properties. For example, the App object’s version information properties are replaced by assembly attributes. In Visual Basic 6.0, you can set the version information by using the Project Properties dialog box, while in Visual Basic .NET you should use the assembly attributes, which are set by editing the AssemblyInfo file of your converted project.
The Visual Basic 6.0, the Screen object provides properties and methods for retrieving, and in some cases setting, global application properties. Using the Screen object, you can set the MousePointer property to display an hourglass pointer when a modal form is visible or retrieve the active control or active form of the application. Although there is not a direct equivalent for the Screen object in Visual Basic .NET, most of the properties can be mapped to equivalent properties in the .NET Framework.
In Visual Basic 6.0, the Printer object enables applications to communicate with a system printer, containing functions for printing text, lines, and images. However, the printing model in the .NET Framework is quite different, and the Printer object has become obsolete. Fortunately, the functionality of the Printer object can be achieved with the classes that are provided in the System.Drawing.Printing namespace, particularly the PrintDocument class.
There are two main options to upgrade the Visual Basic 6.0 printing functionality.
The first option is to replace the Printer object members with equivalent functionality provided by the PrintDocument class. Typically, this requires reimplementation of the printing code. The .NET Framework provides more control and power over the drawing operations, but from the upgrade point of view, it has a learning curve, and the manual reimplementation of all the printing functionality can consume extensive resources.
The second option is to create your own Printer class in Visual Basic .NET based on the .NET PrintDocument class. This approach will allow you to mask the .NET
PrintDocument class functionality with the names provided in the Visual Basic 6.0
Printer object. This allows you to consolidate several drawing methods and collections of graphical objects (such as Circle or Line) and to store the coordinates that will be used to print these objects when the Print method or the EndDoc method is called. When the Print method of the PrintDocument class is called, the PrintPage event is raised. This event can be used to signal the application to draw all the objects and text stored in the collections of your PrinterClass.
Visual Basic .NET does not support the Printers collection, which is used in VB 6.0 to return information about available printers on a system. The printing model has changed, and as a result, references to the Printers collection require manual adjustment; applying the upgrade wizard results in the code being marked with conversion issues, and the only way to achieve a similar effect in Visual Basic .NET is to implement your own Printers collection, using various classes the Microsoft .NET Framework provides.
The Forms collection cannot be automatically upgraded with the upgrade wizard because Visual Basic .NET does not support the Forms collection. The Forms collection in Visual Basic 6.0 is a collection of all loaded forms in the project. The most common uses of the Forms collection are to determine whether a form is loaded, to iterate through the loaded forms, and to unload a form by name.
Visual Basic .NET provides similar functionality and you can create a module and class masking the functionality
However, please note that in Visual Basic 2005, the Forms collection is implemented in the My.Application.OpenForms class. This will make it possible for an automatic upgrade to be performed, with no need to create wrapper classes or modules with global members. Upgraded Forms collection code will look very similar to the original code.
Visual Basic 6.0 has a Clipboard object that allows you to store and retrieve text and graphics to and from the Clipboard. In Visual Basic .NET, you manipulate the
Clipboard using the System.Windows.Forms.Clipboard namespace. The new Clipboard classes are more flexible than those in Visual Basic 6.0 in that they allow you to set and retrieve data in a particular format and query the contents of the Clipboard object to see what formats are supported.
The new flexibility comes at a cost; Visual Basic 6.0 Clipboard object code cannot be automatically upgraded. However, you will find it straightforward to implement the same functionality in Visual Basic .NET.
For Visual Basic 2005, My.Computer is one of the top level groupings under the My namespace provided with Visual Studio .NET 2005. My.Computer provides access to instances of the most commonly used objects of the .NET Framework that relate to items in the computer running the application. One of these items is the Clipboard. The Visual Basic Upgrade Wizard 2005 will automatically upgrade the Visual Basic 6.0 Clipboard members to My.Computer.Clipboard members.
Visual Basic 6.0 allows you to dynamically load ActiveX controls when an application is running. Some ActiveX controls require the use of a license for them to be used within a program. The Licenses collection of an application contains the license information for all loaded ActiveX controls and allows you to add and remove each license as needed. The licensing model in Visual Basic .NET is completely different, so upgrading applications that use the Licenses collection requires some effort.
Visual Basic .NET compiles the license directly into the executable. That means that the control must already be present in the project before it can be dynamically added at run time. Thus, license management cannot be automatically upgraded, and an alternative strategy must be applied in Visual Basic .NET. One way to do this is to add a dummy form to the project and put all ActiveX controls that will be dynamically added to this form. After you create this dummy form, you can dynamically add the ActiveX control to any form in your project.
To add and remove controls at run time, Visual Basic 6.0 provides the Controls Collection. One of the major disadvantages of this model is that the it does not support Microsoft IntelliSense® technology. You have to remember the methods, parameters, and ProgID of the control to add.
The upgrade wizard does not automatically upgrade Visual Basic 6.0 code using the Controls collection Add method because of changes in the behavior of Add between Visual Basic 6.0 and Visual Basic .NET.
It is relatively easy to add and remove intrinsic controls in Visual Basic .NET. In Windows Forms, the form model used in Visual Basic .NET, controls are indexed by number instead of name. To remove a control by name, you have to iterate through the Me.Controls collection, find the desired control, and remove it.
In the Visual Basic 2005, the Controls collection provides methods that allow removing controls in the same manner as Visual Basic 6.0, so you will not have to iterate through the Controls collection. It is also possible to access one control by indicating the name of the control (as a string).
Visual Studio 2005 also adds other new methods to the ControlCollection class that help achieve some of the functionality that Visual Basic 6.0 programmers expected from the ControlArray. ControlCollection is the type of the Controls property of the Control class. This class now contains a find() method that allows you to search a control and all its child controls recursively for a control with a specific name and returns an array with all the matching controls. The other new methods are RemoveByKey(), ContainsKey(), IndexOfKey(), and a new string parameter for the Item property. All these new methods provide ways to manipulate the ControlCollection using the names of the controls instead of their index.
In Visual Basic 6.0, this collection contains all the controls that have been added to a particular Form, including controls that were added to other container controls such as PictureBox or Frame controls. In Visual Basic .NET, the Controls collection includes only the controls that were directly added to the control that owns the collection. For example, if a Form contains a Panel control that contains a Button control named button1, button1 will not be contained in the Controls collection that belongs to the Form. Instead of that, button1 will be contained in the panel’s Controls collection. As a consequence, iterating through the elements in a form’s Controls collection will not consider all the controls contained in the form, as it did in Visual Basic 6.0.
Visual Basic .NET creates wrappers for ActiveX controls. These wrappers must exist before a control can be added. In addition, most ActiveX controls have design-time licenses that must be present before the control can be created on the form. Visual Basic .NET compiles the license into the executable. These factors mean that the control must already be present in the project before it can be dynamically added at run time. One way to do this is to add a dummy form to the project and put all ActiveX controls that will be dynamically added to this form. After you create this dummy form, you can dynamically add the ActiveX control to any form in your project.
After the control is added, it can be dynamically removed in a manner similar to removing an intrinsic control. Only adding requires additional work.