Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Collection Editor for custom control -C# problem
 

Collection Editor for custom control -C# problem

how to create custom collection editor in property grid and how to store edited values at design time

ex:

public class Car
{
private string make;
private string model;
private int year;
private string owner;

public string CarMake
{
get { return this.make; }
set { this.make = value; }
}

public string CarModel
{
get { return this.model; }
set { this.model = value; }
}

public int CarYear
{
get { return this.year; }
set { this.year = value; }
}

public string CarOwner
{
get { return this.owner; }
set { this.owner = value; }
}

public Car()
{
}
}

public partial class xyz: System.Windows.Forms.Control
{
private List<
Car> bv = new List<Car>();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

public List<
Car> Car
{
get { return bv; }

set
{

if (bv.Count > 0)
{
bv = value;

this.Invalidate();
}
}

}

}
Then the property Car appears in the Properties Page in Design mode. And when I'm trying to edit this property in design time, a collection editor appears and work fine. Everything's good up here. But when I close the collection editor, the value entered is cleared, so I reedit, and the collection appears empty. The value of the property Car is losted; it is not been saved. What should I do in order to persistthe data registered in the collection editor?
ManasMSDN  Wednesday, February 18, 2009 8:03 PM
Hi ManasMSDN,


Did you mean the properties' value of the car are lost when you close the collection editor at design time? If so, I am not suffering this problem by copying and pasting your code directly.

To keep the value persist, I think you are doing right. Apply theDesignerSerializationVisibility attribute to the Car property and pass theDesignerSerializationVisibility.Content as the value of the parameter.

Take a look at the following code. When we set value for the car instance in the PropertyGrid at design time. The designer will serialize the coe in the initializeComponent method. As we can see the values we are setting are persisted in this case.

//
//xyz1
//
car1.CarMake="23";
car1.CarModel="34";
car1.CarOwner="234";
car1.CarYear=0;
car2.CarMake=null;
car2.CarModel=null;
car2.CarOwner=null;
car2.CarYear=0;
car3.CarMake=null;
car3.CarModel=null;
car3.CarOwner=null;
car3.CarYear=0;
car4.CarMake=null;
car4.CarModel=null;
car4.CarOwner=null;
car4.CarYear=0;
car5.CarMake="123";
car5.CarModel="312";
car5.CarOwner="123";
car5.CarYear=0;
this.xyz1.Car.Add(car1);
this.xyz1.Car.Add(car2);
this.xyz1.Car.Add(car3);
this.xyz1.Car.Add(car4);
this.xyz1.Car.Add(car5);
this.xyz1.Location=newSystem.Drawing.Point(424,98);
this.xyz1.Name="xyz1";
this.xyz1.Size=newSystem.Drawing.Size(75,23);
this.xyz1.TabIndex=0;
this.xyz1.Text="xyz1";

If you have any doubts, please feel free to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, February 20, 2009 9:53 AM
Hi ManasMSDN,


Did you mean the properties' value of the car are lost when you close the collection editor at design time? If so, I am not suffering this problem by copying and pasting your code directly.

To keep the value persist, I think you are doing right. Apply theDesignerSerializationVisibility attribute to the Car property and pass theDesignerSerializationVisibility.Content as the value of the parameter.

Take a look at the following code. When we set value for the car instance in the PropertyGrid at design time. The designer will serialize the coe in the initializeComponent method. As we can see the values we are setting are persisted in this case.

//
//xyz1
//
car1.CarMake="23";
car1.CarModel="34";
car1.CarOwner="234";
car1.CarYear=0;
car2.CarMake=null;
car2.CarModel=null;
car2.CarOwner=null;
car2.CarYear=0;
car3.CarMake=null;
car3.CarModel=null;
car3.CarOwner=null;
car3.CarYear=0;
car4.CarMake=null;
car4.CarModel=null;
car4.CarOwner=null;
car4.CarYear=0;
car5.CarMake="123";
car5.CarModel="312";
car5.CarOwner="123";
car5.CarYear=0;
this.xyz1.Car.Add(car1);
this.xyz1.Car.Add(car2);
this.xyz1.Car.Add(car3);
this.xyz1.Car.Add(car4);
this.xyz1.Car.Add(car5);
this.xyz1.Location=newSystem.Drawing.Point(424,98);
this.xyz1.Name="xyz1";
this.xyz1.Size=newSystem.Drawing.Size(75,23);
this.xyz1.TabIndex=0;
this.xyz1.Text="xyz1";

If you have any doubts, please feel free to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, February 20, 2009 9:53 AM

You can use google to search for other answers

Custom Search

More Threads

• How do I create fun-style windows
• Adding Dataset to component tray
• ContainerControl inside another ContainerControl
• Help with Custom Form Design(NicePanel ) Problems
• How to change color of the Form's caption?
• Data grid enter event...
• Textbox and margin
• Single click acting as double click in form designer
• Rich text box selection problem
• Control deleted while trying to clear property value