Not sure if you can disable the property in the PropertyGrid but you can hide
it using the directions provided by Shawn in the follwoing post.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OfCi2jcrBHA.225
2%40tkmsftngp03
Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET
This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
""Bharat Patel [MSFT]"" <bha...@online.microsoft.com> wrote in message
news:fuBpyLNG...@cpmsftngxa06.phx.gbl...
I did this kind of trick to get a property to use a drop-down editor
when another property had one value, then change to a modal dialog
editor when that property had a different value.
(If this explanation doesn't make sense, I can try to concoct some
sample code around this)
..David..
"David Jackman" <david....@nextpage.com.nospam> wrote in message
news:3EC0FE43...@nextpage.com.nospam...
http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
As I'm sure others (including me) would like to see an example...
Just a thought
Jay
"David Jackman" <david....@nextpage.com.nospam> wrote in message
news:3EC0FE43...@nextpage.com.nospam...
"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message
news:uIEK3cZG...@TK2MSFTNGP10.phx.gbl...
I was actually addressing David Jackman with my statement, as he suggested
he does have a solution...
Jay
"David W. Simmonds" <da...@simmonds.ca> wrote in message
news:Jfewa.164230$ya.50...@news1.calgary.shaw.ca...
There is a link that creates an email to submit a article/sample to
www.windowsforms.net.
http://www.windowsforms.net/Default.aspx?tabindex=10&tabid=52
Jay
"David Jackman" <david....@nextpage.com.nospam> wrote in message
news:3EC8EC2B...@nextpage.com.nospam...
..David..
To start, here is the class I'll use for the SelectedObject in the grid:
[PropertyTab(typeof(CustomPropertyTab), PropertyTabScope.Component)]
public class SelectedObject
{
private string message1 = "initial value";
private bool message1readonly = false;
[Description("This property is read only if the Message1ReadOnly property is true.")]
public string Message1 { get { return message1; } set { message1 = value; } }
[RefreshProperties(RefreshProperties.All)]
[Description("Determines the read only state for the Message 1 property.")]
public bool Message1ReadOnly { get { return message1readonly; } set { message1readonly = value; } }
}
The class attribute specifies a custom property tab, which is necessary for
getting a custom property descriptor for the properties in the class. Here
is the custom property tab class:
public class CustomPropertyTab : PropertyTab
{
public override string TabName { get { return "CustomPropertyTab"; } }
public override Bitmap Bitmap { get { return new Bitmap(16, 16); } }
public override PropertyDescriptorCollection GetProperties(object component)
{
return GetProperties(component, null);
}
public override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
{
PropertyDescriptorCollection coll = new PropertyDescriptorCollection(null);
foreach (PropertyInfo prop in component.GetType().GetProperties())
{
bool add = true;
foreach (Attribute att in attributes)
{
Attribute[] propAtts = (Attribute[])prop.GetCustomAttributes(att.GetType(), true);
if (propAtts == null || propAtts.Length == 0)
{
if (att.IsDefaultAttribute())
{
continue;
}
else
{
add = false;
break;
}
}
foreach (Attribute propAtt in propAtts)
{
if (!att.Match(propAtt))
{
add = false;
break;
}
}
if (!add)
break;
}
if (add)
{
coll.Add(new CustomPropertyDescriptor(component, prop));
}
}
return coll;
}
}
Here is the custom property descriptor class. This class is pretty straightforward,
and just does the obvious things based on the property info, which was obtained by
the property tab through reflection. The exception is for the ReadOnly property,
which gets its value from the other property in the special case.
public class CustomPropertyDescriptor : PropertyDescriptor
{
protected object m_editObject;
protected PropertyInfo m_propInfo;
public CustomPropertyDescriptor(object component, PropertyInfo propInfo)
: base(propInfo.Name, new Attribute[]{CategoryAttribute.Default})
{
m_editObject = component;
m_propInfo = propInfo;
}
/// <summary>
/// Indicates the category that the property will appear in. The value will be the value given for the <see cref="CategoryAttribute"/>
/// attribute for the property, if one exists. If there is no CategoryAttribute, then this will be <see cref="c_DefaultCategoryName"/>.
/// </summary>
public override string Category
{
get
{
object[] atts = m_propInfo.GetCustomAttributes(typeof(CategoryAttribute), true);
return atts.Length > 0 ? ((CategoryAttribute)atts[0]).Category : "General";
}
}
/// <summary>
/// Indicates the name for the property that will be displayed in the PropertyGrid control. The value will be the value given for the
/// <see cref="DisplayNameAttribute"/> attribute for the property. If there is no DisplayNameAttribute, then this will be the name of
/// the property.
/// </summary>
public override string DisplayName
{
get
{
object[] atts = m_propInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
return atts.Length > 0 ? ((DisplayNameAttribute)atts[0]).Name : m_propInfo.Name;
}
}
/// <summary>
/// Indicates the description for the property. The value will be the value given for the <see cref="DescriptionAttribute"/>
/// attribute for the property, if one exists. If there is no DescriptionAttribute, then this will be null.
/// </summary>
public override string Description
{
get
{
object[] atts = m_propInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
return atts.Length > 0 ? ((DescriptionAttribute)atts[0]).Description : null;
}
}
public override bool IsBrowsable { get { return (m_propInfo.Name.Equals("DontShow")) ? false : true; } }
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.ComponentType"/>.
/// </summary>
public override Type ComponentType { get { return m_propInfo.DeclaringType; } }
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.IsReadOnly"/>.
/// </summary>
public override bool IsReadOnly
{
get
{
if (m_editObject is SelectedObject && m_propInfo.Name.Equals("Message1"))
{
return ((SelectedObject)m_editObject).Message1ReadOnly;
}
return !m_propInfo.CanWrite;
}
}
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.PropertyType"/>.
/// </summary>
public override Type PropertyType { get { return m_propInfo.PropertyType; } }
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.CanResetValue"/>.
/// </summary>
public override bool CanResetValue(object component)
{
return true;
}
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.GetValue"/>.
/// </summary>
public override object GetValue(object component)
{
return m_propInfo.GetValue(component, null);
}
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.ResetValue"/>.
/// </summary>
public override void ResetValue(object component)
{
}
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.SetValue"/>.
/// </summary>
public override void SetValue(object component, object value)
{
m_propInfo.SetValue(component, value, null);
}
/// <summary>
/// Implementation of abstract <see cref="PropertyDescriptor.ShouldSerializeValue"/>.
/// </summary>
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override object GetEditor(Type editorBaseType)
{
object[] atts = m_propInfo.GetCustomAttributes(typeof(EditorAttribute), true);
foreach (EditorAttribute editatt in atts)
{
if (editatt.EditorBaseTypeName == editorBaseType.AssemblyQualifiedName)
{
Type editortype = Type.GetType(editatt.EditorTypeName);
return Activator.CreateInstance(editortype);
}
}
return base.GetEditor(editorBaseType);
}
protected override void FillAttributes(IList attList)
{
base.FillAttributes(attList);
object[] atts = m_propInfo.GetCustomAttributes(typeof(RefreshPropertiesAttribute), true);
foreach (Attribute att in atts)
{
attList.Add(att);
}
atts = m_propInfo.GetCustomAttributes(typeof(TypeConverterAttribute), true);
foreach (Attribute att in atts)
{
attList.Add(att);
}
}
}
Unfortunately, we also need to subclass the property grid so it will use the
custom property tab by default:
public class CustomPropertyGrid : PropertyGrid
{
protected override Type DefaultTabType { get { return typeof(CustomPropertyTab); } }
}
Finally, here's the main window form to hold the whole thing together:
public class MainForm : System.Windows.Forms.Form
{
private CustomPropertyGrid propertyGrid;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
SelectedObject target = new SelectedObject();
propertyGrid.SelectedObject = target;
}
/// <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.propertyGrid = new CustomPropertyGrid();
this.SuspendLayout();
//
// propertyGrid
//
this.propertyGrid.CommandsVisibleIfAvailable = true;
this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid.LargeButtons = false;
this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid.Name = "propertyGrid";
this.propertyGrid.Size = new System.Drawing.Size(292, 271);
this.propertyGrid.TabIndex = 0;
this.propertyGrid.Text = "propertyGrid";
this.propertyGrid.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 271);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.propertyGrid});
this.Name = "MainForm";
this.Text = "Dynamic Property Grid";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
}