This documentation is archived and is not being maintained.

Walkthrough: Upgrading a Visual Basic 6.0 Application to Visual Basic .NET

In this walkthrough, you will upgrade a Visual Basic graphics sample application to Visual Basic .NET. In addition to illustrating the upgrading process, this walkthrough highlights the differences in graphics architectures between Visual Basic 6.0 and Visual Basic .NET. This should also serve as an introduction to dealing with architectural issues in your applications.

The sample application in this case is a simple form that contains two buttons, a picture box, a timer, and a hidden image control. Although not typical of most applications that you will want to upgrade, it does present several challenging issues that you may face when upgrading your own applications.

Note   This walkthrough requires that both Visual Basic 6.0 and the Drawing.vbp sample application be installed on your development computer. The sample application can be installed from the Visual Studio .NET CD; the default location is Program Files\Microsoft Visual Studio .NET 2003\Vb7\VB Samples\MigrationWalkthrough.

To familiarize yourself with the application

  1. In Visual Basic 6.0, open the Drawing.vbp project file (on the File menu, choose Open Project).
  2. On the Run menu, choose Start. Click the buttons, and observe the run-time behavior and appearance of the application.
  3. On the Run menu, choose End to close the application.
  4. In the Project Explorer, select Form1(PicForm.frm). On the View menu, choose Object. Observe the design-time layout of the form.
  5. On the View menu, choose Code. Familiarize yourself with the application's code.
    Tip   If you do not understand how the code works, try stepping through the code by choosing Step Into on the Debug menu.
  6. On the File menu, choose Exit to close the application. If prompted to save changes, choose No.

To run the Upgrade wizard

  1. Open Visual Studio .NET. On the File menu, choose Open, Project.
  2. In the Open Project dialog box, locate the Drawing.vbp file and open it.

    This will launch the Visual Basic Upgrade Wizard. The first page of the wizard contains an explanation of what the wizard will do.

  3. Click Next to go to the second page of the wizard. This page presents options for upgrading; in this case, there are no applicable options available.
  4. Click Next to go to the third page of the wizard. Click Next to accept the default location for the new project, or enter a different location. The default location is a new folder immediately beneath the Visual Basic 6.0 project's folder.

    If prompted to create a new folder, choose Yes.

  5. On the fourth page of the Wizard, click Next to begin the upgrade.

    When the upgrade is finished, the wizard will close and the new project will appear in Solution Explorer.

To view the upgrade results

  1. In Solution Explorer, select _UpgradeReport.htm and double-click to open the Upgrade Report.

    Note that the report shows no Global Issues that need to be addressed; for PicForm.vb it shows six errors and no warnings.

    Note   If there were warnings, they would also appear in the Task List window as UPGRADE_WARNING items. Warnings are for code that might cause subtle differences in the run-time behavior of your application. Double-clicking an UPGRADE_WARNING in the Task List window will take you directly to the code that may need to be modified.
  2. Expand the PicForm.vb section by clicking on the plus sign in the New Filename column. This displays a detailed list of upgrade issues for the form.

    Note that the issues apply to three different locations: the ClearPictureBox_Click procedure, the Timer1_Timer procedure, and the form layout. You can click on the Description link to display a Help topic explaining each issue. The layout issues in this case do not require any action; checking the Help reveals that they are for two properties of the Visual Basic 6.0 PictureBox control that do not map to equivalent Visual Basic .NET properties

To fix the ClearPictureBox_Click error

  1. In Solution Explorer, select PicForm.vb. On the View menu, choose Code.
  2. In the Code Editor, select the ClearPictureBox_Click procedure.
    Tip   There is an UPGRADE_ISSUE comment added to the procedure. If you scroll to the end of the comment, you can click on the link that displays the associated Help topic. All issues (except for layout issues) are added as code comments during upgrade, including UPGRADE_NOTE comments explaining what happened during upgrade; these do not appear in the Upgrade Report.
  3. Add the following code to the ClearPictureBox_Click procedure:
    Dim g as Graphics = Picture1.CreateGraphics()

    The only error in the original procedure is the Picture1.Cls() method call. In Visual Basic .NET the intrinsic controls do not provide direct access to their drawing surfaces with methods (as in Visual Basic 6.0). All graphics operations are handled with a special object of type Graphics. You can access a control's drawing surface by calling the CreateGraphics() method to get an instance of a Graphics object.

  4. Delete or comment out the original line Picture1.Cls().

