how to change the item type display name in pop up collectioneditor

C

colin

Hi,
I have my property editor wich uses the pop up
collection editor for arrays etc,
but i have had to use a generic wrapper for the
elements in some types of collections.

although the editing actually works well,
and I can set the name ok in the primary property editor,
this makes a mess of the type name displayed in the pop up colection editor,
is there any way to change this ?

eg

class UWrapper<T>:List<T>
{
T Item{get{}}
}
UWrapper<Vector> collection;

the collection gets displayed as UWrapper'1
in the title and also the same in the edit value box.

but I would like this to be displayed as Vector or whatever
the generic type is im using.

Ive tried all sorts of attributes and classes but cant seem
to figure this onr out. the pop up colection editor
seems to work diferently than the property editor.


Many thanks
Colin =^.^=
 
C

colin

oops that shoud of been

class UCollection<T>:List<T>
{
T Item{get{}}
}

public class UWrapper<T>
{
}

UCollection<UWrapper<Vector>> collection;
 
M

Marc Gravell

Well, the following shows a way to customise the title (there may be
simpler ways, but I can't see them). Obviously you'd need to put some
different code in around ".Text = " to put in some code (presumably
using reflection and GetGenericTypeDefinition() etc) to put in a more
useful name...

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;

class CollectionEditor<T> : CollectionEditor
{
protected override CollectionForm CreateCollectionForm()
{
CollectionForm form = base.CreateCollectionForm();
form.Text = "Here be thee " + typeof(T).Name + "s";
return form;
}
public CollectionEditor() : base(typeof(T)) {}
}

class Foo
{
[Editor(typeof(CollectionEditor<Bar>), typeof(UITypeEditor))]
public Collection<Bar> Bars { get; private set; }

public Foo() {Bars = new Collection<Bar>();}
}
class Bar
{
public string Name { get; set; }
public int Age { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form
{
Controls = {
new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObject = new Foo()
}
}
});
}
}
 
C

colin

ok cool, thanks il give that a go, ive also been
trying to see what it uses by looking at
the .net reflector tool.

I did try and fool it giving it the type of the inner type

(i found it picks this type by looking for the type of the
Item property wich is the this[int index};)

but giving the data as a list of wrappers, but although the title was
correct,
it complained it couldnt convert the items to the wrapper type
when it tried to save it.

I tried using converter, custom descriptor, and descriptor provider,
but it never seems to hit any of the functions that can be overiden
that i could use

Colin =^.^=
 
C

colin

well that does indeed set the title nicely,
however I doscovered you cant put template parameter inside attributes,

so this didnt work :-

public class UListWrap<T>
{
[Editor(typeof(UCollectionEditor<T>), typeof(UITypeEditor))]
// ^-- error CS0416:
// 'type parameter': an attribute argument cannot use type parameters
public class UDescWrap : CustomTypeDescriptor
{
UColection<T> data;
... GetProperties() etc ...
}
}

I cant avoid the generic becuase i cant see any other way of getting
the type name at the time CreateCollectionForm is called.

also the type of the object still apears as the typename of the wrapped
object
wich would be something like "UWrap'1"

oh and I also have to generate the generic type in reflection
(wich ive manged to do ok)
so i have no clue what T might be at run time.

I dont need to use any of this for simple types like int anyway,
but some of the items in some lists need to be able to get
the list of properties from the class that contains the list.

im thinking instead of using my own wrapper that I could use
typedescriptorprovider for each object in the list
isntead of aplying it to a type, wich aplies to al objects of that type.

interestingly i was looking at the arraycolectioneditor in reflector
and it uses a fairly simple wrapper like mine.

its just the type name that apears obscure thats driving me mad
and thats only in the pop up colection editor,
the rest works ok, am I just being too pedantic ?

many thanks again
Colin =^.^=

ps if i ever get this sorted il post the complete solution
somewhere like code project ...

Marc Gravell said:
Well, the following shows a way to customise the title (there may be
simpler ways, but I can't see them). Obviously you'd need to put some
different code in around ".Text = " to put in some code (presumably using
reflection and GetGenericTypeDefinition() etc) to put in a more useful
name...

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;

class CollectionEditor<T> : CollectionEditor
{
protected override CollectionForm CreateCollectionForm()
{
CollectionForm form = base.CreateCollectionForm();
form.Text = "Here be thee " + typeof(T).Name + "s";
return form;
}
public CollectionEditor() : base(typeof(T)) {}
}

class Foo
{
[Editor(typeof(CollectionEditor<Bar>), typeof(UITypeEditor))]
public Collection<Bar> Bars { get; private set; }

public Foo() {Bars = new Collection<Bar>();}
}
class Bar
{
public string Name { get; set; }
public int Age { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form
{
Controls = {
new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObject = new Foo()
}
}
});
}
}
 
M

Marc Gravell

Darn, you beat me to it... I was going to suggest
TypeDescriptionProvider ;-p

As for pedantic - well: it is your system; does the effort justify it?
Only you can answer that...

Marc
 
C

colin

Marc Gravell said:
Darn, you beat me to it... I was going to suggest
TypeDescriptionProvider ;-p

As for pedantic - well: it is your system; does the effort justify it?
Only you can answer that...

Marc

well the typedescriptorprovidor works for the pop up class but it breaks the
property window
i get null being passed to my MyPropertyDesripptor::getvalue(object
component)

as for being pedantic this has now become a challange of wits and
im not prepared to give in !!!

maybe the learning process may yeild some value.

thanks
Colin =^.^=
 
C

colin

I think i might of found a better way now,
I use type delegator wich wraps my array type
and overides GetElementType to return
another typedelegator wich wraps my element type
and overides Name to return the inner type.

im not sure how this extends to other collections yet,
or how useful it will be to use in place of a whole collection
of classes.

but it is just one class with a couple of simple functions.

Colin
 

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