Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Problem with code serialization for child controls
 

Problem with code serialization for child controls

Hello,
I have a user control that contains a TextBox in it. I want to be able to change the properties of the TextBox at design time. For the most part I got it to work, but there's a small problem. Here's the sample code:

public class MyUserControl : System.Windows.Forms.UserControl
{
...
...
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox TBox
{
get { return this.textBox1; }
}
...
...
}

public class Form1 : Form
{
...
//
// userControl11
//
this.userControl11.Controls.Add(this.userControl11.TBox); // how can I prevent this?
this.userControl11.Location = new System.Drawing.Point(64, 32);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(128, 32);
this.userControl11.TabIndex = 0;
//
// userControl11.TBox
//
this.userControl11.TBox.Location = new System.Drawing.Point(0, 0);
this.userControl11.TBox.Name = "textBox1";
this.userControl11.TBox.TabIndex = 0;
this.userControl11.TBox.Tag = "asd";
this.userControl11.TBox.Text = "textBox1";
...
...
}


This is exactly what I want it to do, except for the part where it adds the TextBox to the Controls collection of UserControl again. Why does it do this, since the UserControl will add it itself?
Any help appreciated.
Thanks
Amit
MigrationUser 1  Thursday, June 23, 2005 8:44 PM
Try not using the Usercontrol and instead inherit Control and have the control show on that controls surface, you see the problem is that usercontrol class is a container class. 

Hope this helps.
MigrationUser 1  Saturday, June 25, 2005 9:01 AM
Thanks for the reply. I discovered that it doesn't really add the control anew if it's already been added. That line is in effect harmless so I just let it be.
MigrationUser 1  Sunday, June 26, 2005 6:21 AM
Try not using the Usercontrol and instead inherit Control and have the control show on that controls surface, you see the problem is that usercontrol class is a container class. 

Hope this helps.
MigrationUser 1  Saturday, June 25, 2005 9:01 AM
Thanks for the reply. I discovered that it doesn't really add the control anew if it's already been added. That line is in effect harmless so I just let it be.
MigrationUser 1  Sunday, June 26, 2005 6:21 AM

You can use google to search for other answers

Custom Search

More Threads

• How to handle Ctrl-Drag on design surface?
• can no longer open forms in the designer
• Serialization of custom colors (and other types)
• Adding Dataset to component tray
• How to control order of statements in Win Form serialization process?
• Code Editor Control
• Transparency Issue
• Splitter control on event
• Free controls for Windows Form
• IPersistComponentSettings