public class MyLabel : Label { [Browsable(false)] public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } [Browsable(false)] //<--- public override string Text { get { return base.Text; } set { base.Text = value; } } }
public class MyLabel : Label
{
[Browsable(false)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
} [Browsable(false)] //<---
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
}
在网上找了很多资料都没找到比较好的解决方法
在 codeProject 找到一个
http://www.codeproject.com/KB/miscctrl/bending_property.aspx
利用此类,让自已的控件继续接口ICustomTypeDescriptor
public partial class CustomLabel : System.Windows.Forms.Label, ICustomTypeDescriptor
用类组合的方法使用源码中的类, 目的是达到了
结果是这个控件变成了组件而不是可视控件, 估计是许多方法被覆盖了. 还有哪位大侠出来指点指点
对于控件就不知道如何处理了
贴出代码,共享一下:using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;namespace MBar.DesignControl
{
public class CustomBarCodeDesigner : System.Windows.Forms.Design.ControlDesigner
{
protected override void PostFilterProperties(System.Collections.IDictionary properties)
{
//locked属性是在设计器中添加的,必须自定义设计器才能把它过滤掉
properties.Remove("Locked");
properties.Remove("Size");
base.PostFilterProperties(properties);
}
} /// <summary>
///
/// </summary>
[Designer(typeof(CustomBarCodeDesigner))]
[ToolboxBitmap("Barcode.bmp")]
public partial class CustomBarCode : System.Windows.Forms.Label, ICustomTypeDescriptor
{
#region 变量区 static Hashtable FilterredPropertyNames = null; //存放要过滤的属性 private FontType mCustomFont = FontType.Code39; /// <summary>
/// 条码字体类型
/// </summary>
public enum FontType
{
Code39 = 0,
Code128
} #endregion #region 初始化 public CustomBarCode()
{
InitializeComponent(); //过滤不显示又不影响控件正常使用的属性
FilterredPropertyNames = new Hashtable();
FilterredPropertyNames.Add("AccessibleDescription", null);
FilterredPropertyNames.Add("AccessibleName", null);
FilterredPropertyNames.Add("AccessibleRole", null);
FilterredPropertyNames.Add("AllowDrop", null);
FilterredPropertyNames.Add("AutoEllipsis", null);
FilterredPropertyNames.Add("AutoSize", null);
FilterredPropertyNames.Add("BackColor", null);
FilterredPropertyNames.Add("BorderStyle", null);
FilterredPropertyNames.Add("ContextMenuStrip", null);
FilterredPropertyNames.Add("Cursor", null);
FilterredPropertyNames.Add("Enabled", null);
FilterredPropertyNames.Add("FlatStyle", null);
//FilterredPropertyNames.Add("Font", null);
FilterredPropertyNames.Add("ForeColor", null);
FilterredPropertyNames.Add("Image", null);
FilterredPropertyNames.Add("ImageAlign", null);
FilterredPropertyNames.Add("ImageIndex", null);
FilterredPropertyNames.Add("ImageKey", null);
FilterredPropertyNames.Add("ImageList", null);
FilterredPropertyNames.Add("Locked", null);
FilterredPropertyNames.Add("RightToLeft", null);
//FilterredPropertyNames.Add("Size", null);
//FilterredPropertyNames.Add("Text", null);
FilterredPropertyNames.Add("TextAlign", null);
FilterredPropertyNames.Add("UseCompatibleTextRendering", null);
FilterredPropertyNames.Add("UseMnemonic", null);
FilterredPropertyNames.Add("Visible", null);
FilterredPropertyNames.Add("Anchor", null);
FilterredPropertyNames.Add("CausesValidation", null);
FilterredPropertyNames.Add("ImeMode", null);
FilterredPropertyNames.Add("UseWaitCursor", null);
// location 不是过滤,过滤后, 不能调整位置
//FilterredPropertyNames.Add("Location", null);
FilterredPropertyNames.Add("Margin", null);
FilterredPropertyNames.Add("MaximumSize", null);
FilterredPropertyNames.Add("MinimumSize", null);
FilterredPropertyNames.Add("Padding", null);
FilterredPropertyNames.Add("TabIndex", null);
FilterredPropertyNames.Add("TabStop", null);
FilterredPropertyNames.Add("Dock", null);
FilterredPropertyNames.Add("DataBindings", null);
FilterredPropertyNames.Add("Tag", null); // 初始化固定属性
this.AutoSize = true;
this.Font = new Font("C39HrP48DmTt", 35); } #endregion #region 自定义属性 [Browsable(true)]
[DisplayName("字体")]
[DefaultValue(FontType.Code39)]
public FontType CustomFont
{
get
{
return mCustomFont;
}
set
{
mCustomFont = value; if (mCustomFont == FontType.Code39)
{
this.Font = new Font("C39HrP48DmTt", 35);
}
}
} [Browsable(true)]
[DisplayName("字体大小")]
[DefaultValue(35)]
public float CustomFontSize
{
get
{
return this.Font.Size;
}
set
{
Font newFont = new Font(this.Font.Name, value, this.Font.Style, this.Font.Unit);
this.Font = newFont;
}
} #endregion #region 属性过滤 #region 不能用属性过滤的, 重写属性, 让其在propertyGrid中不可见
[Browsable(false)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
} [Browsable(false)]
public new Point Location
{
get
{
Point point = base.Location;
return point;
}
set
{
base.Location = value;
}
} [Browsable(false)]
public override ISite Site
{
get
{
return base.Site;
}
set
{
base.Site = value;
}
} [DisplayName("文本")]
[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
#endregion
#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));
//return TypeDescriptor.GetProperties(typeof(CustomLabel), attributes);
} PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return this.FilterProperties(TypeDescriptor.GetProperties(this.GetType()));
//return TypeDescriptor.GetProperties(typeof(CustomLabel));
} object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
} #endregion
#region 属性过滤其它方法 ///// <summary>
///// 在添加到父控件后, 给控件命名, 在初始化时不能命名,因为这时还没有添加到父控件, this.Parent.Container为空
///// </summary>
///// <param name="e"></param>
//protected override void OnParentChanged(EventArgs e)
//{
// base.OnParentChanged(e);
// if (this.Parent != null)
// {
// this.Text = NameCreator.CreateName(this.Parent.Container, this.GetType());
// }
//} private PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection properties)
{
List<PropertyDescriptor> list = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in properties)
{
if (!FilterredPropertyNames.ContainsKey(pd.Name))
{
list.Add(pd);
}
}
return new PropertyDescriptorCollection(list.ToArray(), true);
} #endregion #endregion
}
}
逼得我重新注册了一个