5,693,062 members and growing! (17,873 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Windows Forms     Intermediate License: The Code Project Open License (CPOL)

A Simple Alternative to Windows Forms Data Binding

By Michael L Perry

An open source library of Windows Forms controls that update themselves automatically.
C# (C# 1.0, C# 2.0, C# 3.0, C#), VB 7.x, VB 8.0, VB 9.0, VB, Windows, WinForms, Dev

Posted: 10 Apr 2008
Updated: 10 Apr 2008
Views: 6,774
Bookmarked: 7 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.38 Rating: 3.40 out of 5
0 votes, 0.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
3 votes, 60.0%
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

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
{
    get
    {
        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 --------------------------------
#endregion 

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 http://codeplex.com/updatecontrols. Then open the Visual Studio project attached to this article. I started some class-level documentation of the library at http://updatecontrols.net/cs/documentation.shtml. If you have any questions, send me an email at support@updatecontrols.net.

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

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


Michael blogs at http://adventuresinsoftware.com, where he discusses lessons learned on the road to quality computing. The blog is all about observations, mistakes, and a-ha moments that have lead to improvements in his career.

Michael also created a new way of Windows Forms programming at http://updatecontrols.net. If you hate data binding, you'll love this. Instead of setting properties to bind your controls directly to tables, you handle events. The text box, for example, fires the GetText event, from which you return the text to display. You can call through your own business logic. And when the underlying data changes, the event is fired again ... automatically.

It's a lot like using a spreadsheet. You just specify the formula and the program figures out when to update it. But it's better than a spreadsheet because the formula is real code. It can even be a call to your business logic tier.

Please come by and drop me a note.
http://adventuresinsoftware.com
http://updatecontrols.net
Occupation: Architect
Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralDemo project doesn't buildmemberPhil Herold4:34 15 Apr '08  
GeneralRe: Demo project doesn't buildmemberMichael L Perry2:37 16 Apr '08  
GeneralSuggestion...supporterMarc Clifton12:18 11 Apr '08  
GeneralRe: Suggestion...memberMichael L Perry18:25 11 Apr '08  
GeneralRe: Suggestion...supporterMarc Clifton3:40 12 Apr '08  
GeneralRe: Suggestion...memberMichael L Perry13:02 12 Apr '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 10 Apr 2008
Editor:
Copyright 2008 by Michael L Perry
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project