65.9K
CodeProject is changing. Read more.
Home

ReadOnlyController: Working with the ReadOnly property on controls

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Dec 18, 2007

CPOL

1 min read

viewsIcon

25572

downloadIcon

201

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;