Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ConvertEventArgs.Value Property

Gets or sets the value of the ConvertEventArgs.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public Object Value { get; set; }

Property Value

Type: System.Object
The value of the ConvertEventArgs.

The value contained by the Value property depends on the event in which the ConvertEventArgs is returned. The ConvertEventArgs can be returned in either the Format event or the Parse event.

When the ConvertEventArgs is returned in the Format event, the Value property contains the unformatted property value of the data source. Within the Format event, you can read the property value, format the value, and reset the Value property to the new (formatted) value, thereby setting the value displayed in the data-bound control.

When the ConvertEventArgs is returned in the Parse event, the property contains the custom-formatted value of the data-bound control. Within the Parse event, you must read the formatted value, parse it, and convert it back to the same data type as the data source. You can then reset the Value property to the unformatted value, and thereby set the value of the data source. To determine the type of the data source, examine the DesiredType property value.

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

private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. Test this using the DesiredType. 
   if(cevent.DesiredType != typeof(string)) return;

   // Use the ToString method to format the value as currency ("c").
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
   // The method converts back to decimal type only.  
   if(cevent.DesiredType != typeof(decimal)) return;

   // Converts the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

private void BindControl()
{
   // Creates the binding first. The OrderAmount is typed as Decimal.
   Binding b = new Binding
   ("Text", ds, "customers.custToOrders.OrderAmount");
   // Adds the delegates to the events.
   b.Format += new ConvertEventHandler(DecimalToCurrencyString);
   b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

.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.

Show:
© 2014 Microsoft. All rights reserved.