This is part of a set of examples from this months Well Formed magazine
which has an article on enhancing the user UI by customizing the
PropertyGrid view. To get the rest of the article you can subscribe to Well
Formed here...
http://www.bobpowell.net/subscribe.htm
Code After my signature.
--
Bob Powell [MVP]
C#, System.Drawing
September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
----------------------------------------------
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();
}
/// <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()
{
// DONE: Add Filterable.GetConverter implementation
return TypeDescriptor.GetConverter(this,true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
// DONE: Add Filterable.GetEvents implementation
return TypeDescriptor.GetEvents(this,attributes,true);
}
EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEve nts()
{
// DONE: Add
Filterable.System.ComponentModel.ICustomTypeDescri ptor.GetEvents
implementation
return TypeDescriptor.GetEvents(this,true);
}
public string GetComponentName()
{
// DONE: Add Filterable.GetComponentName implementation
return TypeDescriptor.GetComponentName(this,true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
// DONE: Add Filterable.GetPropertyOwner implementation
return this;
}
public AttributeCollection GetAttributes()
{
// DONE: Add Filterable.GetAttributes implementation
return TypeDescriptor.GetAttributes(this,true);
}
public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
// DONE: 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()
{
// DONE: 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)
{
// DONE: Add Filterable.GetEditor implementation
return TypeDescriptor.GetEditor(this, typeof(UITypeEditor), true);
}
public PropertyDescriptor GetDefaultProperty()
{
// DONE: Add Filterable.GetDefaultProperty implementation
return TypeDescriptor.GetDefaultProperty(this, true);
}
public EventDescriptor GetDefaultEvent()
{
// DONE: Add Filterable.GetDefaultEvent implementation
return TypeDescriptor.GetDefaultEvent(this,true);
}
public string GetClassName()
{
// DONE: Add Filterable.GetClassName implementation
return TypeDescriptor.GetClassName(this,true);
}
#endregion
}
}
----------------------------------------------
"Nicolas" <nl*****@hotmail.com> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
Thank you, I try some of those but get very confuse with all those csharp
code, so it never works.
Won't you have any very sample code to point me to the right direction.
Thank you very much.
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OH****************@TK2MSFTNGP12.phx.gbl... Derive a class from TextBox that implements ICustomTypeDescriptor.
You can use the GetProperties method to return a
PropertyDescriptorCollection that you can create on the fly looking at
all the properties and only putting the ones you want, such as those with
the "Appearance" category on the list.
--
Bob Powell [MVP]
C#, System.Drawing
September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
"Nicolas" <nl*****@hotmail.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl... I want to display in my application a PropertyGrid showing only the
"Appearance" of a control (ie. textBox) so the user can change it.
Bare with me, I'm new in csharp.
Where do I go from:
this.PropertyGrid1.SelectedObject = this.Textbox1;
Thank a lot