Export (0) Print
Expand All

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 Handling and Raising 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.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft