1. Computing

Formatting Strings, Numbers, and Other Objects

Custom Formatting and Data Binding

By

One "special case" of custom formatting involves (usually) databases. It can also involve other data sources, but the idea is the same, so we'll work with a database here.

The problem is that data is often saved in a database in some format that isn't even close to the way you want it displayed. (Database data types are just weird!) So you very often want to automatically convert the data whenever it's retrieved from the database and converted back again whenever it's saved in the database. VB.NET does the job for you.

The way to do this is to add your own Handler to the Format and the Parse event of the Binding object. You're familiar with handers. The last clause of event code (such as the Load event of a Form) is:


... Handles MyBase.Load

That means this subroutine is called whenever the Form is loaded.

A Binding object has a Format event and a Parse event. The AddHandler statement lets you code subroutines that are called ...

Format - "when the property of a control is bound to a data value"

Parse - "when the value of a data-bound control changes"

So, if you have a control - in the example below, I use a TextBox - that is bound to a data source, you can write code to format the data automatically and unformat it automatically again.

For this example, I use the RecipeDataSource project from Part Six of the About Visual Basic series on databases: Coding the VB.NET Many-to-Many Database App. (You may want to read that article ... or the whole series ... to get the most out of this explanation.) In the app developed for that article, a Textbox named IngredientTextBox is bound to a field in the Ingredients table in a SQL Server database. Automatically formatting IngredientTextBox requires two pieces of code:

1) A subroutine that will be called by the handler added to the Binding.Format event.

2) The code to add a Binding object.

3) The code to add the handler.

The reverse operation ... un-formatting again ... requires another subroutine and a similar line of code to add a handler to the Binding.Parse event. the same Binding object (item 2 above) is used.

To illustrate formatting process, the code I developed capitalizes the first word in an Ingredient and saves new data as lower case again. To do this, two subroutines added to the RecipeDataSource project:


Private Sub FormatIngredient(
    ByVal sender As Object,
    ByVal e As ConvertEventArgs)
    Dim splitIngredient() As String =
        Split((e.Value.ToString).Trim, " ", 2)
    If splitIngredient.Length > 1 Then
        e.Value =
            UCase(splitIngredient(0)) & " " &
            splitIngredient(1)
    End If
End Sub
Private Sub UnFormatIngredient(
    ByVal sender As Object,
    ByVal e As ConvertEventArgs)
    e.Value = LCase(e.Value.ToString)
End Sub

The Binding object and the code to add both handlers are added to the Load event of the form:


Dim b As Binding =
    IngredientTextBox.DataBindings("Text")
AddHandler b.Format, AddressOf FormatIngredient
AddHandler b.Parse, AddressOf UnFormatIngredient

After that, it all happens automatically ...

--------
Click Here to display the illustration
--------

On the next page, we look at yet another way to trigger formatting, the ToString method.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Formatting Strings, Numbers, and Other Objects

©2014 About.com. All rights reserved.