CollectionBase derived class not saving design time items

N

niyad

hi
On a form ive added my custom component (MyComponent) which has a property
(IDs in the expample code) exposing a collection of int16. I see this
property in the PropertyGrid and I dont have trouble adding items to the
collection at design time (using standard CollectionEditor class). I can
Add, remove items at design time, however all the items are lost after a
compilation.

public class MyComponent : Component{
Int16Collection int_col;

public Int16Collection IDs{
get{
return int_col;
}
set{
int_col = value;
}
}
}

Here's my custom collection class.

public class Int16Collection : CollectionBase {
public Int16 this[ int index ]{
get{
return( (Int16) List[index] );
}
set{
List[index] = value;
}
}
.....
....

I dont understand how/why all the items added at design time disappear on
compilation of the form. Where are the items saved between design time and
compilation time?

any help much appreciated
 
C

Claes Bergefall

Add the following to your IDs property
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

/claes
 
N

niyad

Still the same. I had tried that before.

Claes Bergefall said:
Add the following to your IDs property
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

/claes


niyad said:
hi
On a form ive added my custom component (MyComponent) which has a property
(IDs in the expample code) exposing a collection of int16. I see this
property in the PropertyGrid and I dont have trouble adding items to the
collection at design time (using standard CollectionEditor class). I can
Add, remove items at design time, however all the items are lost after a
compilation.

public class MyComponent : Component{
Int16Collection int_col;

public Int16Collection IDs{
get{
return int_col;
}
set{
int_col = value;
}
}
}

Here's my custom collection class.

public class Int16Collection : CollectionBase {
public Int16 this[ int index ]{
get{
return( (Int16) List[index] );
}
set{
List[index] = value;
}
}
....
...

I dont understand how/why all the items added at design time disappear on
compilation of the form. Where are the items saved between design time and
compilation time?

any help much appreciated
 
G

Guest

I have been working with custom controls with collection properties for over a year now. Everytime I am working on one i am trying to make the designer correctly serialize the items. I even decompiled all the serialization classes and analyzed how they work thinking I might find a way. I couldn't... Therefore, everytime I need it, i create a custom codedom serializer and I serialize the collection items myself. I wrote a DeepSerialize routine that I pass for each Item[x] so that all it's properties are correctly serialized. Even if the Item has a collection as property itself (for instance I have a control with pages and each page can have multiple items) they are serialized. So, the answer so far is: do it yourself.
What you can accomplish however, with the use of a TypeConverter is create the constructor for the collection similar to how the controls are added to their parent. But then again, if you have a lot of properties for each item that won't work...
 
C

Claes Bergefall

Hmm, that should work
I noticed that IDs property is Read/Write, Perhaps that's
the reason the attribute doesn't work in your case.

/claes


niyad said:
Still the same. I had tried that before.

Claes Bergefall said:
Add the following to your IDs property
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

/claes


niyad said:
hi
On a form ive added my custom component (MyComponent) which has a property
(IDs in the expample code) exposing a collection of int16. I see this
property in the PropertyGrid and I dont have trouble adding items to the
collection at design time (using standard CollectionEditor class). I can
Add, remove items at design time, however all the items are lost after a
compilation.

public class MyComponent : Component{
Int16Collection int_col;

public Int16Collection IDs{
get{
return int_col;
}
set{
int_col = value;
}
}
}

Here's my custom collection class.

public class Int16Collection : CollectionBase {
public Int16 this[ int index ]{
get{
return( (Int16) List[index] );
}
set{
List[index] = value;
}
}
....
...

I dont understand how/why all the items added at design time disappear on
compilation of the form. Where are the items saved between design time and
compilation time?

any help much appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top