cnd
cnd

Reputation: 33784

Looking for a way to dynamically change field names in PropertyGrid

I've got COM object attached to property grid.

Type typeObj = Type.GetTypeFromProgID(progIdService);
var obj = Activator.CreateInstance(typeObj);
propertyGrid1.SelectedObject = obj;

Now I need some way to translate object fields into my language using some translator. I was trying to use wrapper around object but with COM object I have no PropertyInfo, I have only PropertyDescription so I'm still looking for all the possible variants of doing it.

Upvotes: 2

Views: 745

Answers (2)

Simon Mourier
Simon Mourier

Reputation: 139226

What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

like this:

DynamicTypeDescriptor dtp = new DynamicTypeDescriptor(typeObj);

// get current property definition and remove it
var current = dtp.Properties["ThePropertyToChange"];
dtp.RemoveProperty("ThePropertyToChange");

// add a new one, but change its display name
DynamicTypeDescriptor.DynamicProperty prop = new DynamicTypeDescriptor.DynamicProperty(dtp, current, obj);
prop.SetDisplayName("MyNewPropertyName");
dtp.AddProperty(prop);

propertyGrid1.SelectedObject = dtp.FromComponent(obj);

Upvotes: 3

Rob
Rob

Reputation: 11798

I think you can use reflection to get the property names, although I haven't tried with COM objects yet. Be sure to include the System.Reflection namespace, then you can use it like that:

var props = myComObject.GetType().GetProperties();
foreach (var prop in props)
{
    MessageBox(prop.Name);
}

Upvotes: 0

Related Questions