views:

150

answers:

1

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?

This is in vb.net 2008.

Thanks for the help!

+1  A: 

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

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

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.

Tom Juergens
Thanks, that worked!
Cyclone
Edited code - had left out MyBase.PreFilterProperties(properties), which is important (MSDN: If you override the PreFilterProperties method, call the base implementation before you perform your own filtering.)
Tom Juergens