Microsoft KB Archive/311337

= You receive an &quot;Image Cannot be Converted to Icon&quot; compiler error message after you upgrade a Visual Basic 6.0 project to Visual Basic .NET or Visual Basic 2005 =

Article ID: 311337

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311337



SYMPTOMS
After you complete the steps in the Upgrade Wizard to upgrade a Visual Basic 6.0 project, when you try to run the newly created Visual Basic .NET or Visual Basic 2005 project, you receive the following compiler error message:

Value of type 'System.Drawing.Image' cannot be converted to 'System.Drawing.Icon'.

The Upgrade Wizard does not report any errors or warnings.

NOTE: The Visual Basic .NET Upgrade Wizard is included in Visual Studio .NET Professional.



CAUSE
This problem occurs because Visual Basic 6.0 code sets the Icon property of the form. In Visual Basic 6.0, the Icon property of a form is of type Image. In Visual Basic .NET or Visual Basic 2005, the Icon property is of type StdPicture.

The following line causes this error message in the Visual Basic .NET or Visual Basic 2005 project: Me.Icon = Image1.Image The following Visual Basic 6.0 code produces the above line: Set Me.Icon = Image1.Picture



RESOLUTION
To resolve this problem, replace the following code in the upgraded Visual Basic .NET or Visual Basic 2005 project Me.Icon = Image1.Image with: Dim bmp As Bitmap bmp = Image1.Image Me.Icon = Icon.FromHandle(bmp.GetHicon)



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Visual Basic 6.0 Standard EXE project. Form1 is created by default.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 Add an Image control to Form1. Set the Picture property of the Image control to an icon (.ico) file.  Insert the following code in the General Declarations section for Form1: Private Sub Form_Load Set Me.Icon = Image1.Picture End Sub  Save the project.</li> Start Visual Studio .NET or Visual Studio 2005, and open the Visual Basic 6.0 project that you saved in the last step. The Upgrade Wizard starts.</li> Complete the steps in the Upgrade Wizard. The Upgrade Wizard creates a new Visual Basic .NET or Visual Studio 2005 project and an upgrade report. Make sure that the upgrade report does not report any errors or warnings.</li> Press the F5 key to run the newly created Visual Basic .NET or Visual Basic 2005 project. You receive the error message that is listed in the &quot;Symptoms&quot; section.</li></ol>

Additional query words: from VB6 to Visual Basic .NET compile

Keywords: kbvs2005swept kbvs2005applies kbmigrate kbprb KB311337

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.