Re: Adding to custom collection property in designer

From: Bob Dankert (bob_at_nospamnvsn-it.com)
Date: 09/24/04


Date: Fri, 24 Sep 2004 08:15:53 -0500

Thanks a lot for all your help. I played around with your stuff and
switched around some of my code with yours and was able to determine that my
collection is the problem. If I use all of your code except substitute my
collection in for yours, then the code will no longer work -- it does not
save columns when I add them with the column editor.

I will paste my code below as I am curious what might be wrong with this?

public class MyListBoxColumnCollection : IList
{
private ArrayList myList;
public MyListBoxColumnCollection()
{
myList = new ArrayList();
}
public virtual void AddRange(MyListBoxColumn[] values)
{
foreach (MyListBoxColumn ch in values)
this.myList.Add(ch);
}
#region IList Explicait Members
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyListBoxColumn this[int index]
{
get
{
if (index > this.myList.Count-1)
{
throw new ArgumentOutOfRangeException("MyListBoxColumnCollection.Items
index",index,"Index out of range, please specify a valid value");
}
else
return (MyListBoxColumn)this.myList[index];
}
set
{
if (index > this.myList.Count-1)
{
throw new ArgumentOutOfRangeException("MyListBoxColumnCollection.Items
index",index,"Index out of range, please specify a valid value");
}
else
this.myList[index] = value;
}
}
public void Insert(int index, MyListBoxColumn value)
{
if (index > this.myList.Count-1)
{
throw new ArgumentOutOfRangeException("MyListBoxColumnCollection.Items
index",index,"Index out of range, please specify a valid value");
}
this.myList.Insert(index, value);
}
public void Remove(MyListBoxColumn value)
{
if (this.myList.Contains(value))
this.myList.Remove(value);
}
public bool Contains(MyListBoxColumn value)
{
return this.myList.Contains(value);
}
public int IndexOf(MyListBoxColumn value)
{
return this.myList.IndexOf(value);
}
public int Add(MyListBoxColumn value)
{
return this.myList.Add(value);
}
#endregion
#region ICollection Members
public bool IsSynchronized
{
get
{
return this.myList.IsSynchronized;
}
}
public int Count
{
get
{
return this.myList.Count;
}
}
public void CopyTo(Array array, int index)
{
this.myList.CopyTo(array, index);
}
public object SyncRoot
{
get
{
return this.myList.SyncRoot;
}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return this.myList.GetEnumerator();
}
#endregion
#region IList Members
public bool IsReadOnly
{
get
{
return false;
}
}
object System.Collections.IList.this[int index]
{
get
{
return null;
}
set
{
}
}
void System.Collections.IList.Insert(int index, object value)
{
}
void System.Collections.IList.Remove(object value)
{
}
bool System.Collections.IList.Contains(object value)
{
return false;
}
int System.Collections.IList.IndexOf(object value)
{
return 0;
}
int System.Collections.IList.Add(object value)
{
return 0;
}
public void Clear()
{
this.myList.Clear();
}
public void RemoveAt(int index)
{
if (index > this.myList.Count-1)
{
throw new ArgumentOutOfRangeException("MyListBoxColumnCollection.Items
index",index,"Index out of range, please specify a valid value");
}
this.myList.RemoveAt(index);
}
public bool IsFixedSize
{
get
{
return false;
}
}

#endregion
}

Thanks a ton,

Bob Dankert

""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:7qGbDSgoEHA.2640@cpmsftngxa06.phx.gbl...
> Hi Bob,
>
> Also, we may explicitly specify the CollectionEditor to use certain
> constructor through inherit the default CollectionEditor, then override
> CollectionEditor.CreateInstance method and explicitly call the
> constructor.
> Like this:
> public class MyCollectionEditor: CollectionEditor
> {
> private Type[] types;
> public MyCollectionEditor(Type type): base(type)
> {
> types=new Type[]{typeof(MyListBoxColumn)};
> }
>
> protected override Type[] CreateNewItemTypes()
> {
> return types;
> }
>
> protected override object CreateInstance(Type itemType)
> {
> if(itemType==typeof(MyListBoxColumn))
> {
> MyListBoxColumn mlbc=new MyListBoxColumn(1);
> return mlbc;
> }
> return base.CreateInstance(itemType);
> }
> }
>
> Thank you for your patience and cooperation. If you have any questions or
> concerns, please feel free to post it in the group. I am standing by to be
> of assistance.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
>



Relevant Pages


Quantcast