New Posts New Posts RSS Feed: Tech Tip: Binding a Business Object Property To a Set of .NET RadioButtons
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Tech Tip: Binding a Business Object Property To a Set of .NET RadioButtons

 Post Reply Post Reply
Author
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Topic: Tech Tip: Binding a Business Object Property To a Set of .NET RadioButtons
    Posted: 06-Jun-2007 at 3:15pm
 
Level 100
DevForce Express

Binding a Business Object Property To a Set of .NET RadioButtons

RadioButtons, like the station selection buttons on a car radio, are designed to be used in a set. That set demands that the end user make a definitive choice among several mutually exclusive alternatives. In that regard, a set of RadioButtons presents the user with the same rules of usage put forward by a ComboBox, or a ListBox with its MultiSelect property set to False. But a set of RadioButtons represents a particularly elegant and intuitive control, and when you have the screen space for it, may provide the end user experience you really want.

It imposes a different set of coding demands, however, compared to the ComboBox and the ListBox, since the latter are individual controls, whereas a set of RadioButtons is a collection of individual controls. ASP.NET provides a RadioButtonList control that encapsulates the desired functionality in a single control, but there is no corresponding control in the WinForm control set. How on earth does one bind a single property to a collection of RadioButtons?

In raw .NET, you don't. Either you must use the Radio buttons unbound, writing code to check and uncheck them as you navigate among your business objects; or find (or create) a custom equivalent of the ASP.NET RadioBoxList.

With DevForce, you already have what you need. Here's how you bind to your RadioButton set:

  1. Drag a container control (GroupBox, Panel, etc.) on to the target Form or UserControl. 2.
  2. Populate the container control with a set of RadioButtons -- one for each of the possible values of the business object property you intend to bind.
  3. Set the value of the Tag property for one of the RadioButtons to one of the possible values of the targetted business object property. Do the same for the remaining RadioButtons, until each possible value that the business object property can take on is represented by exactly one RadioButton.

You can set the Tag property values in the Visual Studio WinForm designer, or in code. Here's a code example:

 
 

C#:

mRaceAmericanIndianRB.Tag = "American Indian";
mRaceAsianRB.Tag = "Asian";
mRaceAfricanAmericanRB.Tag = "African American";
mRaceNativeHawaiianRB.Tag = "Native Hawaiian";
mRaceWhiteRB.Tag = "White";
mRaceHispanicRB.Tag = "Hispanic";
mRaceOtherRB.Tag = "Other";



VB.NET:

mRaceAmericanIndianRB.Tag = "American Indian"
mRaceAsianRB.Tag = "Asian"
mRaceAfricanAmericanRB.Tag = "African American"
mRaceNativeHawaiianRB.Tag = "Native Hawaiian"
mRaceWhiteRB.Tag = "White"
mRaceHispanicRB.Tag = "Hispanic"
mRaceOtherRB.Tag = "Other"
 
(Incidentally, the Tag property is of type Object, so you can set it to any sort of values that you need to. If binding to a boolean property of a business object, for example, you could set the Tag properties of two RadioButtons to the boolean values true and false. If the Race categories above were stored as integer foreign keys, you could enter the numeric values or enum values representing them.)
 
4.

In code, use the AddGroup() method of a ControlBindingManager's Descriptors collection to set up the bindings.

 

 

C#:

ViewDescriptor raceViewDescriptor =
    new ViewDescriptor(EntityPropertyDescriptors.Employee.Race); mEmployeesCBM.Descriptors.AddGroup(mRaceGroupBox, raceViewDescriptor);


VB.NET:

Dim raceViewDescriptor As ViewDescriptor = _
    New ViewDescriptor(EntityPropertyDescriptors.Employee.Race) mEmployeesCBM.Descriptors.AddGroup(mRaceGroupBox, raceViewDescriptor)

Notice that the control targetted by the AddGroup() method is the container that houses the RadioButtons - in this case, a GroupBox. The method also requires a ViewDescriptor as a parameter, and a simple one such as that shown above will do. The ViewDescriptor's constructor requires only a PropertyDescriptor for the property to which you wish to bind the GroupBox. You can get that from the EntityPropertyDescriptors class (which is automatically generated by the DevForce Object Mapper).

That's all there is to it. The ControlBindingManager will see to it that the RadioButton whose Tag value matches the business object's bound property value will get selected as you navigate through your business objects!

 

 



Edited by IdeaBlade - 09-Jul-2007 at 12:19pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down