By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,644 Members | 1,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,644 IT Pros & Developers. It's quick & easy.

Problem with PropertyGrid control

P: n/a
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 one category(Appearance,
Behavior,ecc.) but I want to chose directly which
properties to show.I've read that I can use the
SelectedObjects to put an array of object with some
properties in common with the SelectedObject,and only
properties in common for all the object will be
displayed.I've tryed to build an object with only some
properties and I've tryed to use like I've explain but it
doesn't work(it displays all the properties of the
control).Anyone has some idea to solve my problem?Thank's
to everybody
..
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You need to have your classes implement ICustomTypeDescriptor and return
only those properties with a certain attribute.

The code below my signature shows a simple demo of property filtering
according to the Category attribute. In this case it only shows those
proerties in the appearance category.

Well Formed this month has a more in-depth article on customizing properties
for use in the Property Grid including a dynamic filtering system. Check out
the current issue page

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

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

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

Blog http://bobpowelldotnet.blogspot.com

------------------------------------------------------------------
using System;

using System.Drawing;

using System.Drawing.Design;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Reflection;

namespace PropertyFilter

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.PropertyGrid propertyGrid1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

Filterable f=new Filterable();

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(8, 32);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "With";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(8, 72);

this.button2.Name = "button2";

this.button2.TabIndex = 0;

this.button2.Text = "Without";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// propertyGrid1

//

this.propertyGrid1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Win dows.Forms.AnchorStyles.To
p | System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.propertyGrid1.CommandsVisibleIfAvailable = true;

this.propertyGrid1.LargeButtons = false;

this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;

this.propertyGrid1.Location = new System.Drawing.Point(160, 16);

this.propertyGrid1.Name = "propertyGrid1";

this.propertyGrid1.Size = new System.Drawing.Size(272, 240);

this.propertyGrid1.TabIndex = 1;

this.propertyGrid1.Text = "propertyGrid1";

this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;

this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(440, 266);

this.Controls.Add(this.propertyGrid1);

this.Controls.Add(this.button1);

this.Controls.Add(this.button2);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

this.f.Filtered=true;

this.propertyGrid1.SelectedObject=f;

this.propertyGrid1.Refresh();

}

private void button2_Click(object sender, System.EventArgs e)

{

this.f.Filtered=false;

this.propertyGrid1.SelectedObject=f;

this.propertyGrid1.Refresh();

}

}

class Filterable : ICustomTypeDescriptor

{

bool _filtered;

public bool Filtered

{

get{return _filtered;}

set{_filtered=value;}

}

string _stringOne;

[Category("Other")]

public string StringOne

{

get{return _stringOne;}

set{_stringOne=value;}

}

string _stringTwo;

[Category("Appearance")]

public string StringTwo

{

get{return _stringTwo;}

set{_stringTwo=value;}

}

#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()

{

// TODO: Add Filterable.GetConverter implementation

return TypeDescriptor.GetConverter(this,true);

}

public EventDescriptorCollection GetEvents(Attribute[] attributes)

{

// TODO: Add Filterable.GetEvents implementation

return TypeDescriptor.GetEvents(this,attributes,true);

}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEve nts()

{

// TODO: Add
Filterable.System.ComponentModel.ICustomTypeDescri ptor.GetEvents
implementation

return TypeDescriptor.GetEvents(this,true);

}

public string GetComponentName()

{

// TODO: Add Filterable.GetComponentName implementation

return TypeDescriptor.GetComponentName(this,true);

}

public object GetPropertyOwner(PropertyDescriptor pd)

{

// TODO: Add Filterable.GetPropertyOwner implementation

return this;

}

public AttributeCollection GetAttributes()

{

// TODO: Add Filterable.GetAttributes implementation

return TypeDescriptor.GetAttributes(this,true);

}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)

