Collection with the correct object type on the Collection dialog

D

Daniel Jorge

Hi there,

I'm new to webControls and I'm having a doubt regarding collection into
the Control.
Well, I've built a webCustomControl (let's call it MyPrimaryControl
here) and it has a collection of custom objects (let's call it
"MyCustomItem").
I built a class called MyCustomItemCollection which implements IList,
ICollection and IStateManager interfaces. No inheritances!
I implemented all the mothods except the ones for ViewState management.
IndexOf, Add, Insert, Remover and so on are all there!

Now I get:
MyPrimaryControl.CutomItems = MyCustomItemCollection

When I use my primary control the property CustomItems (which is a
collection) open up the Help dialog and let me add several System.Object
objects into the collection. But yet, I can't edit any of them. I can't save
the items as well.

Does anybody know how to edit a collection with the correct object type,
make it editable at Design-Time and force it to save the items into the
object.

Thanks in advance.

Bye
 
L

lisa

Hi Daniel,

You don't say if you're doing this in VB or C#. This is one area where
doing it in C# will be easier.

The thing is, your CollectionEditor can only show the properties of the
items in the collection if the indexer of the collection is of the
item's type.

You might as well have inherited from ControlCollection or
CollectionBase. The thing is, when you implement IList, you need to
implement IList.Item. That's what the CollectionEditor picks up.

I did this in VB, and had to do it like this:

Private Property IList_Item(ByVal index As Integer) As Object _
Implements IList.Item
Get
Return Me.owner.Controls.Item(index)
End Get
Set(ByVal Value As Object)
Me.RemoveAt(index)
Me.AddAt(index, CType(Value, Tab))
End Set
End Property 'Item

Default Public ReadOnly Property Item(ByVal index As Integer) As Tab
Get
Return CType(Me.IList_Item(index), Tab)
End Get
End Property 'Item

See, IList requires that the implementation of Item be of type Object.
But that's no good for the CollectionEditor. So make the
implementation property be private, and then have the default public
property just a window into the implementation property. It's
incredibly dumb, but there you are.

In C#, it'd be like this:

object IList.Item(int index)
{
get
{
return this.owner.Controls[index];
}

set
{
this.RemoveAt(index);
this.AddAt(index, (Tab) value);
}
}

public Tab this[int index]
{
get
{
return (Tab) this.owner.Controls[index];
}
}

Hope that helps,
Lisa
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top