Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Binding.Parse Event

Occurs when the value of a data-bound control changes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration
Public Event Parse As ConvertEventHandler

The Format and Parse events allow you to create custom formats for displaying data. For example, if the data in a table is of type Decimal, you can display the data in the local currency format by setting the Value property of the ConvertEventArgs to the formatted value in the Format event. You must consequently unformat the displayed value in the Parse event.

The Parse event occurs under the following conditions:

For more information about handling events, see Consuming Events.

The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and adds the Binding to the BindingsCollection of a TextBox control through the DataBindings property. The DecimalToCurrencyString event delegate, added to the Format event, formats the bound value (a Decimal type) as currency using the ToString method. The CurrencyStringToDecimal event delegate, added to the Parse event, converts the value displayed by the control back to the Decimal type.

Private Sub DecimalToCurrencyString(sender As Object, cevent As ConvertEventArgs)
    ' The method converts only to string type. Test this using the DesiredType. 
    If cevent.DesiredType IsNot GetType(String) Then 
        Exit Sub 
    End If 

    ' Use the ToString method to format the value as currency ("c").
    cevent.Value = CType(cevent.Value, decimal).ToString("c")
End Sub 

Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
    ' The method converts back to decimal type only. 
    If cevent.DesiredType IsNot GetType(Decimal) Then 
        Exit Sub 
    End If 

    ' Convert the string back to decimal using the shared Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString, _
  NumberStyles.Currency, nothing)

End Sub 

Private Sub BindControl
    ' Create the binding first. The OrderAmount is typed as Decimal. 
    Dim b As Binding = New Binding _
        ("Text", ds, "Customers.custToOrders.OrderAmount")
    ' Add the delegates to the event. 
    AddHandler b.Format, AddressOf DecimalToCurrencyString
    AddHandler b.Parse, AddressOf CurrencyStringToDecimal
    text1.DataBindings.Add(b)
End Sub

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.