Click here to Skip to main content
14,084,843 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 10 Apr 2008
Licenced CPOL

A Simple Alternative to Windows Forms Data Binding

, 10 Apr 2008
Rate this:
Please Sign up or sign in to vote.
An open source library of Windows Forms controls that update themselves automatically.


While data binding is a powerful and convenient method for rendering database and object data in Windows Forms controls, it can get complex. The Update Controls library adopts a simpler approach. Instead of binding control data to properties, these controls fire events to get their data. An event can call whatever business logic is required. The control keeps track of the data that was accessed by that business logic, and when any of that data changes, the control fires the event again to update itself.

The GetText event

The UpdateTextBox control is a simple example. It fires an event called GetText that returns a string. You handle this event by returning the string that the text box should display.

private string firstNameTextBox_GetText()
    return _person.FirstName;

Whenever the FirstName property of the person is changed, the control will fire the event again to get the new text.

But controls don’t have to be bound directly to data. They can call business logic. For example, a label that displays the person’s full name would have this GetText event.

private string fullNameLabel_GetText()
    return _person.FullName;

Where the Person object defines FullName like this.

public string FullName
        return string.Format("{0} {1}", FirstName, LastName);

Now when either FirstName or LastName changes, the full name label updates itself.

The SetText event

When the user edits the text in an UpdateTextBox, it fires an event called SetText. As you can imagine, this one takes the string that the user just entered.

private void firstNameTextBox_SetText(string value)
    _person.FirstName = value;

Because this event changes the FirstName property, it causes the full name label to update itself.

But wait! I didn’t tell it that the first name text box and the full name label were related. There’s no code in there to force the full name to update when the first name is changed. You don’t need that code, because the Update Controls library figures out the relationship on its own.

The Dynamic sentry

The bit of magic that makes that happen is the Dynamic sentry. This object sits next to each of your data fields and monitors all access to that data. The Person object has two: one for first name and one for last name.

private string _firstName;
private string _lastName;

#region Dynamic properties
// Generated by Update Controls --------------------------------
private Dynamic _dynFirstName = new Dynamic();
private Dynamic _dynLastName = new Dynamic();

public string FirstName
    get { _dynFirstName.OnGet(); return _firstName; }
    set { _dynFirstName.OnSet(); _firstName = value; }

public string LastName
    get { _dynLastName.OnGet(); return _lastName; }
    set { _dynLastName.OnSet(); _lastName = value; }
// End generated code --------------------------------

Every time you get the value of a data field, you call its sentry’s OnGet. And every time you change it, you call OnSet. But don’t worry about writing all of this sentry code yourself. The Update Controls library includes a Visual Studio add-in that generates C# or VB code for data fields. Just select the fields you want to expose and hit Ctrl+D, G.

Just before Update Controls fires a Get event like GetText, it pushes a monitor to the stack. When you call Dynamic.OnGet, it looks at the stack to find that monitor. At this point, it knows that that control depends upon that data. After the event ends, it pops that monitor off the stack and remembers the set of Dynamic sentries that were accessed.

Then, when you call Dynamic.OnSet, it looks up all of the controls that accessed that sentry. It now knows that all of those controls are out-of-date. When you’re finished making all of your changes, each of those controls fires its Get event to update itself.

Other control types

The Update Controls library wraps all of the common Windows Forms controls. Each of these control types have one or more Get events that return the type of data that they display. For example, the UpdateCheckBox fires a GetChecked event that returns a Boolean.

Some controls display more than one piece of data. These fire several Get events for the different kinds of data they display. The UpdateListBox, for example, first fires GetItems to get a list of objects, then fires GetItemText for each item to get the text to display.

Most controls also fire a GetEnabled event. This event returns true when the control should be enabled, and false when it should be disabled. If the event is not handled, then the control is always enabled. This makes it easy to enable and disable controls in response to changes in other controls.

Try it out

Download the source code or binary installer from Then open the Visual Studio project attached to this article. I started some class-level documentation of the library at If you have any questions, send me an email at

The Update Controls library is licensed under the LGPL. The QuickDemo example application is licensed under the Code Project Open License.


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


About the Author

Michael L Perry
Architect Improving Enterprises
United States United States
Code is math. Michael writes about techniques for using mathematics to prove the correctness of code at He also maintains two open-source projects that let you write verifiable user interface code (Update Controls) and collaborative applications (Correspondence).

You may also be interested in...

Comments and Discussions

GeneralThe New Update Controls Pin
Qwertie9-Apr-11 10:18
memberQwertie9-Apr-11 10:18 
General3rd party controls Pin
scotru25-Mar-11 22:49
memberscotru25-Mar-11 22:49 
GeneralDemo project doesn't build Pin
Phil Herold15-Apr-08 3:34
memberPhil Herold15-Apr-08 3:34 
GeneralRe: Demo project doesn't build Pin
Michael L Perry16-Apr-08 1:37
memberMichael L Perry16-Apr-08 1:37 
GeneralSuggestion... Pin
Marc Clifton11-Apr-08 11:18
protectorMarc Clifton11-Apr-08 11:18 
GeneralRe: Suggestion... Pin
Michael L Perry11-Apr-08 17:25
memberMichael L Perry11-Apr-08 17:25 
GeneralRe: Suggestion... Pin
Marc Clifton12-Apr-08 2:40
protectorMarc Clifton12-Apr-08 2:40 
GeneralRe: Suggestion... Pin
Michael L Perry12-Apr-08 12:02
memberMichael L Perry12-Apr-08 12:02 

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 | Cookies | Terms of Use | Mobile
Web06 | 2.8.190518.1 | Last Updated 11 Apr 2008
Article Copyright 2008 by Michael L Perry
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid