Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C#
Article

ReadOnlyController: Working with the ReadOnly property on controls

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Dec 2007CPOL1 min read 25.3K   201   14  
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:

C#
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:

Image 1

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.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Interactive Intelligence
United States United States
I am a Team Lead and Senior Software Developer for Interactive Intelligence in Indianapolis, Indiana, USA. I ampretty much exclusively a C# developer but worksquite a bit with javascript, xml, xslt, sql, and other “buzzword” acronyms working in the full gambit of .net development including winforms, webforms, middle teir, database, communication layers, etc.

I also volunteer as the Logisitics Director at the local .Net User’s Group: IndyNDA and stay pretty active in the developer community in Indianapolis.

Comments and Discussions

 
-- There are no messages in this forum --