Using CollectionBase

J

Juan Martinez

Hello!!

I have created my own collection :

[TypeConverter(typeof(MyCollectionConverter))]
public class MyCollection
{

private Color color1 = Color.White;
private Color color2 = Color.Black;
private int value1 = 0;
private int value2 = 0;
private bool blink = false;
private bool visible = true;
private string nname = string.Empty;

[Browsable(true)]
public Color Color1
{
get { return color1; }
set { color1 = value; }
}

[Browsable(true)]
public Color Color2
{
get { return color2; }
set { color2 = value; }
}

[Browsable(true)]
public int Value1
{
get { return value1; }
set { value1 = value; }
}

[Browsable(true)]
public int Value2
{
get { return value2; }
set { value2 = value; }
}

[Browsable(true)]
public bool Blink
{
get { return blink; }
set { blink = value; }
}

[Browsable(true)]
public bool Visible
{
get { return visible; }
set { visible = value; }
}

[Browsable(true)]
public string Name
{
get { return name; }
set { name = value; }
}

public MyCollection()
{
}
}

public class MyCollectionConverter : CollectionBase
{
public MyCollectionConverter ()
{
}

public int Add(MyCollectione)
{
return this.InnerList.Add(e);
}

public void AddRange(MyCollection[] es)
{
this.InnerList.AddRange(es);
}

public void Remove(MyCollectione)
{
InnerList.Remove(e);
}

public new void RemoveAt(int index)
{
InnerList.RemoveAt(index);
}

public bool Contains(MyCollection e)
{
return InnerList.Contains(e);
}

public MyCollection this[int index]
{
get { return (MyCollection)this.InnerList[index]; }
set { this.InnerList[index] = value; }
}
}

and in a click event i add some elements to the collection like this:

MyCollectionConverter myCollectionConverter = new MyCollectionConverter
();

for ( int i = 0; i < 5; i++ )
{
MyCollection myCollection = new MyCollection();
myCollection.Name = "Element " + i.ToString();

myCollectionConverter.Add(myCollection);
}

and then i show myCollectionConverter in a PropertyGrid, when i open the
Collection editor i can see all the elements.

My question is how can i disable the add and remove buttons in the
Collection editor this because i don't want that a user can add more
elements.

This is possible?

Somebody know how can i do that?

Regards,
Alberto Martinez
 
I

intrader

Juan said:
Hello!!

I have created my own collection :

[TypeConverter(typeof(MyCollectionConverter))]
public class MyCollection
{

private Color color1 = Color.White;
private Color color2 = Color.Black;
private int value1 = 0;
private int value2 = 0;
private bool blink = false;
private bool visible = true;
private string nname = string.Empty;

[Browsable(true)]
public Color Color1
{
get { return color1; }
set { color1 = value; }
}

[Browsable(true)]
public Color Color2
{
get { return color2; }
set { color2 = value; }
}

[Browsable(true)]
public int Value1
{
get { return value1; }
set { value1 = value; }
}

[Browsable(true)]
public int Value2
{
get { return value2; }
set { value2 = value; }
}

[Browsable(true)]
public bool Blink
{
get { return blink; }
set { blink = value; }
}

[Browsable(true)]
public bool Visible
{
get { return visible; }
set { visible = value; }
}

[Browsable(true)]
public string Name
{
get { return name; }
set { name = value; }
}

public MyCollection()
{
}
}

public class MyCollectionConverter : CollectionBase
{
public MyCollectionConverter ()
{
}

public int Add(MyCollectione)
{
return this.InnerList.Add(e);
}

public void AddRange(MyCollection[] es)
{
this.InnerList.AddRange(es);
}

public void Remove(MyCollectione)
{
InnerList.Remove(e);
}

public new void RemoveAt(int index)
{
InnerList.RemoveAt(index);
}

public bool Contains(MyCollection e)
{
return InnerList.Contains(e);
}

public MyCollection this[int index]
{
get { return (MyCollection)this.InnerList[index]; }
set { this.InnerList[index] = value; }
}
}

and in a click event i add some elements to the collection like this:

MyCollectionConverter myCollectionConverter = new MyCollectionConverter
();

for ( int i = 0; i < 5; i++ )
{
MyCollection myCollection = new MyCollection();
myCollection.Name = "Element " + i.ToString();

myCollectionConverter.Add(myCollection);
}

and then i show myCollectionConverter in a PropertyGrid, when i open the
Collection editor i can see all the elements.

My question is how can i disable the add and remove buttons in the
Collection editor this because i don't want that a user can add more
elements.

This is possible?

Somebody know how can i do that?

Regards,
Alberto Martinez
I am guessing here, and I hope it does not stop from someone more knowleable
answering. So my guess is that you may need to add a static variable to
keep track of class level state.
 
J

james.curran

My question is how can i disable the add and remove buttons in the
Collection editor this because i don't want that a user can add more
elements. <<

I'm a bit confused by all of this.

You have a class called "MyCollection" (which is not a collection, but
an object)
You have a class called MyCollectionConverter (which is not a
converter, but a collection).
You have a "Collection editor" which you don't explain at all.

Attached to some object is a button click event handle. This leads be
to infer that the "Collection editor " is a WIndows Form with buttons
on it.

Which means that the answer to your question has nothing to do with any
of the code you showed us, and everything to do with the code you did
not.

You want to hide a button. We need to see the button. What the button
does is irrelevant.
 

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