SYMPTOMS
A Microsoft Visual Basic 6.0 project that was upgraded by using Microsoft Visual Studio .NET 2003 no longer functions as expected when you click to select the
Enable application framework check box in Microsoft Visual Studio 2005.
Consider the following scenario:
- You have a Standard
EXE project that was created by using Visual
Basic 6.0.
- You have upgraded the Standard
EXE project to a Microsoft Windows Forms project by using
Visual Studio .NET 2003.
- Then, you have recompiled the
upgraded Windows Forms project in Visual
Studio 2005.
In this scenario, you may notice the following behavior when you
run the Windows Forms project:
- If you use the Form1.DefInstance.PropertyName property
to reference a form property of the default instance of a startup form, any
changes to the property do not take effect.
- If you use the Form1.DefInstance.MethodName method
to reference a method, any method that you call on the default instance of a
startup form does not work as excepted.
Note Form1 is a placeholder for the class
name of the startup form.
CAUSE
This problem may occur in the scenario that is mentioned in
the "Symptoms" section if you enable the
Enable application
framework option in Visual Studio 2005.
The Visual Basic
Upgrade tool adds a
Try/
Catch block to the
New constructor in the project. When the
Enable application
framework option is enabled, the
If statement in this
Try/
Catch block no longer evaluates to
True in Visual Studio 2005.
The following code example shows the
Try/
Catch block that contains the
If statement.
Public Sub New()
.
Try
'For the startup form, the first instance that is created is the default instance.
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
.
End Sub
Because of this behavior, the
m_vb6FormDefInstance = Me statement in the form's
New constructor cannot run when the startup form is being
initialized. Therefore, the
m_vb6FormDefInstance variable does not point to the default instance of the startup
form after the startup form has been initialized.
When you first use
the
Form1.DefInstance property later in the code, a new form is created. Additionally,
the
m_vb6FormDefInstance variable is set to point to this new form. Any later calls to the
Form1.DefInstance property will return a reference to this new form instead of a
reference to the startup form.
RESOLUTION
To resolve this problem, change the old logic from the
If
statement to appear similar to the following code example.
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Or _
System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is My.Application.GetType Then
m_vb6FormDefInstance = Me
End If