473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 35833
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mevar81 | last post by:
Hi to everybody.I have a problem with the PropertyGrid control.I want to display not all the properties of a generic Control(Button,TextBox,ComboBox,ecc.).In general I don't want to display only...
3
by: Frank Rizzo | last post by:
I added a PropertyGrid control to my form and set its SelectedObject property to one of my objects which exposes a lot of properties of type System.Drawing.Color. The System.Drawing.Color display...
0
by: Frank Rizzo | last post by:
Hello, I have the property grid control in my WinForm app. I have a property where I implemented a simple drop-down editor via the TypeConverter. I was wondering whether it is possible to add...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
4
by: phcmi | last post by:
I have a PropertyGrid question. My task is to replace a legacy dialog box presentation with a modern one. The dialog itself allows the user to set configuration settings in our application, so...
0
by: Richy | last post by:
Hi, I am running VB Express 2005 and have been developing an application fine under Windows XP, but now I have switched to WIndows Vista I am getting strange behaviour with the PropertyGrid...
3
by: Redivivus | last post by:
Hi Ho to use dropdown in propertygrid with values from database? Lets say i have table ITEMS and columns ID, NAME Now i want to select id, name from items and display those values in...
4
by: Rohan | last post by:
I need to use the Collection editor of the propertyGrid, but i can't this to work, i looked around on the NET on collection and I am kinda lost. i have this so far, i don't know what i should do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.