如何隐藏继续控件的属性可见性

kugua70708 2009-03-28 01:52:37
自定义一个控件, 从 system.Windows.Forms 命名空间继续( 比如Label)
想把其大部分的属性在属性框中设计时不可见
应试怎么做, 最好能有个例子
谢谢!
...全文
714 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mofee999 2009-03-31
  • 打赏
  • 举报
回复
还想说一CSDN, 为了防止刷屏不让连续回复是不错, 可是应该加上个时间判断吧, 昨天不能回复, 今天还不能回复
逼得我重新注册了一个
mofee999 2009-03-31
  • 打赏
  • 举报
回复

#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
}
}
mofee999 2009-03-31
  • 打赏
  • 举报
回复
终于得到解决了,方法有许多种,可能需要根据不同需要综合使用
贴出代码,共享一下:

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

kugua70708 2009-03-29
  • 打赏
  • 举报
回复
楼上的是一种方法,不过需要写一大堆重写属性,要命的是这种方法只能针对能重写的属性
不能重写的属性,仍然会显示
kugua70708 2009-03-29
  • 打赏
  • 举报
回复
这种问题,很多开发自定义IDE, 自定义控件应该都会遇到吧, 好像很少见到有这样的贴子或讨论
kugua70708 2009-03-29
  • 打赏
  • 举报
回复
查了MSDN, 好像ICustomTypeDescriptor 只能用于组件, 不是方法被覆盖
对于控件就不知道如何处理了
kugua70708 2009-03-29
  • 打赏
  • 举报
回复
2楼和4楼的方法都具有可行性, 不过感觉不是很方便
在网上找了很多资料都没找到比较好的解决方法
在 codeProject 找到一个
http://www.codeproject.com/KB/miscctrl/bending_property.aspx
利用此类,让自已的控件继续接口ICustomTypeDescriptor
public partial class CustomLabel : System.Windows.Forms.Label, ICustomTypeDescriptor
用类组合的方法使用源码中的类, 目的是达到了
结果是这个控件变成了组件而不是可视控件, 估计是许多方法被覆盖了.

还有哪位大侠出来指点指点
efpsy 2009-03-29
  • 打赏
  • 举报
回复
在自定义类中将控件的visible属性设置为假,在程序运行中如果想显示它可以如下作:在类中定义一个私有变量和一个属性用变量对属性进行操作从而决定是否显示自定义控件
活靶子哥哥 2009-03-29
  • 打赏
  • 举报
回复
[Browsable(false)]这个Attribute贴到你的属性上
PandaIT 2009-03-29
  • 打赏
  • 举报
回复
不知道还有什么更好的办法?
gisyellow 2009-03-29
  • 打赏
  • 举报
回复
使用new修饰符隐藏不能重写的基类的属性,然后设置属性的特性,即2楼所述。
gomoku 2009-03-28
  • 打赏
  • 举报
回复

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; }
}
}
coodd 2009-03-28
  • 打赏
  • 举报
回复
好像做不到

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