Microsoft KB Archive/893709

= FIX: You may receive an error message when you open a form in Design view in Visual Studio .NET =

Article ID: 893709

Article Last Modified on 7/19/2005

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-





SYMPTOMS
In Microsoft Visual Studio .NET, when you open a form in Design view, you may receive one of the following error messages even though the project builds and runs correctly:

An exception occurred while trying to create an instance of. The exception was &quot;Constructor on type  not found.&quot;

Object reference not set to an instance of an object.



CAUSE
This issue may occur if you use inheritance and one of the following conditions is true:
 * You have a base form that does not contain a default constructor. A default constructor is a constructor that does not expect parameters.
 * The Form_Load event is used to set values of objects that are contained in the form.

At design time, the inherited form instantiates the base form by using the default constructor. If no default constructor exists, Visual Studio .NET cannot display the inherited form in Design view, and you receive an error message. When the base form is instantiated, the Form_Load event is also called. Objects that are referenced in this event may return the &quot;Object reference no set to an instance of an object&quot; error message when you inherit the base form.



RESOLUTION
To resolve this issue, provide a default constructor for the base form. This constructor should not take any parameters. The default constructor must contain a call to the InitializedComponent method of the base form.

Note The DesignMode property of the Form object determines whether a form is in Design view or not. This property only lets you run code when a form is not in Design view.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Steps to reproduce the issue
 Start Visual Studio .NET. Create a new project. In Solution Explorer, right-click Form1.cs, and then click View Code.  Locate the default constructor. The default constructor is similar to the following code sample. public Form1   Declare a string parameter that is named p_strSample that will be passed to the constructor. To do this, use the following code sample. public Form1( String p_strSample)  On the Build menu, click Build Solution.</li> In Solution Explorer, right-click your project, point to Add, and then click Add Inherited Form.</li> Make sure that Inherited Form is selected under Templates, and then click Open.</li> In the Inheritance Picker window, click Form1, and then click OK.</li> In Solution Explorer, double-click Form2.cs. You receive an error message in Design view.</li></ol>

Keywords: kbbug kbfix KB893709

-

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

© Microsoft Corporation. All rights reserved.