PC Review


Reply
Thread Tools Rate Thread

Code generation for property 'Controls' failed. Error was: 'Objectreference not set to an instance of an object.'

 
 
Don
Guest
Posts: n/a
 
      14th Mar 2007
I'm getting the following exception displayed in the task list at design
time for my project:

"Code generation for property 'Controls' failed. Error was: 'Object
reference not set to an instance of an object.'"

I've traced the problem to a custom control I created that inherits from
Inherits System.Windows.Forms.TextBox. In this custom control, I have
two constructors. I'm not sure why I created the second constructor --
it was so long ago -- but the second constructor is causing the problem.
This is the problem constructor:

Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)

End Sub

I've seen this constructor used in many examples of custom controls, but
I'm not 100% sure what it's for. I suspect it may have something to do
with the persistence of certain custom properties of the custom control.


Anyway, the exception only occurs when I have a second custom control --
a System.Windows.Forms.UserControl -- which has a container control on
it (e.g. a GroupBox) inside of which exists my inherited custom Textbox
control. If this second custom control is place within a container
control on a form and the form is saved, the exception appears in the
task list at design time.

This exception is harmless at runtime, but it's bothersome.


You can reproduce this exception by doing the following:

1. Create a new, blank VB.NET project.

2. Create a normal Windows form (e.g. Form1) in the project.

3. Create a UserControl and replace its text with the following:


Public Class MyCustomTextbox
Inherits System.Windows.Forms.TextBox

#Region " Windows Form Designer generated code "

Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)

End Sub
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region

End Class


4. Create a second UserControl and replace its text with the following:


Public Class TestControl
Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents CustomTextbox As TextBoxTest.MyCustomTextbox
Friend WithEvents GroupBox As System.Windows.Forms.GroupBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.CustomTextbox = New TextBoxTest.MyCustomTextbox(Me.components)
Me.GroupBox = New System.Windows.Forms.GroupBox
Me.GroupBox.SuspendLayout()
Me.SuspendLayout()
'
'CustomTextbox
'
Me.CustomTextbox.Location = New System.Drawing.Point(16, 56)
Me.CustomTextbox.Name = "CustomTextbox"
Me.CustomTextbox.TabIndex = 0
Me.CustomTextbox.Text = ""
'
'GroupBox
'
Me.GroupBox.Controls.Add(Me.CustomTextbox)
Me.GroupBox.Location = New System.Drawing.Point(8, 16)
Me.GroupBox.Name = "GroupBox"
Me.GroupBox.Size = New System.Drawing.Size(136, 120)
Me.GroupBox.TabIndex = 1
Me.GroupBox.TabStop = False
Me.GroupBox.Text = "GroupBox1"
'
'TestControl
'
Me.Controls.Add(Me.GroupBox)
Me.Name = "UserControl2"
Me.GroupBox.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

End Class


5. Save all open files, close all designers, and rebuild the project.

6. Open the Form1 designer.

7. Place a container control, such as a GroupBox or Panel control, on
the form.

8. Place a TestControl (the second custom control we created) within the
container control you placed on the form in the previous step.

9. Save the form designer code. The exception should appear in your
task list.


Can anyone shed some light on this problem?

- Don


 
Reply With Quote
 
 
 
 
Mudhead
Guest
Posts: n/a
 
      14th Mar 2007
Remove all of the Windows Form Designer generated code.

Public Class MyCustomTextbox
Inherits System.Windows.Forms.TextBox

' Remove all of this:
#Region "Windows Form Designer generated code"

#End Region

' Your code

End Class


