PropertyGrid / CollectionEditor problem...

K

Keith Degrace

I am having a bit of a problem with the PropertyGrid.
I've search all over and haven't found my answer yet...

Here's the scenario...

I have the following classes...

Control: Abstract class that represents a control. I
have concrete classes that derive this class for all
typical types of controls. (Button, ListBox, ect..)

ControlCollection: A collection of Control objects.

Window: Typical window. It has a ControlCollection
member that holds all the concrete controls of this
window.

I have all this setup to be browsable in a PropertyGrid
and everything works great except for one minor thing...

When I open up the CollectionEditor for the Controls
property of my window and click the 'Add' button, I get
the expected error that it can't create an instance of
the abstract class Control.

Is it possible to somehow override that 'Add' button to
maybe display a dialog where the user would be able to
choose what type of Control that will be added? or maybe
even completly removing the 'Add' and 'Remove' buttons
altogether?

Thanks in advance,
Keith Degrace
 
B

Bob Powell [MVP]

You need to create a collection class for a fully implemened version of the
object you're storing. Not for the abstract class.

Derive a fully implemented base class for your other classes from the
abstract class or perhaps consider using an interface instead of an abstract
class.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
K

Keith Degrace

Thanks for the reply, but it wasn't the answer I am
looking for.

I have a full set of controls: Button, ListBox,
ComboBox... The list goes on. I am not really interested
in keeping a list for each type in my Window class. And
making the base Control class concrete is not an option
either since I don't want the user to be able to create
instances of a Control object.

The solution I am looking for is a way to intercept
that 'Add' button so that I can display a list of
concrete controls that the user can chooser from. And I
would like to do this without having to rewrite my own
CollectionEditor.

Keith
 
K

Keith DeGrace

Figured it out. This little piece of code did the
trick. By doing this, a dropdown arrow appear on
the 'Add' button of the CollectionEditorForm with the
types that I specified in the CreateNewItemTypes method.

public class ControlCollectionEditor : CollectionEditor
{
public ControlCollectionEditor(Type type) : base(type)
{
}

protected override Type[] CreateNewItemTypes()
{
Type[] newItemTypes = new Type[2];

newItemTypes[0] = typeof(Button);
newItemTypes[1] = typeof(ListBox);
...

return newItemTypes;
}
}

Keith
 

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