473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

Don
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
Mar 14 '07 #1
1 8318
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..comwrote 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


Mar 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
9
by: Alan Silver | last post by:
Hello, I have a user control which I am trying to load dynamically, but am running into problems. I think the problem is because I have two .ascx files that refer to the same .ascx.cs file. A...
2
by: slawrence | last post by:
Hi I am getting an error "Code generation for property 'SubCategoryId' failed. Error was 'Property accessor 'SubCategoryId' on object 'yyy.zzz' threw the following exception: 'Object reference...
0
by: slawrence | last post by:
Hi I am getting an error "Code generation for property 'SubCategoryId' failed. Error was 'Property accessor 'SubCategoryId' on object 'yyy.zzz' threw the following exception: 'Object reference...
14
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
0
by: steve.kaye | last post by:
I'm writing a control which contains a collection of Column objects which needs to be written to InitializeComponent() by the designer. I've created a collection object (ColumnCollection) derived...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.