Cyclone
Cyclone

Reputation: 18295

Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode

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!

Upvotes: 2

Views: 5219

Answers (1)

Tom Juergens
Tom Juergens

Reputation: 4592

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.

Upvotes: 3

Related Questions