Dela via


Walkthrough: Upgrading a Visual Basic 6.0 Application to the Current Version of Visual Basic

In this walkthrough, you will upgrade a Visual Basic graphics sample application to Visual Basic 2008. In addition to illustrating the upgrading process, this walkthrough highlights the differences in graphics architectures between Visual Basic 6.0 and Visual Basic 2008. 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 Visual Basic 6.0 to be installed on your development computer.

To create the Visual Basic 6.0 application

  1. Open Visual Basic 6.0. On the File menu, choose New Project.

  2. In the New Project dialog box, choose Standard EXE, and click OK.

  3. Add a PictureBox control to the form, and size it to fill most of the form, leaving space at the bottom.

  4. Set the AutoRedraw property to True, the DrawStyle property to 0-Solid, and the FillStyle property to 0-Solid.

  5. Add two CommandButton controls below the PictureBox control.

  6. Select the first CommandButton, and set the Name property to ClearPictureBox and the Caption property to Clear.

  7. Select the second CommandButton, and set the Name property to ShowImage and the Caption property to Show Image.

  8. Add an Image control to the form, and set the Name property to sourceImage and the Visible property to False.

  9. Set the Picture property of the Image control to any bitmap image. You can use one of the pictures in your My Pictures folder.

  10. Add a Timer control to the form. Set the Enabled property to False and the Interval property to 25.

  11. Double-click the form to open the Code Editor, and enter the following code.

    Option Explicit
    Private LeftPos As Double
    
    Private Sub ClearPictureBox_Click()
        Picture1.Cls
    End Sub
    
    Private Sub ShowImage_Click()
        LeftPos = 1
        Me.Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
    
        If LeftPos <= 0 Then
            Me.Timer1.Enabled = False
            Picture1.Print "Visual Basic ROCKS!"
        Else
            LeftPos = LeftPos - 0.01
            Picture1.Cls
            Picture1.PaintPicture sourceImage, LeftPos * _
    Picture1.Width, 0
        End If
    End Sub
    
  12. Press F5 to run the application. Click the buttons to observe the behavior, and step through the code to see how it works.

  13. On the File menu, choose Save Project As.

  14. In the Save File As dialog box, save the form as PicForm.frm and save the project as Drawing.vbp.

To run the Upgrade wizard

  1. Open Visual Basic 2008. On the File menu, choose Open Project.

  2. In the Open Project dialog box, locate the Drawing.vbp file and open it.

    This launches 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 closes, and the new project appears in Solution Explorer.

To view the upgrade results

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

    Notice 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 takes 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.

    Notice 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 2008 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 an upgrade, including UPGRADE_NOTE comments explaining what happened during the 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()
    g.Clear(Picture1.BackColor)
    g.Dispose()
    

    The only error in the original procedure is the Picture1.Cls() method call. In Visual Basic 2008, 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 2008 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:

    g.Clear(Picture1.BackColor)
    
  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:

    g.Dispose()
    

    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

    if you are 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.

Further Modifying the Application

The solution here is to draw only 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 AsInteger = sourceImage.Image.Width
    Dim imageHeight AsInteger = sourceImage.Image.Height
    Dim left AsDouble = imageWidth + (Picture1.Size.Width * LeftPos)
    
    g.FillRectangle(New SolidBrush(Me.BackColor), _
      New Rectangle(left, 0, 6, imageHeight))
    

    In place of the Clear method, 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 2008 PictureBox control; 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 2008.

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 works only on Windows XP.

To enhance the application

  1. In the Code Editor, select the Class Name drop-down list and choose (Form1 Events).

  2. In the Method Name drop-down box, select the FormClosing event.

  3. In the Form1_FormClosing procedure, add the following code:

    Dim i AsSingleFor i = 1 To 0 Step -0.1
      Me.Opacity = i
      Application.DoEvents()
      System.Threading.Thread.Sleep(100)
    Next
    
  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

Concepts

Graphics for Visual Basic 6.0 Users

Reference

Windows Forms Overview

Other Resources

Upgrading from Visual Basic 6.0