472,122 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

PropertyGrid - how to display a string array as dropdown property


I am using a property grid to display a classes properties, all is OK for
single value properties but i need to display a list of strings for the user
to select from and update the property value based on the selection.
A Drop down list in the property grid would be ideal.
For instance - like the StartPosition property of a form in the IDE.

I think this would be a simple and often used procedure but i have not been
able to locate an example of the code required, can anyone advise?

Dec 15 '06 #1
4 35374
Normally this would be done via the type-converter of the property-type
(string in this case), which you obviously can't change in this
scenario. You could encapsulate the string into a simple class, and set
the converter for that, otherwise just set the converter for the
property:

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class MyTest
{
private int test1;
public int Test1
{
get { return test1; }
set { test1 = value; }
}
private string test2;
[TypeConverter(typeof(StringListConverter))]
public string Test2
{
get { return test2; }
set { test2 = value; }
}
}
public class StringListConverter : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true; // display drop
}
public override bool
GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true; // drop-down vs combo
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
// note you can also look at context etc to build list
return new StandardValuesCollection(new string[] { "abc",
"def", "ghi" });
}
}
class Program
{
static void Main()
{
using (Form f = new Form())
using(PropertyGrid pg = new PropertyGrid())
{
pg.Dock = DockStyle.Fill;
pg.SelectedObject = new MyTest();
f.Controls.Add(pg);
Application.Run(f);
}
}
}

Dec 15 '06 #2

Thanks, works fine, I would like to extend my use of the Property grid
to include objects within objects that would also be viewable/updateable
within the one property grid.
Can you point me in the direction of tutorial type documentation for
property grid, the help doc explanations are difficult for me to grasp?

rgds,Steve

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
Normally this would be done via the type-converter of the property-type
(string in this case), which you obviously can't change in this
scenario. You could encapsulate the string into a simple class, and set
the converter for that, otherwise just set the converter for the
property:

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class MyTest
{
private int test1;
public int Test1
{
get { return test1; }
set { test1 = value; }
}
private string test2;
[TypeConverter(typeof(StringListConverter))]
public string Test2
{
get { return test2; }
set { test2 = value; }
}
}
public class StringListConverter : TypeConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true; // display drop
}
public override bool
GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true; // drop-down vs combo
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
// note you can also look at context etc to build list
return new StandardValuesCollection(new string[] { "abc",
"def", "ghi" });
}
}
class Program
{
static void Main()
{
using (Form f = new Form())
using(PropertyGrid pg = new PropertyGrid())
{
pg.Dock = DockStyle.Fill;
pg.SelectedObject = new MyTest();
f.Controls.Add(pg);
Application.Run(f);
}
}
}

Dec 15 '06 #3
More example than tutorial; for what you want, the main thing is to
specify something like ExpandableObjectConverter as a type descriptor.
However, much much more customisation is possible.

Example:

http://www.codeproject.com/cs/miscct...ectiondata.asp

Marc

Dec 15 '06 #4
descrpitor: I meant converter...

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
More example than tutorial; for what you want, the main thing is to
specify something like ExpandableObjectConverter as a type
descriptor.
However, much much more customisation is possible.

Example:

http://www.codeproject.com/cs/miscct...ectiondata.asp

Marc

Dec 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mevar81 | last post: by
3 posts views Thread by Frank Rizzo | last post: by
reply views Thread by Frank Rizzo | last post: by
4 posts views Thread by phcmi | last post: by
3 posts views Thread by Redivivus | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.