ConvertEventArgs.DesiredType Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the data type of the desired value.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Type DesiredType { get; }

Property Value

Type: System.Type

The Type of the desired value.

The DesiredType property enables you to check the type of the property that the value is being converted to.

The following code example uses the DesiredType property to determine whether the conversion of one type to another can proceed. The DecimalToCurrencyString method tests whether the DesiredType is a string. If not, the code exits the method. Similarly, the CurrencyStringToDecimal method tests whether the DesiredType is a Decimal, and exits if it is not true.

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

   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

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

   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

.NET Framework
Available since 1.1
Return to top
Show: