110,539
社区成员
发帖
与我相关
我的任务
分享
private TextBox F(int i)
{
//利用参数i返回主窗体的TextBox控件,如参数为2,则返回textBox2,参数为99,则返回textBox99
}
swich(i)
{
case 1:
retrun textBox1;
}
/// <summary>
/// 根据控件名和属性名赋值,并返回控件实例
/// </summary>
/// <param name="ClassInstance">控件所在实例</param>
/// <param name="ControlName">控件名</param>
/// <param name="PropertyName">属性名</param>
/// <param name="Value">属性值</param>
/// <returns>控件实例</returns>
public static Object SetValueControlProperty(Object ClassInstance, string ControlName, string PropertyName, Object Value)
{
Object Result = null;
Type myType = ClassInstance.GetType();
FieldInfo myFieldInfo = myType.GetField(ControlName, BindingFlags.NonPublic | BindingFlags.Instance);
if (myFieldInfo != null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myFieldInfo.FieldType);
PropertyDescriptor myProperty = properties.Find(PropertyName, false);
if (myProperty != null)
{
Object ctr;
ctr = myFieldInfo.GetValue(ClassInstance); //取得控件实例
try
{
myProperty.SetValue(ctr, Value);
Result = ctr;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
return Result;
}