PC Review


Reply
Thread Tools Rate Thread

Collection property & Design Time support

 
 
tomsiw@gmail.com
Guest
Posts: n/a
 
      23rd Apr 2007
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

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Apr 2007
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


 
Reply With Quote
 
tomsiw@gmail.com
Guest
Posts: n/a
 
      23rd Apr 2007
On 23 Kwi, 12:32, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Apr 2007
http://msdn2.microsoft.com/en-us/lib...ioneditor.aspx

> This editor can edit collections that have an Item property


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


 
Reply With Quote
 
Tomek
Guest
Posts: n/a
 
      24th Apr 2007
On 23 Kwi, 14:13, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> http://msdn2.microsoft.com/en-us/lib...ntmodel.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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      24th Apr 2007
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; }
}
}


 
Reply With Quote
 
Tomek
Guest
Posts: n/a
 
      25th Apr 2007
On 24 Kwi, 13:04, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      25th Apr 2007
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


 
Reply With Quote
 
Tomek
Guest
Posts: n/a
 
      26th Apr 2007
On 25 Kwi, 12:29, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CompositeControl Design Time Error (Could not add List<> property tothe collection) Matt Winward Microsoft ASP .NET 0 20th Mar 2008 05:32 PM
Adding items in collection property during design mode Saimeera Microsoft ASP .NET 0 10th May 2006 12:34 PM
can a custom control have an image property that can be set in design time from the property browser? news.austin.rr.com Microsoft Dot NET Compact Framework 3 3rd Mar 2005 02:08 AM
Design time property support for a collection Edward Diener Microsoft Dot NET Framework 8 29th Dec 2003 11:08 PM
Item property of Collection classes not support by CF Henrik Bach Microsoft Dot NET Compact Framework 3 23rd Sep 2003 10:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 PM.