{

// TODO: Add Filterable.GetProperties implementation

PropertyDescriptorCollection
pdc=TypeDescriptor.GetProperties(this,attributes,t rue);

if(_filtered)

{

PropertyDescriptorCollection pds=new PropertyDescriptorCollection(new
PropertyDescriptor[0]);

foreach(PropertyDescriptor pd in pdc)

{

Attribute a=pd.Attributes[typeof(CategoryAttribute)];

if(a!=null && ((CategoryAttribute)a).Category=="Appearance")

{

pds.Add(pd);

}

}

return pds;

}

else

return TypeDescriptor.GetProperties(this,attributes,true) ;

}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetPro perties()

{

// TODO: Add
Filterable.System.ComponentModel.ICustomTypeDescri ptor.GetProperties
implementation

PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,true);

if(_filtered)

{

PropertyDescriptorCollection pds=new PropertyDescriptorCollection(new
PropertyDescriptor[0]);

foreach(PropertyDescriptor pd in pdc)

{

Attribute a=pd.Attributes[typeof(CategoryAttribute)];

if(a!=null && ((CategoryAttribute)a).Category=="Appearance")

{

pds.Add(pd);

}

}

return pds;

}

else

return pdc;

}

public object GetEditor(Type editorBaseType)

{

// TODO: Add Filterable.GetEditor implementation

return TypeDescriptor.GetEditor(this, typeof(UITypeEditor), true);

}

public PropertyDescriptor GetDefaultProperty()

{

// TODO: Add Filterable.GetDefaultProperty implementation

return TypeDescriptor.GetDefaultProperty(this, true);

}

public EventDescriptor GetDefaultEvent()

{

// TODO: Add Filterable.GetDefaultEvent implementation

return TypeDescriptor.GetDefaultEvent(this,true);

}

public string GetClassName()

{

// TODO: Add Filterable.GetClassName implementation

return TypeDescriptor.GetClassName(this,true);

}

#endregion

}

}

------------------------------------------------------------------

"Mevar81" <to*****@hotmail.com> wrote in message
news:07****************************@phx.gbl...
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 one category(Appearance,
Behavior,ecc.) but I want to chose directly which
properties to show.I've read that I can use the
SelectedObjects to put an array of object with some
properties in common with the SelectedObject,and only
properties in common for all the object will be
displayed.I've tryed to build an object with only some
properties and I've tryed to use like I've explain but it
doesn't work(it displays all the properties of the
control).Anyone has some idea to solve my problem?Thank's
to everybody
.

Nov 15 '05 #2

P: n/a
Thank you for your suggestions.Now I'll try what you've
said.Thank you again for your great example!
-----Original Message-----
You need to have your classes implement ICustomTypeDescriptor and returnonly those properties with a certain attribute.

The code below my signature shows a simple demo of property filteringaccording to the Category attribute. In this case it only shows thoseproerties in the appearance category.

Well Formed this month has a more in-depth article on customizing propertiesfor use in the Property Grid including a dynamic filtering system. Check outthe current issue page

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

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

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

Blog http://bobpowelldotnet.blogspot.com

--------------------------------------------------------- ---------using System;

using System.Drawing;

using System.Drawing.Design;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Reflection;

namespace PropertyFilter

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.PropertyGrid propertyGrid1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;
Filterable f=new Filterable();

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(8, 32);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "With";

this.button1.Click += new System.EventHandler (this.button1_Click);
//

// button2

//

this.button2.Location = new System.Drawing.Point(8, 72);

this.button2.Name = "button2";

this.button2.TabIndex = 0;

this.button2.Text = "Without";

this.button2.Click += new System.EventHandler (this.button2_Click);
//

// propertyGrid1

//

this.propertyGrid1.Anchor =
((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.propertyGrid1.CommandsVisibleIfAvailable = true;

this.propertyGrid1.LargeButtons = false;

this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point (160, 16);
this.propertyGrid1.Name = "propertyGrid1";

this.propertyGrid1.Size = new System.Drawing.Size(272, 240);
this.propertyGrid1.TabIndex = 1;

this.propertyGrid1.Text = "propertyGrid1";

this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(440, 266);

this.Controls.Add(this.propertyGrid1);

this.Controls.Add(this.button1);

this.Controls.Add(this.button2);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)
{

this.f.Filtered=true;

this.propertyGrid1.SelectedObject=f;

this.propertyGrid1.Refresh();

}

private void button2_Click(object sender, System.EventArgs e)
{

this.f.Filtered=false;

this.propertyGrid1.SelectedObject=f;

this.propertyGrid1.Refresh();

}

}

