0
我正在写一个库,允许我自定义表单元素。下面的代码是一个函数,获取控件的名称,获取属性的名称,然后设置控件的属性,但我似乎无法让它工作出于某种原因。感谢任何帮助appricated。使用字符串设置控件属性C#
代码:
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);
}
调用示例:
SetProp("greg", "Text", "hi")
'pInfo.SetValue()'看起来是错误的。你应该传递'mycontrol',而不是'name'。试试'pInfo.SetValue(mycontrol,x);'。 – TyCobb 2014-10-17 00:05:05
@TyCobb是的,解决了这个问题。非常感谢你! – gregyjames 2014-10-17 00:15:43