Collection property & Design Time support

T

tomsiw

Hello,

I am trying to create a user control which allows the user to enter a
collection of items at design-time. Code example:

public class MyControl : System.Windows.Forms.UserControl
{
....
MyItem [] itemList;

public MyItem [] ItemList
{
get { return itemList; }
set { itemList = value; }
}
....
}

public class MyItem
{
private string caption;

public string Caption
{
get { return caption; }
set { caption = value; }
}
}

I want to add design-time support for ItemList property. Currently in
design mode Visual Studio opens Collection Editor for ItemList
property
and I can define all items but after clicking OK items are not saved
(they just disappear). I tried to use
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
for ItemList property but it doesn't help. I also have implemented a
TypeConverter for ItemList which can make a convertion between
collection of items and string - but is also doesn't help. Maybe you
can
help me.

Regards,
Tomasz Siwarga
 
M

Marc Gravell

Try changing to a list/collection type rather than an array (it should
be read only). In 2.0 you could probably use

private readonly Collection<MyItem> myItems = new
Collection<MyItem>();
public Collection<MyItem> Items {get {return myItems;}}

Marc
 
T

tomsiw

Try changing to a list/collection type rather than an array (it should
be read only). In 2.0 you could probably use

private readonly Collection<MyItem> myItems = new
Collection<MyItem>();
public Collection<MyItem> Items {get {return myItems;}}

Marc


The project is created using .NET 1.1 and I have no chance to change
it to 2.0.
I tried to use collection derived from ArrayList and the problem still
remains.

my code looks like this:
private readonly MyItemCollection myItems;
public MyItemCollection Items { get {return myItems;} }

Maybe you know any working example for .NET 1.1 ...

Thanks for your help,
Regards,
Tom
 
M

Marc Gravell

T

Tomek

http://msdn2.microsoft.com/en-us/library/system.componentmodel.design...
This editor can edit collections that have an Itemproperty

Try adding a (typed) Item[int index] indexer to MyItemCollection (you
may need to make the default (untyped) indexer explicit on the
interface if you are inheriting).

Alternatively you can subclass CollectionEditor and override a few
things (perhaps CollectionItemType, but I can't remember off-hand)

Marc

I added typed indexer but the problem still exists. I will try to
subclass CollectionEditor.
Thanks for suggestions,
Tom
 
M

Marc Gravell

I don't have my 1.1 tools "on hand" (and can't be bothered to csc),
but the following might work:

using System;
using System.Windows.Forms;
using System.Collections;

static class Program {
static void Main() {
using (Form f = new Form())
using (PropertyGrid pg = new PropertyGrid()) {
pg.Dock = DockStyle.Fill;
f.Controls.Add(pg);
pg.SelectedObject = new MyOuterItem();
Application.Run(f);
}
}
}
public class MyOuterItem {
private readonly MyDataCollection items = new MyDataCollection();
public MyDataCollection Items { get { return items; } }
}
public class MyDataItem {
public override string ToString() {
return Name;
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private int id;
public int Id {
get { return id; }
set { id = value; }
}
}
public class MyDataCollection : CollectionBase {
public MyDataItem this[int index] {
get { return (MyDataItem)List[index]; }
set { List[index] = (MyDataItem)value; }
}
}
 
T

Tomek

I don't have my 1.1 tools "on hand" (and can't be bothered to csc),
but the following might work:
.......

Marc,
Your example works fine. I get the same functionality in my code when
I changed ArrayList to CollectionBase. But one more problem still
exists - serialization. After I define collection of items using
property grid in design mode - it should be saved in .resx file or
in .cs file so that I can use it in runtime (just like any other
property of my user control). As I mentioned before I implemented
TypeConverter for MyDataCollection. Designer is using this converter
by calling method ConvertTo but it never calls ConvertFrom. I also set
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)].
But collection of items which now - thanks to your help - can be
defined in desing time using propery grid is never serialized.
Mabye this is also a piece of cake for you :)
Regards,

Tom
 
M

Marc Gravell

The serialization I'm not sure about; however, from what I can see it
must be a: marked serializable, and to use TypeConverter the converter
must also return true from CanConvertFrom and CanConvertTo (for
string).

Marc
 
T

Tomek

The serialization I'm not sure about; however, from what I can see it
must be a: marked serializable, and to use TypeConverter the converter
must also return true from CanConvertFrom and CanConvertTo (for
string).

Marc

Marc,

I joined all your suggestions all togother and now I have exactly what
I wanted to have. Thank you for your help.
Best regards,

Tom
 

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