473,396 Members | 1,966 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,396 software developers and data experts.

Problem with PropertyGrid control

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
2 3945
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terry | last post by:
I have a very basic program, but for some reason I can't get it to behave properly. What I want is a basic form with a TabControl that fills the entire form. The tab control should have 4 tabs...
4
by: Zoury | last post by:
Hi! Is it possible to use the PropertyGrid control without bind it to a control? As if it were a standard list? I would like to add "properties" in it but i never know what it will be. For...
0
by: Jay Shakankar via .NET 247 | last post by:
Dear Friends, I have been assigned to develop a Custom PropertyGrid with lookand fill like the link below. http://www.visualhint.com/feature5.php In the PropertyGrid value cells, I am assigned...
0
by: jays | last post by:
Dear Friends, I have been assigned to develop a Custom PropertyGrid with look and fill like the link below http://www.visualhint.com/feature5.php In the PropertyGrid value cells, I am...
1
by: ANDRES BECERRA | last post by:
Herfried K. Wagner was kind enough to point me to the PropertyGrid control http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformspropertygridclasstopic.asp I have found a few...
7
by: siddhiash | last post by:
Hi Friends I want to add PasswordChar Property which shows ****** for string which I type in PropertyGrid Control. Regards, Siddharth
4
by: Bernie Yaeger | last post by:
How can I set the browsableattributes of the control that has been selected (selectedobject) by the propertygrid? Here's what I'm after - I want to open a form with a propertygrid in it. The...
5
by: Ger | last post by:
The propertygrid is a great control, but I would like to show a more descriptive text for the properties in the control. I tried to find a solution within the system.componentmodel but did not...
6
by: Steve Teeples | last post by:
Can someone show me an example of how to place a "CheckedListBox" property within a PropertyGrid? -- ----------- Thanks, Steve
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.