ANSWER: Remove/Hide the (Name) property for a control in a propertygrid

P

pigeonrandle

Hi,
I hope this helps someone else because despite the fact that this is
obviously a common problem, after 5 hours of googling i found no exact
answer, just a load of 'do this, implement this, etc..' rant.

class LabelNoName : System.Windows.Forms.Label,
ICustomTypeDescriptor
{

#region ICustomTypeDescriptor Members

public String GetClassName()
{
return TypeDescriptor.GetClassName(this,true);
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this,true);
}

public String GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType,
true);
}

public EventDescriptorCollection GetEvents(Attribute[]
attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
return GetProperties();
}

public PropertyDescriptorCollection GetProperties()
{
PropertyDescriptorCollection pdcBase =
TypeDescriptor.GetProperties(this, true);
PropertyDescriptorCollection pdcNew = new
PropertyDescriptorCollection(null);
int iPropertiesIndex = 0;

while (iPropertiesIndex < pdcBase.Count)
{
PropertyDescriptor pd = pdcBase[iPropertiesIndex];
if (pd.DisplayName != "Name")
{
pdcNew.Add(pd);
}
iPropertiesIndex++;
}

return pdcNew;
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

#endregion
}

#############################################################################################

Also, other properties (not added by the designer ie in brackets/
parentheses) can be removed using this technique which in fairness is
better documented:

[DesignerAttribute(typeof(LabelPreFilteredAttributesDesigner))]
class LabelPreFilteredAttributes : System.Windows.Forms.Label,
ICustomTypeDescriptor
{

}

class LabelPreFilteredAttributesDesigner :
System.Windows.Forms.Design.ControlDesigner
{
protected override void PreFilterProperties(IDictionary
properties)
{
properties.Remove("Dock");
}

//AND\OR

protected override void PostFilterProperties(IDictionary
properties)
{
properties.Remove("Location");
}
}

##########################################################################################

Less rant, more answers please,
James Randle.
 
M

Marc Gravell

Less rant, more answers please,
Was there a question? If the question was "how to not show property
x", then the simplest approach would depend on whether the
PropertyGrid in question is your own (on your own Form), or the IDEs;
if the former, note that you can create your own PropertyTab,
overriding the GetProperties() method(s) on a UI-by-UI basis (you need
to swap the tabs, but this isn't hard). For the IDE, an alternative
approach (in 2.0) is to look at TypeDescriptionProvider, which should
thoeretically allow you to hook into the model after-the-fact, i.e.
without having to inherit from the control. For *an* example of this
(not quite the same) see here:
http://groups.google.co.uk/group/mi...read/thread/7eea9e25cd44ad97/c1931954cf069571

Marc
 
M

Marc Gravell

One additional note: having the two GetProperties() methods return the
same can change the behaviour... for instance, normally the
Attribute[] version will be used to filter [Browsable(false)] etc.
There isn't (AFAIK) a simple helper to allow you to construct a
convenient GetProperties(Attribute[]) method - but as long as you know
that you are changing something... for instance, additional properties
might now appear.

Marc
 
P

pigeonrandle

Marc,
Thanks for providing some extra information for whatever weary
traveller rests their virtual head here!
 
P

pigeonrandle

Taking in account Marc's comments about the altered behaviour, you can
replace

public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
return GetProperties();
}

with...

public PropertyDescriptorCollection GetProperties(Attribute[]
attributes)
{
PropertyDescriptorCollection pdcBase =
TypeDescriptor.GetProperties(this, attributes, true);
PropertyDescriptorCollection pdcNew = new
PropertyDescriptorCollection(null);
int iPropertiesIndex = 0;


while (iPropertiesIndex < pdcBase.Count)
{
PropertyDescriptor pd = pdcBase[iPropertiesIndex];
if (pd.DisplayName != "Name")
{
pdcNew.Add(pd);
}
iPropertiesIndex++;
}


return pdcNew;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top