Click here to Skip to main content
13,293,474 members (64,869 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 23 Mar 2006

The BindingSource component to disable Windows Forms controls associated with ReadOnly properties

, 23 Mar 2006
Rate this:
Please Sign up or sign in to vote.
An article describing how you can add custom functionality to the BindingSource component.

Sample Image


Windows Forms databinding has greatly improved in .NET 2.0. Microsoft has enhanced our object-oriented toolbox by allowing Windows Forms controls to databind to our custom objects' properties. This new functionality is centered around the new BindingSource component. Although this component takes a lot of the leg-work out of databinding custom objects, I recently had an issue with some rather large objects that contained both Read-Write and ReadOnly properties. After setting up databinding, I had to go through and manually set the "Enabled" property of these controls depending on whether each individual property was ReadOnly or not. I decided to create a custom BindingSource component that would take care of this manual process in the background.


Another option that I considered exploring was creating a custom attribute to add to the properties that I wanted to have ReadOnly or Read-Write capabilities to. The reason I didn't do this was that I wanted to avoid requiring the business object developer having to implement more than what is necessary just to satisfy the UI developer.

Using the code

We start off by creating a class library that will contain our custom SmartPropertyBindingSource component and our ReadOnlyBehavior enumeration. This enumeration will be used to determine the rendering behavior of the databound control. This class library project should reference the System.Windows.Forms and set it to be a globally imported namespace (you may also just write Imports System.Windows.Forms at the top of the SmartPropertyBindingSource.vb file).

We will be supporting two options for how the databound control of the ReadOnly property will be rendered on the form. This is set through the ReadOnlyControlBehavior property in the SmartPropertyBindingSource. You can choose to have it disabled or hidden.

Public Enum ReadOnlyBehavior
    Disable = 0
    Hide = 1
End Enum

We will now add a class named SmartPropertyBindingSource which inherits from BindingSource.

Public Class SmartPropertyBindingSource
    Inherits BindingSource
End Class

Next, we will create a Public property in this class to set the control rendering behavior for ReadOnly properties.

Public Property ReadOnlyControlBehavior() As ReadOnlyBehavior
        Return mReadOnlyBehavior
    End Get
    Set(ByVal value As ReadOnlyBehavior)
        mReadOnlyBehavior = value
    End Set
End Property

We will also go ahead and provide overloads for the three constructors in the base class. Once we have done this, we are ready to write the functionality of the class. By overriding the OnBindingComplete, we will have access to the current object's property information, thanks to the System.Reflection namespace. With this information, we will be able to find out if the property in question is ReadOnly.

Protected Overrides Sub OnBindingComplete(ByVal e As _

    'get the current type of the object we are binding
    Dim t As Type = e.Binding.BindingManagerBase.Current.GetType

    'get the property information using the BindingMemberInfo object
    Dim prop As System.Reflection.PropertyInfo = _

    'see if it ReadOnly
    If Not prop.CanWrite Then

        'decide what to do with the control
        Select Case Me.ReadOnlyControlBehavior
            Case ReadOnlyBehavior.Disable
                e.Binding.Control.Enabled = False
            Case ReadOnlyBehavior.Hide
                e.Binding.Control.Visible = False
        End Select

    End If

    'continue with the method in the base class

End Sub

That's all there is to it. Using reflection, we can investigate whether the property is ReadOnly and render (or not render) the control appropriately. We also don't have to ask the domain object's developer to do anything special to improve the user experience.

Points of Interest

The new .NET 2.0 framework provides a great deal of enhanced functionality from the previous framework allowing for easier integration of custom business objects. We can build upon this functionality by customizing classes to better suit our needs.


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


About the Author

Michael Bosch
Web Developer
United States United States
Michael Bosch is a software applications developer specializing in .NET development for both Windows platform and ASP.NET web applications. He has been working on developing software applications in VB.NET for over 5 years. His experience also includes SQL Server database design and implementation. He is currently working on large enterprise integration projects using BizTalk server.

Michael graduated from the University of Florida in May 2002 with a BS in Decision and Information Sciences and is currently working as the Enterprise Applications Integration Architect for Brightstar Corporation in Miami, FL.

You may also be interested in...


Comments and Discussions

Questionchrome multidropdown Pin
Member 1309484830-Mar-17 4:08
memberMember 1309484830-Mar-17 4:08 
GeneralExcellent Pin
kevininstructor9-Jun-14 12:55
memberkevininstructor9-Jun-14 12:55 
General[Message Removed] Pin
immetoz4-Oct-08 16:06
memberimmetoz4-Oct-08 16:06 
GeneralOutstanding! Pin
R. Scott Page19-Dec-07 17:31
memberR. Scott Page19-Dec-07 17:31 
That's the kind of information I like to see posted! Not only does that prevent me from wasting my time hiding/disabling ReadOnly controls, but I now have time for one more beer!

Big Grin | :-D
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
( President Ronald Reagan)

QuestionHow to disable edit for DataGridView Pin
I fucking hate spam19-Jan-07 10:17
memberI fucking hate spam19-Jan-07 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 23 Mar 2006
Article Copyright 2006 by Michael Bosch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid