Upgrading Your First VB6 Application

To get a feel for what the Migration Wizard does for you and what modifications you'll need to make, you will create a new, simple VB6 application and then run it through the Migration Wizard.

Start VB6 and create a new Standard EXE project. Put a text box, a list box, and two buttons on the form. Using the Properties window, change the Sorted property of the list box to True.

Double-click on the first button to open the code window. Enter the following code in the Command1_Click event procedure:

Private Sub Command1_Click()
    List1.AddItem Text1
    List1.ListIndex = List1.NewIndex
End Sub

Press F5 to run the application. Notice that each time you click the command button, the contents of the text box are added to the list box. The list box displays the text in alphabetical order, and selects each line of text as it is added. Now, stop the application and return to VB6. Add a second form to your application. Add a label to the form and resize it to make it larger than normal. Double-click on the form and add the following code in the Form_Load event handler:

Private Sub Form_Load()
    Label1 = "The text entered on Form1 is: " & Form1.Text1
End Sub

Go back to Form1, and add the following code to the Command2_Click event procedure:

Private Sub Command2_Click()
    Form2.Show
End Sub

You now have an application with two forms. One button adds the values entered in the text box to a list box on Form1. The second button opens the second form, which has a reference back to the first form. You can run the application to make sure that it works. Save the project as VB6upgrade.vbp and save the forms with any name you choose; this example will assume that you left them named Form1.frm and Form2.frm.

Save the application with the name VB6upgrade. Close VB6 and open VB .NET. Choose to open an existing project and open the VB6upgrade.vbp file you just created. Opening a VB6 project in VB .NET automatically starts the Visual Basic Upgrade Wizard.

The Visual Basic Upgrade Wizard

The Visual Basic Upgrade Wizard starts automatically because you opened a VB6 project in VB .NET. The first screen just displays some general information, so click the Next button to move into the wizard.

Step 2 of the wizard is shown in Figure 7.1. In most cases, you will leave the options at their default settings, but we will examine the options here for completeness. First, the wizard asks you what type of project you are upgrading. In this case, your only choice is EXE, and that is correct. (If you were upgrading an ActiveX EXE server, you would have the choice to upgrade it to an EXE or DLL.) Below that is one check box; this option tells the wizard to generate default interfaces for any public classes you created. This is useful if the project is a DLL that exposes base classes that are implemented by other applications. Because you do not have any public classes, you can ignore this option. Leave the page as you see it in Figure 7.1 and click the Next button.

Figure 7.1. Step 2 of the Visual Basic Upgrade Wizard.


Step 3 of the wizard asks for the location of the resulting .NET project. By default, the wizard places the VB .NET project in a new directory inside the VB6 project directory called <projectname>.NET. If you want, you can change the path to another name and click the Next button.

Step 4 of the wizard says that you are now ready to upgrade. So, if you click the Next button, the upgrade will start, and at the conclusion of the upgrade, the upgraded project will open in Visual Basic .NET. The wizard always creates a new project; the old VB6 project is left unchanged.

Remember from before that you will have to make some modifications to your project after it is upgraded? Well, the Upgrade Wizard creates an upgrade report in HTML format that lists those modifications. This report shows up in the Solution Explorer as _UpgradeReport.htm, so you'll need to double-click on it to open it. Even though this was a very simple application, one error is reported. Figure 7.2 shows the upgrade report, with the section for Form1 expanded to show the details of the error that was generated.

Figure 7.2. The upgrade report is created by the Upgrade Wizard as it upgrades your VB6 projects to VB .NET.


Examining the Upgraded Forms and Code

Now, double-click on Form1.vb to open it in the designer. Notice first that a ToolTip1 control shows up in the component tray at the bottom of the form. By default, controls in VB .NET do not have a ToolTip property. Therefore, you must add a ToolTip control to a form for your controls on that form to have a ToolTip property.

Double-click on Command1 to get to the code view. Notice that the line of code in the event handler has changed. The old line was:

List1.AddItem Text1

The wizard has upgraded that code to the following line:

List1.Items.Add(Text1.Text)

First, you now have parentheses around the argument. You saw this in Chapter 2, “Your First VB .NET Application.” Also, in VB6, you referred to Text1, but in VB .NET, this has changed to Text1.Text. This should not be surprising because you have learned that VB .NET does not support default properties unless they accept parameters. You'll also notice that the method has changed from List1.AddItem to List1.Items.Add.

Also notice that an upgrade issue was added that alerts you to a problem with the following line:

'UPGRADE_ISSUE: ListBox property List1.NewIndex was not upgraded.
Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup2059.htm
List1.SelectedIndex = List1.NewIndex

If you click on the Click for more hyperlink, you will be taken to a help topic that explains the issue. In VB6, the ListBox.NewIndex property returned the index of the most recently added item. In VB .NET, the property doesn't exist; the index of the newly added item is returned from the Items.Add method.

To fix the problem, change the body of the event to this:

Private Sub Command1_Click(ByVal eventSender As System.Object, _
 ByVal eventArgs As System.EventArgs) Handles Command1.Click
   Dim NewIndex As Integer
   NewIndex = List1.Items.Add(Text1.Text)
   List1.SelectedIndex = NewIndex
End Sub

Running this project reveals that all the code runs fine, and the project works as it did before. Not all projects will transition as smoothly, of course.

Modifications

Why do you need to make modifications yourself? Why can't the upgrade tool do the entire upgrade for you? The answer to this is twofold. In some cases (as in the preceding example), there is not an exact one-to-one correlation between the way code is written in VB6 and the equivalent in VB .NET. So, the best option for the upgrade tool is to alert you to the difference and tell you what you should change yourself. The second reason is that VB6 code that uses late binding is not fully interpreted until it is run. Because the upgrade tool examines the design-time representation of code, it can't perform default property resolutions or name changes for late-bound objects. To see this, take a look at the following examples.

In VB6, the default property of a label control is Caption. In VB .NET, the default property is Text, and the property has to be fully qualified. The upgrade tool knows this, so the following VB6 code

Dim l As Label
Set l = Me.Label1
l = "Hello World"

upgrades perfectly to

Dim l As System.Windows.Forms.Label
l = Me.Label1
l.Text = "Hello World"

However, if you wrote the code using late binding, the upgrade tool could not perfectly upgrade the code because o is late-bound, so it cannot be resolved at design time:

Dim o As Object
Set o = Me.Label1
o = "Hello World"

upgrades to

Dim o As Object
Set o = Me.Label1
'UPGRADE_WARNING: Cannot resolve default property of object o
o = "Hello World"

In this case, you would need to resolve the default property yourself or change the code in VB6 and upgrade it again.

The upgrade tool alerts you to the changes you need to make by listing them in the upgrade report, and by putting to-do comments in code. There are four types of to-do comments:

  • UPGRADE_ISSUE— Compile errors; these are things that must be fixed before the code compiles

  • UPGRADE_WARNING— Differences in behavior; these are things that might cause a problem, and you certainly should look at before running your application

  • UPGRADE_TODO— Code that was partially upgraded, but that you need to complete yourself

  • UPGRADE_NOTE— Code that was significantly changed; you do not have to anything here—the message is purely informational

Differences in Form Code

If you examine the code in the Command2_Click event handler in Form1, you'll notice that the call has changed to this:

Private Sub Command2_Click(ByVal eventSender As System.Object, _
 ByVal eventArgs As System.EventArgs) Handles Command2.Click
   Form2.DefInstance.Show()
End Sub

This points out one of the biggest differences between VB and VB .NET: Forms are not automatically created and ready for you to call. In other words, you couldn't use the following line in your code on Form1:

Form2.Show

Instead, forms are just another type of class, so you have to create the class first and then call a Show method. The Upgrade Wizard's approach to this is to create a new public property called DefInstance, and this property returns an instance of Form2. Therefore, you call this property, and then the Show method on the form that is exposed through the property. There is an alternative way to do this.

Note

The DefInstance property is created for you in the Upgrade Support section that is automatically added to the code of each form.


Instead of the code generated for you by the wizard, you could have shown Form2 using this code:

Dim frm2 as New Form2()
frm2.Show()

As you add new forms to your project, the best way to show forms is to use code like the following:

Dim MyNewForm as New NewForm()
MyNewForm.Show()

Another thing the tool changes: At the top of the form file, the following two lines are added:

Option Strict Off
Option Explicit On

Option Explicit is turned on, even though you didn't explicitly turn it on in the VB6 project you just created. Option Strict is turned off here, which means you can do some implicit conversions.

In the middle of your code is a collapsed block labeled Windows Form Designer generated code. If you expand this, you will see quite a bit of code that creates the form and the controls on the form, and sets up the size, location, and other properties of the form and controls. Similar code was always created for you in previous versions of VB, but it was not revealed to you. Now, you can see it and you can even modify it. Modifications, however, should be left to the designer, which means you go to the designer and make changes and the code is generated for you.

An interesting effect of upgrading is that code is always upgraded to Option Explicit On—so if you have variables that are implicitly created, they will be explicitly created in the upgraded code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.137.152