I am adding some design time support to a class that I need the user to
be able to modify. There are two class properties that I would like not
to display in the property grid. I have marked the property with the
browsable attribute as I have done in the past. But this time, these
properties are still getting displayed.
In this case, I have created a simple type converter for this class.
I'm wondering if I need to do something more elaborate in the
GetProperties method than returning TypeConverter.GetProperties(myType).
Does this mess up the original browsable attributes setting?
Thanks,
Scott
Here's some code:
[Serializable]
[TypeConverter(typeof(ResultConverter))]
public class Result : IResult
{
private string m_Name;
private string m_Units;
private double m_ToleranceMax;
private double m_ToleranceMin;
private bool m_IsUsed;
private double m_Value;
public Result(string name, string units)
{
m_Name = name;
m_Units = units;
m_ToleranceMin = 0;
m_ToleranceMax = 0;
// used by default
m_IsUsed = true;
}
public Result(string name, string units, double toleranceMin,
double toleranceMax ) : this(name, units)
{
if (toleranceMax >= toleranceMin)
{
m_ToleranceMin = toleranceMin;
m_ToleranceMax = toleranceMax;
}
else
{
throw new ArgumentException("The maximum tolerance must
be greater than or equal to the minimum tolerance.");
}
}
public Result(string name, string units, double toleranceMin,
double toleranceMax, bool isUsed) :
this(name, units, toleranceMin, toleranceMax)
{
m_IsUsed = isUsed;
}
public override string ToString()
{
return string.Format("{0} ({1})", m_Name, m_Units);
}
#region IResult Members
[Browsable(false)]
public string Name
{
get
{
return m_Name;
}
}
[Browsable(false)]
public string Units
{
get
{
return m_Units;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Description("Tolerance maximum for this result")]
public double ToleranceMax
{
get
{
return m_ToleranceMax;
}
set
{
if (value >= m_ToleranceMin)
{
m_ToleranceMax = value;
}
else
{
throw new ArgumentException("The maximum tolerance
must be greater than or equal to the minimum tolerance.");
}
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Description("Tolerance minimum for this result")]
public double ToleranceMin
{
get
{
return m_ToleranceMin;
}
set
{
if (value <= m_ToleranceMax)
{
m_ToleranceMin = value;
}
else
{
throw new ArgumentException("The minimum tolerance
must be less than or equal to the maximum tolerance.");
}
}
}
[Browsable(false)]
public double Value
{
get
{
return m_Value;
}
set
{
m_Value = value;
}
}
[Description("This flag is used to tell the analysis routine to
calculate and display this result.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
DefaultValue(true) ]
public bool IsUsed
{
get
{
return m_IsUsed;
}
set
{
m_IsUsed = value;
}
}
public bool IsWithinTolerances()
{
return ((m_Value >= m_ToleranceMin) && (m_Value <=
m_ToleranceMax));
}
#endregion
}
class ResultConverter : TypeConverter
{
public override bool
GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(Result));
}
}
Linda Liu [MSFT] - 01 Sep 2006 04:13 GMT
Hi ScottP,
Yes, you are right. Since you have associated your custom TypeConverter
with the Result class and returned True in the overridden
GetPropertiesSupported method, the PropertyGrid will display all the
properties returned by the overridden GetProperties method, regardless of
what attributes applied to the properties in the Result class.
The code "TypeDescriptor.GetProperties(typeof(Result))" returns ALL public
properties in the Result class, including those properties applied the
Browsable attribute with a value of false. If you wouldn't like to display
these properties in the PropertyGrid, you should pass an attribute filter
parameter in the TypeDescriptor.GetProperties method. The override
GetProperties method provides a parameter attributes which already contains
a Browsable attribute with a value of true. So we could use this parameter
directly.
The following is a sample.
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
PropertyDescriptorCollection cols =
TypeDescriptor.GetProperties(typeof(Result),attributes);
return cols;
}
Build and run the program again. You will find the Name, Units and Value
properties aren't visible in the PropertyGrid now.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 05 Sep 2006 11:54 GMT
Hi Scott,
How about the problem now?
If the problem isn't solved or you have anything unclear, please feel free
to tell me.
Thank you for using our MSDN Managed Newsgroup Support Service!
Sincerely,
Linda Liu
Microsoft Online Community Support
ScottP - 06 Sep 2006 23:19 GMT
> Hi Scott,
>
[quoted text clipped - 8 lines]
> Linda Liu
> Microsoft Online Community Support
Linda,
Thank you. That seems to have fixed the problem.
Just so I understand. The property grid class passes in an array of
attributes to use for filtering the elements for the properties
collection. By passing this into the generic
TypeDescriptor.GetProperties method, the runtime filtered the public
properties of my type.
This has the same effect:
PropertyDescriptorCollection cols =
TypeDescriptor.GetProperties(typeof(Result), new Attribute[] {
BrowsableAttribute.Yes });
Thanks again,
Scott