根据控件名字和属性动态修改值
2008-10-15 21:12
281 查看
public object GetCompProperty(object form, string compName, string propertyName)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
return propertyDescriptor.GetValue(obj);
}
return null;
}
public void SetCompProperty(object form, string compName, string propertyName, object value)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
propertyDescriptor.SetValue(obj, value);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCompProperty(this, "button1", "Width").ToString());
SetCompProperty(this, "button1", "Width", 100);
}
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
return propertyDescriptor.GetValue(obj);
}
return null;
}
public void SetCompProperty(object form, string compName, string propertyName, object value)
{
Type formType = form.GetType();
FieldInfo compInfo = formType.GetField(compName, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Component obj = (Component)compInfo.GetValue(form);
Type compType = obj.GetType();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(obj);
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection.Find(propertyName, false);
if (propertyDescriptor != null)
{
propertyDescriptor.SetValue(obj, value);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(GetCompProperty(this, "button1", "Width").ToString());
SetCompProperty(this, "button1", "Width", 100);
}
相关文章推荐
- c#的winform,怎么根据控件的名字获取控件属性!
- WPF 中动态创建、删除控件,注册控件名字,根据名字查找控件
- 如何根据一个规则表动态地在Form的初始化时设置控件的属性
- 动态编辑母版页(与母版页通信,如修改控件属性等)
- 动态修改控件属性
- 利用反射动态创建控件,且修改其属性
- asp.net动态编辑母版页(与母版页通信,如修改控件属性等)
- 动态修改 控件属性 2011.07.21
- asp.net动态编辑母版页(与母版页通信,如修改控件属性等)
- 转: javascript动态添加、修改、删除对象的属性和方法
- DuiDesigner修改:增加对RichEdit控件属性的保存
- Vue input控件通过value绑定动态属性及修饰符
- Vue input控件通过value绑定动态属性及修饰符的方法
- 动态批量修改字段属性
- jquery实现根据控件属性模糊查询
- 后台动态添加布局文件、控件与动态设置属性
- asp.net 包含动态创建控件的容器如果要切换显示/隐藏不要用 Visible 属性
- IOS 加载Xib 后 如何 动态修改xib中的控件frame
- 运行时使用代码动态的修改AspxGridView的列属性
- 动态加载用户控件及其属性访问