C# 设计时怎么动态改变控件属性的可见性 本帖最后由 QQ81867376 于 2012-06-26 12:14:28 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class ControlTest : TextBox从这个控件继承 这个是你自己写的控件?直接设置控件的visible属性啊 写自定义控件的时候如果需要外部设定的属性记得写成public,然后再get事件里面写需要变更的值。如果你的控件是gdi+渲染的,记得在get事件里面重新画一下。 Devexpress 的图表统计,对应的属性就有这样的啊。 如果是自定义控件,只要继承ICustomTypeDescriptor接口,实现其方法即可解决,举例如下: public class ControlTest : System.Windows.Forms.Control, ICustomTypeDescriptor { List<string> propertyNames = null;//存放要显示的属性 public ControlTest() : base() { propertyNames = new List<string>(); propertyNames.Add("Size"); propertyNames.Add("Location"); } #region ICustomTypeDescriptor 成员 AttributeCollection ICustomTypeDescriptor.GetAttributes() { return TypeDescriptor.GetAttributes(this.GetType()); } string ICustomTypeDescriptor.GetClassName() { return TypeDescriptor.GetClassName(this.GetType()); } string ICustomTypeDescriptor.GetComponentName() { return TypeDescriptor.GetComponentName(this.GetType()); } TypeConverter ICustomTypeDescriptor.GetConverter() { return TypeDescriptor.GetConverter(this.GetType()); } EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this.GetType()); } PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this.GetType()); } object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this.GetType(), editorBaseType); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this.GetType(), attributes); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return TypeDescriptor.GetEvents(this.GetType()); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) { return this.FilterProperties(TypeDescriptor.GetProperties(this.GetType(), attributes)); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return this.FilterProperties(TypeDescriptor.GetProperties(this.GetType())); } object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return this; } #endregion #region 属性过滤 private PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection properties) { List<PropertyDescriptor> list = new List<PropertyDescriptor>(); foreach (string pname in propertyNames) { var property = properties[pname]; if (property != null) { list.Add(property); } } return new PropertyDescriptorCollection(list.ToArray(), true); } #endregion }这样,你可以通过修改propertyNames里面的元素,来达到显示所需属性的目的。 在你的那个贴子里回复了。有一个扩展属性的东东。msdn讲的比较详细一些。微软的tooltip你搜一下就有。青龙白虎这个更不错。 public static void SetPropertyVisible(object entity, string attrName, bool visible){ Type type = typeof(BrowsableAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(entity); AttributeCollection attrs = props[attrName].Attributes; FieldInfo info = type1.GetField("browsable", BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Instance); info.SetValue(attrs[type], visible);}通过反射处理,动态修改属性的Browsable值。 entiry 是表示 “要获取其属性的组件”吧,我调用的时候达不到目的,当设为false时候,把所有控件的属性都搞没有了,比如窗体,文本框的,这样太强了吧, public bool IsShowTextColor { get { return isShowTextColor; } set { isShowTextColor = value; //方案一 // SetPropertyVisible(value); //this.UpdateStyles(); //this.Refresh(); //this.Update(); //方案二 SetPropertyVisible(this, "TextColor", value); } } 晕。首先你得先在你要动态调整的属性上加上 Browsable。private String name = String.Empty;[Browsable(true)]public String Name{ get{ return this.name; } set { if (this.name != value) { this.name = value; } }} 把其它属性也加上Browsable,否则会把整个类里的属性全改变。 加上确实是可以,但是和上面的是一样的效果,再改变对应值的时候,不能立马隐藏或者显示该属性。public bool IsShowTextColor { get { return isShowTextColor; } set { isShowTextColor = value; //this.UpdateStyles(); //this.Refresh(); //this.Update(); //方案二 SetPropertyVisible(this, "TextColor", value); } } 关于跨线程调用控件。最后面注释掉的三项和之前一项有什么区别? 为什么最后一项会报错? 复杂问题,如何让泛型实体类支持参数的new方法 如何给.NET安装程序加密 想在dataGrid中实现对数据库的更新 想做个文本批量处理器,苦于没有思路,给点建议 关于空间中的问题 如何不让WebBrower打开文件时提示"打开或保存"? 怎样实现网站访问者唯一身份认证 关于多层结构里数据层中碰到的IDbDataAdapter接口 疑问,望赐教。 自己用C#写了一个小软件,Access做数据库,asp做接口,特地上来告诉你,哈哈 一道简单的JAVA算法分析 求个思路
直接设置控件的visible属性啊
Devexpress 的图表统计,对应的属性就有这样的啊。
public class ControlTest : System.Windows.Forms.Control, ICustomTypeDescriptor
{
List<string> propertyNames = null;//存放要显示的属性
public ControlTest()
: base()
{
propertyNames = new List<string>();
propertyNames.Add("Size");
propertyNames.Add("Location");
} #region ICustomTypeDescriptor 成员 AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this.GetType());
} string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this.GetType());
} string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this.GetType());
} TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this.GetType());
} EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this.GetType());
} PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this.GetType());
} object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this.GetType(), editorBaseType);
} EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this.GetType(), attributes);
} EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this.GetType());
} PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return this.FilterProperties(TypeDescriptor.GetProperties(this.GetType(), attributes));
} PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return this.FilterProperties(TypeDescriptor.GetProperties(this.GetType()));
} object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
} #endregion #region 属性过滤 private PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection properties)
{
List<PropertyDescriptor> list = new List<PropertyDescriptor>(); foreach (string pname in propertyNames)
{
var property = properties[pname];
if (property != null)
{
list.Add(property);
}
}
return new PropertyDescriptorCollection(list.ToArray(), true);
} #endregion
}
这样,你可以通过修改propertyNames里面的元素,来达到显示所需属性的目的。
有一个扩展属性的东东。msdn讲的比较详细一些。微软的tooltip你搜一下就有。青龙白虎这个更不错。
public static void SetPropertyVisible(object entity, string attrName, bool visible)
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(entity);
AttributeCollection attrs = props[attrName].Attributes;
FieldInfo info = type1.GetField("browsable", BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Instance);
info.SetValue(attrs[type], visible);
}通过反射处理,动态修改属性的Browsable值。
entiry 是表示 “要获取其属性的组件”吧,我调用的时候达不到目的,当设为false时候,把所有控件的属性都搞没有了,比如窗体,文本框的,这样太强了吧, public bool IsShowTextColor
{
get { return isShowTextColor; }
set
{
isShowTextColor = value;
//方案一
// SetPropertyVisible(value); //this.UpdateStyles();
//this.Refresh();
//this.Update(); //方案二
SetPropertyVisible(this, "TextColor", value);
}
}
[Browsable(true)]
public String Name
{
get{ return this.name; }
set
{
if (this.name != value)
{
this.name = value;
}
}
}
{
get { return isShowTextColor; }
set
{
isShowTextColor = value;
//this.UpdateStyles();
//this.Refresh();
//this.Update(); //方案二
SetPropertyVisible(this, "TextColor", value);
}
}