To fix the Timer1_Timer errors

  1. In the Code Editor, select the Timer1_Tick procedure.
    Note   The Timer event from the Visual Basic 6.0 Timer control is upgraded to the Tick event of the Visual Basic .NET Timer component. The Upgrade Report shows the old event name; the Code Editor uses the new event name.
  2. At the start of the function add the following code:
    Dim g as Graphics = Picture1.CreateGraphics()

    As with the previous procedure, the errors here begin with the lack of a Graphics object.

  3. Find the first UPGRADE_ISSUE, the Picture1.Print method. Add the following code:
    g.DrawString("VB .NET ROCKS!",  Me.Font, new SolidBrush(Color.Yellow), 0, 0 )

    The DrawString method of the Graphics object replaces the Visual Basic 6.0 Print method. Where the Print method took a single Text argument, DrawString also takes arguments specifying the Font, a Brush object that specifies a Color object, and the starting coordinates for drawing the text.

  4. Delete or comment out the original line Picture1.Print("Visual Basic ROCKS!").
  5. Find the next UPGRADE_ISSUE, Picture1.Cls(). Replace the code with the following:
  6. Find the last UPGRADE_ISSUE, Picture1.PaintPicture. Add the following code:
    g.DrawImage(sourceImage.Image, CSng(LeftPos * Picture1.Size.Width), 0)

    In this case, the DrawImage method of the Graphics class replaces the PaintPicture method.

  7. Delete or comment out the original line Picture1.PaintPicture(sourceImage, LeftPos * VB6.PixelsToTwipsX(Picture1.Width), 0).
  8. At the end of the procedure, add the following code:

    The Dispose method is necessary to free up the memory resources associated with the Graphics object.

To test the application

  1. On the Debug menu, choose Start.
    Note   The first time you run the application, you will be prompted to save the Solution file. Click Save to save the file and run the application.
  2. Click the ShowImage button.

    You will notice that the image flickers when drawing. This is due to the entire area being cleared before each updated image is drawn on the screen.

The solution here is to only draw to the necessary parts of the screen, instead of the entire area represented by the Graphics object. You will want to make sure that when the image is drawn on the screen, any pixels remaining on the screen from the last call to DrawImage() are obscured with the background color. Since the image is moving from right to left, you only have to obscure the additional pixels to the right of the picture.

To fix the screen flicker problem

  1. In the Timer1_Tick procedure, replace the g.Clear(Picture1.BackColor) method with the following code:
    Dim imageWidth As Integer = sourceImage.Image.Width
    Dim imageHeight As Integer = sourceImage.Image.Height
    Dim left As Integer = imageWidth + (Picture1.Size.Width * LeftPos)
    g.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(left, 0, 6, imageHeight))

    In place of the Clear method, you use the FillRectangle method, using the background color to obscure any remaining pixels to the right of the current image being drawn. To do this, you first need to calculate the left-most coordinate of the rectangle, which is accomplished by the first three lines of code.

  2. On the Debug menu, choose Start.
  3. Click the ShowImage button.

    You will notice that the image no longer flickers as it moves.

There is still one additional problem that should be fixed. You may have noticed that the message displayed in the PictureBox control is in a smaller font than the original version. This is because the Font property of the PictureBox control in the Visual Basic 6.0 application was set to Arial Bold 16 at design time. There is no Font property for the Visual Basic .NET PictureBox control, so instead the form's default font (Me.Font) is used. You can remedy this by declaring a new Font object.

To fix the font

  1. In the Timer1_Tick procedure, add the following declaration:
    Dim f As System.Drawing.Font = New System.Drawing.Font("Arial", _
    16, FontStyle.Bold)
  2. In the call to g.DrawString, change the font parameter from Me.Font to f.
  3. On the Debug menu, choose Start.
  4. Click the ShowImage button.

    The text is now displayed in the correct font.

Now that the application is back to parity with the original Visual Basic 6.0 application, you will add some code to enhance the application. After all, there is no point in upgrading the application if you do not want to take advantage of Visual Basic .NET.

In the next step, you will add functionality to make the form fade out slowly when it is closed.

Note   The fade effect is not visible on 256-color displays; you will need to set your color depth to High Color or True Color to view the results. The Opacity property only works on Windows XP.

To enhance the application

  1. In the Code Editor, select the Class Name drop-down list and choose (Base Class Events).
  2. In the Method Name drop-down box, select the Closing event.
  3. In the Form1_Closing procedure, add the following code:
    Dim i as Single
    For i = 1 to 0 Step -0.1
       Me.Opacity = i
  4. On the Debug menu, choose Start.
  5. Close the form and notice how it fades away.

    You can control how fast it fades by increasing the size of the step (or by reducing the Sleep delay).

See Also

Upgrading from Visual Basic 6.0 | Introduction to Windows Forms | Graphics Changes in Visual Basic .NET