class Filterable : ICustomTypeDescriptor

{

bool _filtered;

public bool Filtered

{

get{return _filtered;}

set{_filtered=value;}

}

string _stringOne;

[Category("Other")]

public string StringOne

{

get{return _stringOne;}

set{_stringOne=value;}

}

string _stringTwo;

[Category("Appearance")]

public string StringTwo

{

get{return _stringTwo;}

set{_stringTwo=value;}

}

#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()

{

// TODO: Add Filterable.GetConverter implementation

return TypeDescriptor.GetConverter(this,true);

}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{

// TODO: Add Filterable.GetEvents implementation

return TypeDescriptor.GetEvents(this,attributes,true);

}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEv ents()

{

// TODO: Add
Filterable.System.ComponentModel.ICustomTypeDescr iptor.Ge tEventsimplementation

return TypeDescriptor.GetEvents(this,true);

}

public string GetComponentName()

{

// TODO: Add Filterable.GetComponentName implementation

return TypeDescriptor.GetComponentName(this,true);

}

public object GetPropertyOwner(PropertyDescriptor pd)

{

// TODO: Add Filterable.GetPropertyOwner implementation

return this;

}

public AttributeCollection GetAttributes()

{

// TODO: Add Filterable.GetAttributes implementation

return TypeDescriptor.GetAttributes(this,true);

}

public PropertyDescriptorCollection GetProperties (Attribute[] attributes)
{

// TODO: Add Filterable.GetProperties implementation

PropertyDescriptorCollection
pdc=TypeDescriptor.GetProperties(this,attributes, true);

if(_filtered)

{

PropertyDescriptorCollection pds=new PropertyDescriptorCollection(newPropertyDescriptor[0]);

foreach(PropertyDescriptor pd in pdc)

{

Attribute a=pd.Attributes[typeof(CategoryAttribute)];

if(a!=null && ((CategoryAttribute) a).Category=="Appearance")
{

pds.Add(pd);

}

}

return pds;

}

else

return TypeDescriptor.GetProperties (this,attributes,true);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetPr operties ()
{

// TODO: Add
Filterable.System.ComponentModel.ICustomTypeDescr iptor.Ge tPropertiesimplementation

PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,true);
if(_filtered)

{

PropertyDescriptorCollection pds=new PropertyDescriptorCollection(newPropertyDescriptor[0]);

foreach(PropertyDescriptor pd in pdc)

{

Attribute a=pd.Attributes[typeof(CategoryAttribute)];

if(a!=null && ((CategoryAttribute) a).Category=="Appearance")
{

pds.Add(pd);

}

}

return pds;

}

else

return pdc;

}

public object GetEditor(Type editorBaseType)

{

// TODO: Add Filterable.GetEditor implementation

return TypeDescriptor.GetEditor(this, typeof (UITypeEditor), true);
}

public PropertyDescriptor GetDefaultProperty()

{

// TODO: Add Filterable.GetDefaultProperty implementation

return TypeDescriptor.GetDefaultProperty(this, true);

}

public EventDescriptor GetDefaultEvent()

{

// TODO: Add Filterable.GetDefaultEvent implementation

return TypeDescriptor.GetDefaultEvent(this,true);

}

public string GetClassName()

{

// TODO: Add Filterable.GetClassName implementation

return TypeDescriptor.GetClassName(this,true);

}

#endregion

}

}

--------------------------------------------------------- ---------
"Mevar81" <to*****@hotmail.com> wrote in message
news:07****************************@phx.gbl...
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 one category(Appearance,
Behavior,ecc.) but I want to chose directly which
properties to show.I've read that I can use the
SelectedObjects to put an array of object with some
properties in common with the SelectedObject,and only
properties in common for all the object will be
displayed.I've tryed to build an object with only some
properties and I've tryed to use like I've explain but it doesn't work(it displays all the properties of the
control).Anyone has some idea to solve my problem? Thank's to everybody
.

.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.