Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ReadOnlyController: Working with the ReadOnly property on controls

0.00/5 (No votes)
18 Dec 2007 1  
The ReadOnlyController is an IExtenderProvider used to easily set the ReadOnly property on controls.

Introduction

Many times, when working with Windows Forms applications, I have needed to set the ReadOnly property on many controls to the same values at different times. For example, if the user doesn't have the rights to edit an object, then the controls should all be read-only. Or perhaps, if the form is in a specific state, then some controls should be read-only while others shouldn't. You could write code to set the property on all of the controls, or make a helper method like this:

public void SetReadOnly(bool readOnly)
{
    textBox1.ReadOnly = readOnly;
    textBox2.ReadOnly = readOnly;
    button1.readOnly = readOnly;
    toolStripButton1.ReadOnly = readOnly;
}

However, this can be tedious to manage, and it would be nice to do this from the designer directly. The ReadOnlyController is a simple component that can be dropped on a form and can help manage the ReadOnly properties on controls.

Background

The ReadOnlyController is a component that can be dropped on a form in Design mode, and implements IExtenderProvider so that it can extend the properties on the controls on the form. Controls that have either a Readonly or Enabled property can be extended to enable control from the ReadOnlyController. See the screenshot below for how this might look in the designer:

Using the code

Simply drop the ReadOnlyController on your form, and since it is an IExtenderProvider, all controls on the form that have either a Readonly or Enabled property will be extended with the ability to control this property from the ReadOnlyController. Then, all you do is just set the ReadOnly property on the ReadOnlyController to the desired value, and all the controls that were set to EnableReadOnly on ReadOnlyController = true will have this property set.

// Set readOnlyController to ReadOnly
// to set all extended controls to ReadOnly as well
readOnlyController1.ReadOnly = true;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here