Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Designer Writing Broken Code in InitializeComponent (Class must be serializable?)
 

Designer Writing Broken Code in InitializeComponent (Class must be serializable?)

Hi,

I have a form that has a user control on it. This user control maintains all of it's values using a global object. The user control has a blank constructor that instanciates that object with no values. For some reason adding this line that instanciates the object causes the designer to try to set the properties of the user control in the InitializeComponent method. For instance, if the user control constructor inclues "globalObject = new myObject();" the designer will add lines of code to create it's own myObject and set it through the user control's property which is not necessary. Any ideas? Thanks.

xr280xr  Monday, November 27, 2006 8:54 PM
You should not modify the code the designer creates in the *.designer.cs file.  It tends to get very confused when you do that.
Peter Ritchie  Monday, November 27, 2006 9:08 PM

The designer is going to attempt to serialize all the public properties of a control, it doesn't know what isn't applicable in your circumstances and what isn't.

In general though, you shouldn't change anything in the FormX.Designer.cs. If you want to do something similar to InitializeComponent, do it in the forms contstructor after the call to InitializeComponent. The recommended approachfor event handlers is to do it in the designer by double-clicking the event in the event list...

Peter Ritchie  Wednesday, November 29, 2006 12:58 AM
You should not modify the code the designer creates in the *.designer.cs file.  It tends to get very confused when you do that.
Peter Ritchie  Monday, November 27, 2006 9:08 PM
I tried, but if I make any design changes it changes it back. Also some times even if I just click save, or build the project it changes it back. There are other classes that have a similar structure w/out this problem but I'm not sure what the difference is. Thanks.
xr280xr  Tuesday, November 28, 2006 2:08 AM
what changes do you want (or are trying)to make?
Peter Ritchie  Tuesday, November 28, 2006 4:48 PM

All kinds, I just started designing the form. Any action that will trigger Visual Studio to rewrite it's InitializeComponenet method. The change I made that caused the error was adding an event handler to a button. Of course I could code these things without using the designer, but I'm trying to understand why the designer thinks it has to initialize the user control's properties when it doesn't need to.

A solution I just found is to add the following attribute to the properties:

[DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]

to prevent the designer from serializing these properties to the InitializeComponent method. I still do not fully understand why it was getting the error in the first place though.

xr280xr  Tuesday, November 28, 2006 9:33 PM

The designer is going to attempt to serialize all the public properties of a control, it doesn't know what isn't applicable in your circumstances and what isn't.

In general though, you shouldn't change anything in the FormX.Designer.cs. If you want to do something similar to InitializeComponent, do it in the forms contstructor after the call to InitializeComponent. The recommended approachfor event handlers is to do it in the designer by double-clicking the event in the event list...

Peter Ritchie  Wednesday, November 29, 2006 12:58 AM

You can use google to search for other answers

Custom Search

More Threads

• VerticalText in DataGrid
• Cannot access a disposed object.
• how to add one more control in open dialog box
• Arrow Keys don't move controls in custom forms designer
• Phased drawing on a pictureBox - problem
• FindControl.
• Visual Inheritance
• Resizing user control
• Help on the Menu Dynamic
• How customize data in datagridview with bound columns?