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



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition SP1
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition SP1
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition SP1
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • 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 CustomType. The exception was "Constructor on type CustomType not found."
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 "Object reference no set to an instance of an object" 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

  1. Start Visual Studio .NET.
  2. Create a new project.
  3. In Solution Explorer, right-click Form1.cs, and then click View Code.
  4. Locate the default constructor. The default constructor is similar to the following code sample.
    public Form1()
  5. 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)
  6. On the Build menu, click Build Solution.
  7. In Solution Explorer, right-click your project, point to Add, and then click Add Inherited Form.
  8. Make sure that Inherited Form is selected under Templates, and then click Open.
  9. In the Inheritance Picker window, click Form1, and then click OK.
  10. In Solution Explorer, double-click Form2.cs. You receive an error message in Design view.

Modification Type:MajorLast Reviewed:7/19/2005
Keywords:kbBug kbfix KB893709 kbAudDeveloper