"Don" <unknown@oblivion..com> wrote in message
news:dOTJh.18750$DN.13466@pd7urf2no...
> I'm getting the following exception displayed in the task list at design
> time for my project:
>
> "Code generation for property 'Controls' failed. Error was: 'Object
> reference not set to an instance of an object.'"
>
> I've traced the problem to a custom control I created that inherits from
> Inherits System.Windows.Forms.TextBox. In this custom control, I have two
> constructors. I'm not sure why I created the second constructor --
> it was so long ago -- but the second constructor is causing the problem.
> This is the problem constructor:
>
> Public Sub New(ByVal Container As System.ComponentModel.IContainer)
> MyClass.New()
>
> 'Required for Windows.Forms Class Composition Designer support
> Container.Add(Me)
>
> End Sub
>
> I've seen this constructor used in many examples of custom controls, but
> I'm not 100% sure what it's for. I suspect it may have something to do
> with the persistence of certain custom properties of the custom control.
>
>
> Anyway, the exception only occurs when I have a second custom control --
> a System.Windows.Forms.UserControl -- which has a container control on it
> (e.g. a GroupBox) inside of which exists my inherited custom Textbox
> control. If this second custom control is place within a container
> control on a form and the form is saved, the exception appears in the task
> list at design time.
>
> This exception is harmless at runtime, but it's bothersome.
>
>
> You can reproduce this exception by doing the following:
>
> 1. Create a new, blank VB.NET project.
>
> 2. Create a normal Windows form (e.g. Form1) in the project.
>
> 3. Create a UserControl and replace its text with the following:
>
>
> Public Class MyCustomTextbox
> Inherits System.Windows.Forms.TextBox
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New(ByVal Container As System.ComponentModel.IContainer)
> MyClass.New()
>
> 'Required for Windows.Forms Class Composition Designer support
> Container.Add(Me)
>
> End Sub
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent() call
>
> End Sub
>
> 'UserControl overrides dispose to clean up the component list.
> Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
> If disposing Then
> If Not (components Is Nothing) Then
> components.Dispose()
> End If
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.ComponentModel.IContainer
>
> 'NOTE: The following procedure is required by the Windows Form
> Designer
> 'It can be modified using the Windows Form Designer.
> 'Do not modify it using the code editor.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> components = New System.ComponentModel.Container
> End Sub
>
> #End Region
>
> End Class
>
>
> 4. Create a second UserControl and replace its text with the following:
>
>
> Public Class TestControl
> Inherits System.Windows.Forms.UserControl
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent() call
>
> End Sub
>
> 'UserControl overrides dispose to clean up the component list.
> Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
> If disposing Then
> If Not (components Is Nothing) Then
> components.Dispose()
> End If
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.ComponentModel.IContainer
>
> 'NOTE: The following procedure is required by the Windows Form
> Designer
> 'It can be modified using the Windows Form Designer.
> 'Do not modify it using the code editor.
> Friend WithEvents CustomTextbox As TextBoxTest.MyCustomTextbox
> Friend WithEvents GroupBox As System.Windows.Forms.GroupBox
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> Me.components = New System.ComponentModel.Container
> Me.CustomTextbox = New TextBoxTest.MyCustomTextbox(Me.components)
> Me.GroupBox = New System.Windows.Forms.GroupBox
> Me.GroupBox.SuspendLayout()
> Me.SuspendLayout()
> '
> 'CustomTextbox
> '
> Me.CustomTextbox.Location = New System.Drawing.Point(16, 56)
> Me.CustomTextbox.Name = "CustomTextbox"
> Me.CustomTextbox.TabIndex = 0
> Me.CustomTextbox.Text = ""
> '
> 'GroupBox
> '
> Me.GroupBox.Controls.Add(Me.CustomTextbox)
> Me.GroupBox.Location = New System.Drawing.Point(8, 16)
> Me.GroupBox.Name = "GroupBox"
> Me.GroupBox.Size = New System.Drawing.Size(136, 120)
> Me.GroupBox.TabIndex = 1
> Me.GroupBox.TabStop = False
> Me.GroupBox.Text = "GroupBox1"
> '
> 'TestControl
> '
> Me.Controls.Add(Me.GroupBox)
> Me.Name = "UserControl2"
> Me.GroupBox.ResumeLayout(False)
> Me.ResumeLayout(False)
>
> End Sub
>
> #End Region
>
> End Class
>
>
> 5. Save all open files, close all designers, and rebuild the project.
>
> 6. Open the Form1 designer.
>
> 7. Place a container control, such as a GroupBox or Panel control, on the
> form.
>
> 8. Place a TestControl (the second custom control we created) within the
> container control you placed on the form in the previous step.
>
> 9. Save the form designer code. The exception should appear in your task
> list.
>
>
> Can anyone shed some light on this problem?
>
> - Don
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Access metadata repository generation query, matrix generationquery, table-generation query, array-generation query. Roy Roebuck Microsoft Access Database Table Design 1 20th Dec 2008 08:23 PM
Nvidia's Ageia Purchase - Why Ageia matters - Next Generation SonyPlayStation GPU - Next Generation PC GPUs - Next Generation Console GPU -Nvidia Inside - Nvidia Everywhere sprite scaler AMD 64 Bit 0 8th Feb 2008 02:43 AM
Nvidia's Ageia Purchase - Why Ageia matters - Next Generation SonyPlayStation GPU - Next Generation PC GPUs - Next Generation Console GPU -Nvidia Inside - Nvidia Everywhere sprite scaler ATI Video Cards 0 8th Feb 2008 02:43 AM
Nvidia's Ageia Purchase - Why Ageia matters - Next Generation SonyPlayStation GPU - Next Generation PC GPUs - Next Generation Console GPU -Nvidia Inside, Nvidia Everywhere sprite scaler Video Cards 0 8th Feb 2008 02:41 AM
Error "object reference not set to instance of an object" when trying to set datasource property of a bound column in datagrid control Martin Widmer Microsoft VB .NET 0 23rd Jan 2006 10:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:02 PM.