Click here to Skip to main content
15,867,835 members
Articles / Desktop Programming / Windows Forms
Article

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

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
23 Mar 20062 min read 54.8K   330   34   5
An article describing how you can add custom functionality to the BindingSource component.

Sample Image

Introduction

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.

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.

VB
Public Enum ReadOnlyBehavior
    Disable = 0
    Hide = 1
End Enum

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

VB
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.

VB
Public Property ReadOnlyControlBehavior() As ReadOnlyBehavior
    Get
        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.

VB
Protected Overrides Sub OnBindingComplete(ByVal e As _
          System.Windows.Forms.BindingCompleteEventArgs)

    '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 = _
        t.GetProperty(e.Binding.BindingMemberInfo.BindingMember)

    '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
    MyBase.OnBindingComplete(e)

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.

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


Written By
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.

Comments and Discussions

 
Questionchrome multidropdown Pin
Member 1309484830-Mar-17 3:08
Member 1309484830-Mar-17 3:08 
GeneralExcellent Pin
karenpayne9-Jun-14 11:55
karenpayne9-Jun-14 11:55 
General[Message Removed] Pin
immetoz4-Oct-08 15:06
immetoz4-Oct-08 15:06 
GeneralOutstanding! Pin
Scott Page19-Dec-07 16:31
professionalScott Page19-Dec-07 16:31 
QuestionHow to disable edit for DataGridView Pin
Locusta7419-Jan-07 9:17
Locusta7419-Jan-07 9: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.