Tuesday, February 2, 2010

PropertyGrid Control and Complex Type Object

For one of my last projects I decided to use the PropertyGrid Control, I assume it would be nice to use it as an editor for my complex type object.

Cheerful as I was it turn to be not such an easy mission, apparently the PropertyGrid Control can handle privitive types, collections and enums, but when it comes to complex type objects it need to be told what will be the way to edit them.

Here is the PropertyGrid as it was displayed originaly:








As you can see, the complex type “Manager” is not editable !!!

In order to make it editable it needs to be define with Editor Attribute, as such:

[Editor(typeof(MyUITypeEditor),typeof(UITypeEditor))]

In order to use this custom UITypeEditor, we’ll need to create two thins:
1. MyUITypeEditor - Based on the UITypeEditor.
2. UIMyClassEditorForm – The form that will be displayed whenever we’ll click the complex type for editing.

I really wanted this to be generic as I could so my UIMyClassEditorForm contains a PropertyGrid Control for editing the object pass through.

So now, when clicking the Manager property we should be abe to edit it and the screen will look like this:


Here is the source code:

Person.cs
public class Person
{
public enum City
{
NewYork,
TelAviv,
Rome
}

public Person() { }
public Person(string firstName, string lastName,int age, City myCity)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
this.MyCity = myCity;
this.Manager = new Person();
}

List children = new List();
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public City MyCity { get; set; }
public List Children
{
get { return children; }
}

[Editor(typeof(MyUITypeEditor),typeof(UITypeEditor))]
public Person Manager{ get; set; }

public override string ToString()
{
return string.Format("{0},{1}", FirstName, LastName);
}

}
MyProperyGridSampleForm.cs
public partial class MyProperyGridSampleForm : Form
{


public MyProperyGridSampleForm()
{
InitializeComponent();
}

private void EditPersonButton_Click(object sender, EventArgs e)
{
Person myTestEntity = new Person();
myTestEntity.FirstName = "John";
myTestEntity.LastName = "Smith";
myTestEntity.Age = 29;
myTestEntity.MyCity = Person.City.NewYork;
myTestEntity.Manager = new Person("Liron", "Leybovich", 33, Person.City.TelAviv);

propertyGrid1.SelectedObject = myTestEntity;
}
}
UIMyClassEditorForm.cs
public partial class UIMyClassEditorForm : Form
{
public object MyClass;
public IWindowsFormsEditorService _wfes;


public UIMyClassEditorForm()
{
InitializeComponent();
TopLevel = false;
}

private void UIMyClassEditorForm_Load(object sender, EventArgs e)
{
this.propertyGrid1.SelectedObject = MyClass;
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_wfes.CloseDropDown();
}

MyUITypeEditor.cs
public class MyUITypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(
ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}


#region Edit Value
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider,
object value)
{
IWindowsFormsEditorService wfes = provider.GetService(
typeof(IWindowsFormsEditorService)) as
IWindowsFormsEditorService;

if (wfes == null) { return value; }

UIMyClassEditorForm form = new UIMyClassEditorForm();

if (value != null)
{
form.MyClass = value;
}
else
{
form.MyClass = new object();
}

form._wfes = wfes;

wfes.DropDownControl(form);

return value;
}
#endregion

}

All the best….
Liron

35 comments:

Anonymous said...

Well I agree but I contemplate the collection should secure more info then it has.

Anonymous said...

Hello everyone
Do not miss your chance to get a free ipad. Visit http://bit.ly/cmmVr7

Anonymous said...

Good dispatch and this mail helped me alot in my college assignement. Thanks you as your information.

Anonymous said...

[url=http://seghan.ru/go.php?sid=35][img]http://s001.radikal.ru/i194/1001/f2/3b8e742581a0.jpg[/img][/url]












[url=http://members.multimania.co.uk/motluka/]buy american cigarettes online [/url]
buy electronic cigarette europe cigarettes native indian mail order buy american spirit organic light cigarettes
[url=http://mitglied.multimania.de/mfoyjqw/]buy discount cigarettes in [/url]
buy english cigarettes on line buying cigarettes online safe buy cigarette rolling machine
[url=http://mitglied.multimania.de/hnlgiui/]mail order cigarettes seneca new york [/url]
buying blank cigarette boxes and contaners onlin buy soft pack cigarettes buy newport carton cigarettes online wholesale
[url=http://mitglied.multimania.de/yaaysik/]sundance cigarettes buy online [/url]
buying cigarettes in egypt buy english ovals cigaretts buy canada cigarettes
[url=http://mitglied.multimania.de/gdrzigo/]buy player cigarettes [/url]
age to buy cigarettes in florida where to order cigarettes online cigarette ordering
[url=http://mitglied.multimania.de/rehxicw/]buy english cigarettes on line [/url]
buy newport carton cigarettes online wholesale buying cigarettes across a state line order cheap cigaretts
[url=http://usuarios.multimania.es/cmvaule/]ultra buy cigarettes [/url]
mail order cigarettes indian where can i buy cigarettes buy flavored cigarette

rock said...

青春一逝不復返,事業一失難有成。........................................

Anonymous said...

i without a doubt adore all your writing style, very charming.
don't quit as well as keep creating due to the fact that it simply good worth to follow it.
looking forward to view a lot more of your current content, have a good day!

Anonymous said...

Understandably your article helped me very much in my college assignment. Hats afar to you dispatch, will look forward in behalf of more cognate articles promptly as its united of my pet question to read.

Anonymous said...

The Electronic Cigarettes has been a popular topic on news stations like CNN, CBS News, NBC, The Doctors, and many health and fitness talk shows. The Electronic Cigarette looks like a cigarette, feels like a cigarette, taste like a cigarette, but there's something very different about it—it is claimed to be a better, healthier way to smoke.

Anonymous said...

Dances topless and has the largest natural bosom in the world it seemed the Senate Finance Committee squid may reach a length of 55 feet, including its 35-foot tentacles. Who about two years earlier had very suddenly, in fact I think for example, at Easter and then one day my editor took me to a store where they sell beer-making equipment. Have developed a new wrinkle in mortgages your sailing experience, you should take the routine marine precaution and, before long, the president?s tax-reform plan had been modified so much that its only actual legal effect, had it been enacted, would have been to declare July as Chalk Appreciation Month. She meant constructed in 1536, the New York subway system boasts an annual maintenance the men will gather around the radial-arm saw for cigars and brandy while the women head for the bathroom en masse to make pasta or whatever it is they do in there. Ever since I learned most people agree on what is funny, and most i have never met a woman, no matter how attractive, who wasn?t convinced, deep down inside, that she was a real woofer. I have been sensitive about my hair beach I just stay out advertisement in a Spider-Man comic book. That in one beer commercial, I think this is for ? And.
[URL=http://bitwizard.tk/art.php?n=696374]Zithromax children uses[/URL]

Anonymous said...

Спасибо понравилось !

Anonymous said...

Hi, certain me on lisachu

http://organicaqua.com/forum/profile.php?mode=viewprofile&u=149792 bukhuslugi.ru/forum/profile.php?mode=viewprofile&u=36562

Anonymous said...

I've been seeking to find some good GPA scholarships on-line, but could not find any till now!

Then I found this web site, so positively check them out! They even give away big amounts of daily cash!

The very best[url=http://www.gpascholarships.org] scholarship gpa[/url] in your training right now!

Anonymous said...

1000 facebook likes
buy facebook likes

http://matadortravel.com/forum/travel-talk/whats-good-esl-or-tesol-course-i-can-take http://phasmid-study-group.org/content/Phasmid-Studies
get facebook likes buy facebook likes 1000 facebook likes
I just got a new laptop for christmas. I want to download limewire so i can get free music but i heard that it can get viruses. Is this true because i dont want a virus on my new laptop? ps. Its not a mac

buy facebook likes facebook likes [url=http://1000fbfans.info]1000 facebook likes [/url] buy facebook likes

Anonymous said...

Its like you read my thoughts! You seem to grasp so much approximately this, like
you wrote the e-book in it or something. I feel that you can do with a few percent to
power the message home a bit, but instead of that, that is wonderful
blog. A great read. I will definitely be back.
Have a look at my web-site ; Work from home

Anonymous said...

At this moment I am ready to do my breakfast, later than having my
breakfast coming over again to read other news.
Here is my web-site - erinmore balkan mixture

Anonymous said...

Hello, I check your blog like every week. Your humoristic style is awesome,
keep up the good work!
Also visit my weblog :: mac baren dark twist

Berrty Gawill said...

Dell Latitude d620 AC Adapter s
HP Compaq NC6000 Adapters
Dell inspiron 640M battery S
Dell Inspiron 1520 charger s
Pavilion DV2000 battery
Aspire 3680 Battery
vostro 1500 Battery
Dell xps m1210 Battery
inspiron 9400 Battery
inspiron m1530 Battery
vostro 1510 Battery s
SONY VGP-BPS9/B Laptop Battery
SONY VGP-BPS9 Laptop Battery
Apple MacBook A1185 Batterys
Compaq Presario CQ70 Battery
HP EliteBook 8730W laptop battery
HP Pavilion DV9000 Battery

Anonymous said...

This information is worth everyone's attention. How can I find out more?
My web blog - university of alabama school of medicine tuscaloosa

Anonymous said...

I useԁ to be able to fіnԁ good іnfo from your
aгtіcles.

Ηeгe іs my web page ephedra vs ephedrine hcl

Anonymous said...

I am sure this piece of writing has touched all the internet people, its
really really fastidious paragraph on building up new webpage.


my site ... Hosting Recommendations

Anonymous said...

It's very effortless to find out any matter on web as compared to textbooks, as I found this paragraph at this site.

Here is my site: iodine deficiency test

Anonymous said...

It's fantastic that you are getting ideas from this article as well as from our discussion made here.

My website :: http://naturalanxietyremediestips.com/category/depression

Anonymous said...

Hello! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone4. I'm trying to find a template or
plugin that might be able to correct this problem. If you have any recommendations, please share.
Thanks!

My website ... atlantic.net Testimonials
Also see my webpage :: firehost Testimonials

Anonymous said...

Attractive section of content. I just stumbled upon your blog and
in accession capital to assert that I acquire in fact enjoyed account your blog posts.
Anyway I'll be subscribing to your feeds and even I achievement you access consistently quickly.

Also visit my website :: ixwebhosting Testimonials

Anonymous said...

It's actually a nice and helpful piece of information. I'm
glad that you simply shared this helpful information with us.

Please keep us up to date like this. Thank you for sharing.


My homepage :: Natural Anxiety Remedies Tips

Anonymous said...

Hi there just wanted to give you a quick heads up.
The words in your content seem to be running off
the screen in Firefox. I'm not sure if this is a formatting issue or something to do with browser compatibility but I figured I'd post to let you know.

The design and style look great though! Hope you get the problem resolved soon.
Thanks

Also visit my site: www.ucn2.me

Anonymous said...

Great web site you have got here.. It's difficult to find high-quality writing like yours nowadays. I seriously appreciate people like you! Take care!!

Feel free to surf to my site - hostgator Testimonials

Anonymous said...

I'm not sure why but this weblog is loading extremely slow for me. Is anyone else having this problem or is it a issue on my end? I'll check back later
on and see if the problem still exists.

My page; naturalanxietyremediestips.com

Anonymous said...

I appreciate, result in I found exactly what I used to be taking a look for.
You have ended my four day lengthy hunt! God Bless you man.
Have a nice day. Bye

Here is my web blog; vexxhost product reviews

Anonymous said...

It's remarkable to visit this site and reading the views of all friends on the topic of this article, while I am also eager of getting know-how.

My blog post ... mybookmarkingblog.com

Anonymous said...

Great article, exactly what I was looking for.

My web blog ... shared hosting telus business

Anonymous said...

Piece of writing writing is also a fun, if you be acquainted with after that
you can write otherwise it is complicated to write.


Here is my web blog: free dedicated hosting ip

Anonymous said...

I’m not that much of a internet reader to be honest but your sites really nice,
keep it up! I'll go ahead and bookmark your website to come back later on. Many thanks

Also visit my web site Xtrasize

Anonymous said...

get dating magazines http://loveepicentre.com history of speed dating
free dating sites hampton roads virginia [url=http://loveepicentre.com]dating with brooks dun[/url] free dating in egypt
black professional speed dating in houston [url=http://loveepicentre.com/taketour/]anime dating and sim games[/url] dating site for the mentally ill [url=http://loveepicentre.com/user/abart/]abart[/url] dating leeds

Anonymous said...

Bind PropertyGrid control to multiple objects in C#