• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

Windows forms; lock properties in a derivated object

I work with VS2010.
In my application I hav a Main Form with the button "btn1". From this Mainform I have the deivatied form Form1.
I declerated the btn1 ad protected, becaus I want to modify it in Form1. How can I make some of the proporties of btn1 unchangable in the Form1? (f.ex. the position and the size shouldn't mustn't be changed in Form1)
  • 3
2 Solutions
instead of exposing btn1 - expose only the properties you want to be able to change.

protected string btn1text
get { return btn1.text; }
set { btn1.text = value; }
ICSAutomationAuthor Commented:
And how can i do this with the event handlers?

Actually I'm looking for a way, where I still can use the porperties in the Propertywindow of the VS. Some of the properties are editable and some are not...
Some event I can create and some not...

to expose an event

public event EventHandler btn1Click
        add { btn1.Click += value; }
        remove { btn1.Click -= value; }
You will see the properties and events on the form  but you can add a category and description to find them easily

[Description("Text to display on button 1")]
protected string btn1text ...

[Description("Event for when button 1 is clicked. ")]
public event EventHandler btn1Click...

To make a property readonly in the Properties window:

public int Width
    get; set;

When this property is seen in the Properties window, it will be readonly. This is ok for our classes where we can add this ReadOnly attribute to a property. For the types which we dont have the code, we can set ReadOnly attribute at runtime as shown in the code below.

I used the below code once...just posting here...see if this is useful for you in some way.

// Make FindButton's Size property readonly in Properties window

PropertyDescriptor pd = TypeDescriptor.GetProperties(FindButton)["Size"];
AttributeCollection attributes = pd.Attributes;
ReadOnlyAttribute readOnlyAttribute = (ReadOnlyAttribute)attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = readOnlyAttribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(readOnlyAttribute, true);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now