Usercontrol exposing an object array from a contained control

J

Johnny Jörgensen

I've got a usercontrol containing a ListView control. I would like to
comehow expose the Listviews Item and Column collections so that I can
access them using

MyUserControl.Items.Add("NewItem");

instead of like I'm doing now:

MyUserControl.ListView.Items.Add("NewItem");

I've named the contained ListView "ListView" in order to avoid having to
write:

MyUserControl.ListView1.Items.Add("NewItem");

I tried doing:

public ListView.ListViewItemCollection Items

{

get { return this.ListView.Items; }

set { this.ListView.Items = value; }

}

....like with any other property, but it doesn't work.

Furthermore, The Items will always be added from code, but I would of
course like to be able to add the Columns to the Column collection from the
designer.

Can anybody tell me if this is possible without too much trouble...



Cheers,

Johnny J.
 
M

Marc Gravell

What fails? Unless I am mistaken you just need to remove the setter -
this property only needs a "get".

Don't worry - you will still be able to edit it; it comes down to
*editing* the items (Add, Remove, Clear, etc), not reassigning the
entirecollection. You don't need a "set" for this.

As for the IDE editor - if it isn't already specified on the class (it
isn't always), then you need the right EditorAttribute on the
property. The following (courtesy of reflector) might help:
[Localizable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ColumnHeaderCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[MergableProperty(false)]
public ColumnHeaderCollection Columns {get {...}}

[MergableProperty(false)]
[Editor("System.Windows.Forms.Design.ListViewItemCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
public ListViewItemCollection Items {get {...}}





As for the editor
 
J

Johnny Jörgensen

Thanks a lot Marc

I didn't even try it without the Set. But it works just like you said.

Cheers,
Johnny J.





Marc Gravell said:
What fails? Unless I am mistaken you just need to remove the setter - this
property only needs a "get".

Don't worry - you will still be able to edit it; it comes down to
*editing* the items (Add, Remove, Clear, etc), not reassigning the
entirecollection. You don't need a "set" for this.

As for the IDE editor - if it isn't already specified on the class (it
isn't always), then you need the right EditorAttribute on the property.
The following (courtesy of reflector) might help:
[Localizable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ColumnHeaderCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[MergableProperty(false)]
public ColumnHeaderCollection Columns {get {...}}

[MergableProperty(false)]
[Editor("System.Windows.Forms.Design.ListViewItemCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
public ListViewItemCollection Items {get {...}}





As for the editor
Johnny Jörgensen said:
I've got a usercontrol containing a ListView control. I would like to
comehow expose the Listviews Item and Column collections so that I can
access them using

MyUserControl.Items.Add("NewItem");

instead of like I'm doing now:

MyUserControl.ListView.Items.Add("NewItem");

I've named the contained ListView "ListView" in order to avoid having to
write:

MyUserControl.ListView1.Items.Add("NewItem");

I tried doing:

public ListView.ListViewItemCollection Items

{

get { return this.ListView.Items; }

set { this.ListView.Items = value; }

}

...like with any other property, but it doesn't work.

Furthermore, The Items will always be added from code, but I would of
course like to be able to add the Columns to the Column collection from
the designer.

Can anybody tell me if this is possible without too much trouble...



Cheers,

Johnny J.
 

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