Question formatting bound data

kpgraci

Active member
Joined
Feb 21, 2011
Messages
30
Location
New Orleans
Programming Experience
10+
I have a window form with a label.

On the form I have a dataset, bindingsoure, and tableadaptor (all created by the IDE in response to binding the text property of the label)

To fill the datatable, the IDE created this:

VB.NET:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000]Me.ClientsTableAdapter.Fill([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000].MyDataSet.Clients)[/COLOR][/SIZE][/FONT][/SIZE][/FONT]

Works fine.

The bound field is a phone number, stored in the db as a 10 digit numeric string

I need to format this with the standard ()-, and I have a function that will do just that.

How can I apply the formatting so the phone number appears in the label properly?

EDIT:

In the Advanced section of the databinding property of the label control, under custom formatting there is a disclaimer:

VB.NET:
In order to ensure that a custom-formatted value successfully displays in the control and is persisted to the database, handle the parse or format event for the binding.

This seems promising... How to I handle the format event for the binding???
 
Last edited:
I did this, and it works, but I don't know is this is the best way to do this.

VB.NET:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Private Sub ClientsBindingSource_BindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.BindingCompleteEventArgs) Handles ClientsBindingSource.BindingComplete
        If e.Binding.Control.Name = "PhoneLabel" Then
            Dim s As String = e.Binding.Control.Text
            If s.Length = 10 Then
                s = "(" & s.Substring(0, 3) & ") " & s.Substring(3, 3) & "-" & s.Substring(6)
                e.Binding.Control.Text = s
            End If
        End If
    End Sub
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
 
E.g.
Public Class Form1

    Private WithEvents phoneLabelBinding As Binding

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Get the binding for the Text property of the appropriate Label.
        Me.phoneLabelBinding = Me.Label1.DataBindings("Text")
    End Sub

    Private Sub phoneLabelBinding_Format(sender As Object, e As System.Windows.Forms.ConvertEventArgs) Handles phoneLabelBinding.Format
        'This event handler is executed when data is passed from the source to the control.
    End Sub

    Private Sub phoneLabelBinding_Parse(sender As Object, e As System.Windows.Forms.ConvertEventArgs) Handles phoneLabelBinding.Parse
        'This event handler is executed when data is passed from the control to the source.
    End Sub

End Class
If you apply custom formatting when data gets passed from the source to the control then you need to remove the formatting when the data goes from the control back to the source.
 
Back
Top