删除用户控件中的某些属性,即强制一个值并且在设计模式下不可编辑 [英] Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode

查看:13
本文介绍了删除用户控件中的某些属性,即强制一个值并且在设计模式下不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何基本上锁定默认属性,使用户无法编辑它?例如,如果我想锁定 BackColor 属性,我怎样才能让控件的最终用户无法编辑它?

How can I basically lock a default property so the user cannot edit it? For example, if I wanted to lock the BackColor property, how can I make it so the end user of the control can't edit it?

这是在 vb.net 2008 中.

This is in vb.net 2008.

感谢您的帮助!

推荐答案

从属性网格中移除属性就足够了,还是您真的想让它保持可见但被锁定?

Would removing the property from the property grid be enough, or do you really want to keep it visible but locked?

要删除它,您可以实现控件设计器并按如下方式处理 PreFilterProperties:

To remove it, you could implement a control designer and handle PreFilterProperties as follows:

Public Class MyControlDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
        MyBase.PreFilterProperties(properties)
        properties.Remove("BackColor")
    End Sub
End Class

<DesignerAttribute(GetType(MyControlDesigner))> _
Public Class MyControl
    ' ...
End Class

如果删除它不够好,那么通过这种方式锁定它也应该是可能的.您必须尝试将 ReadOnlyAttribute 分配给 BackColor 属性,也许首先将其从集合中删除,然后将其作为具有属性集的新属性添加回来.具体不知道,没试过,但我觉得你不能直接设置属性.

If removing it isn't quite good enough, just locking it should also be possible this way. You'd have to try to assign a ReadOnlyAttribute to the BackColor property, perhaps by first removing it from the collection then adding it back as a new property with the attribute set. Don't know exactly, haven't tried it out, but I don't think you'll be able to set the attribute directly.

这篇关于删除用户控件中的某些属性,即强制一个值并且在设计模式下不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