5,542,300 members and growing! (17,962 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

ReadOnlyController: Working with the ReadOnly property on controls

By Dan Rigsby

The ReadOnlyController is an IExtenderProvider used to easily set the ReadOnly property on controls.
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows (Windows, NT4, Win2K, WinXP, Win2003, Vista, TabletPC, Embedded)

Posted: 18 Dec 2007
Updated: 18 Dec 2007
Views: 2,280
Bookmarked: 4 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
votes for this Article.
Popularity: 0.00 Rating: 0.00 out of 5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Many times when working with a WindowForms application I have needed to set the ReadOnly property on many controls to the same values at different times. For example, if user doesnt have the right to edit an object, then the controls should all be readonly. Or perhaps if the form is in a specific state, then some controls should be readonly 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 woudl 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 these controls.

Background

The ReadOnlyController is a Component so that can be dropped on a form in design mode and implements IExtenderProvider so that it can extend the properties on controls on the form. Controls that have either a Readonly or Enabled property can be extended to enable control from the ReadOnly Controller. 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dan Rigsby


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.
Occupation: Team Leader
Company: Interactive Intelligence
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Search Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Dec 2007
Editor:
Copyright 2007 by Dan Rigsby
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project