Binding.Parse Event
Occurs when the value of a data-bound control changes.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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:
When the EndCurrentEdit method of the BindingManagerBase is called.
When the Current of the BindingManagerBase changes (in other words, when the Position changes).
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.