Reputation: 581
I am currenly writing a library that allows my to customize form elements. The code below is a function that gets the name of a control, gets the name of the property and then sets the property of the control but I cant seem to get it to work for some reason. Thanks any help is appricated.
The code:
public void SetProp(string name, string prop, string value)
{
Form FormControl = Application.OpenForms[form];
Control mycontrol = FormControl.Controls.Find(name, true)[0];
PropertyInfo pInfo = mycontrol.GetType().GetProperty(prop);
TypeConverter tc = TypeDescriptor.GetConverter(pInfo.PropertyType);
var x = tc.ConvertFromString(value);
pInfo.SetValue(name, x, null);
}
Sample Call:
SetProp("greg", "Text", "hi")
Upvotes: 1
Views: 1394
Reputation: 9089
You need to pass in the actual source object into the PropertyInfo.SetValue
call so it can actually be modified. PropertyInfo
is basically just information about the property (hence the name), it has no attachment to that specific instance.
You can get it work by changing your call like so:
pInfo.SetValue(mycontrol, x);
http://msdn.microsoft.com/en-us/library/hh194291(v=vs.110).aspx
Upvotes: 3