ConvertEventArgs 类

提供有关 FormatParse 事件的数据。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Class ConvertEventArgs
    Inherits EventArgs
用法
Dim instance As ConvertEventArgs
public class ConvertEventArgs : EventArgs
public ref class ConvertEventArgs : public EventArgs
public class ConvertEventArgs extends EventArgs
public class ConvertEventArgs extends EventArgs

备注

对于通过 Binding 对象进行数据绑定的 Windows 窗体控件所显示的值,可使用 ConvertEventArgs 进行格式化和取消它们的格式设置。每当将控件属性绑定到值时均会发生 Format 事件,每当绑定值更改时均会发生 Parse 事件。

Format 事件和 Parse 事件允许您创建自定义格式来显示数据。例如,如果某表中的数据是 Decimal 类型,可通过在 Format 事件中将 ConvertEventArgsValue 属性设置为格式化的值,指定数据应以本地货币格式显示。因此,必须取消 Parse 事件中显示的值的格式。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例创建一个 Binding,向 Parse 事件和 Format 事件添加 ConvertEventHandler 委托,并使用 DataBindings 属性向 TextBox 控件的 BindingsCollection 添加 Binding。添加到 Format 事件的 DecimalToCurrencyString 事件委托,使用 ToString 方法将绑定值(Decimal 类型)格式化为货币。添加到 Parse 事件的 CurrencyStringToDecimal 事件委托,将控件所显示的值回转为 Decimal 类型。

Private Sub DecimalToCurrencyString(sender As Object, cevent As ConvertEventArgs)
   ' The method converts only to string type. Test this using the DesiredType.
   If Not cevent.DesiredType Is GetType(String) Then
      Return
   End If 
   ' Use the ToString method to format the value as currency ("c").
   cevent.Value = CDec(cevent.Value).ToString("c")
End Sub 'DecimalToCurrencyString
 
 
Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
   ' The method converts back to decimal type only. 
   If Not cevent.DesiredType Is GetType(Decimal) Then
      Return
   End If 
   ' Converts the string back to decimal using the shared Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString, _
   NumberStyles.Currency, nothing)

End Sub 'CurrencyStringToDecimal
 
 
Private Sub BindControl()
   ' Creates the binding first. The OrderAmount is typed as Decimal.
   Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
   ' Adds the delegates to the events.
   AddHandler b.Format, AddressOf DecimalToCurrencyString
   AddHandler b.Parse, AddressOf CurrencyStringToDecimal
   text1.DataBindings.Add(b)
End Sub 'BindControl
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);
}
private:
   void DecimalToCurrencyString( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts only to string type. Test this using the DesiredType.
      if ( cevent->DesiredType != String::typeid )
      {
         return;
      }
      
      // Use the ToString method to format the value as currency ("c").
      cevent->Value = ( (Decimal^)(cevent->Value) )->ToString( "c" );
   }

   void CurrencyStringToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts back to decimal type only. 
      if ( cevent->DesiredType != Decimal::typeid )
      {
         return;
      }
      
      // Converts the string back to decimal using the static Parse method.
      cevent->Value = Decimal::Parse( cevent->Value->ToString(),
         NumberStyles::Currency, nullptr );
   }

   void BindControl()
   {
      // Creates the binding first. The OrderAmount is typed as Decimal.
      Binding^ b = gcnew Binding(
         "Text",ds,"customers.custToOrders.OrderAmount" );
      
      // Adds the delegates to the events.
      b->Format += gcnew ConvertEventHandler(
         this, &Form1::DecimalToCurrencyString );
      b->Parse += gcnew ConvertEventHandler(
         this, &Form1::CurrencyStringToDecimal );
      text1->DataBindings->Add( b );
   }
private void DecimalToCurrencyString(Object sender, ConvertEventArgs cevent)
{
    // The method converts only to string type. 
    // Test this using the DesiredType.
    if (!(cevent.get_DesiredType().Equals(String.class.ToType()))) {
        return ;
    }
    // Use the ToString method to format the value as currency ("c").
    cevent.set_Value(((System.Decimal)cevent.get_Value()).ToString("c"));
} //DecimalToCurrencyString

private void CurrencyStringToDecimal(Object sender, ConvertEventArgs cevent)
{
    // The method converts back to decimal type only. 
    if (!(cevent.get_DesiredType().Equals(System.Decimal.class.ToType()))) {
        return ;
    }
    // Converts the string back to decimal using the static Parse method.
    cevent.set_Value(Decimal.Parse(cevent.get_Value().ToString(), 
        NumberStyles.Currency, null));
} //CurrencyStringToDecimal

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.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    b.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl

继承层次结构

System.Object
   System.EventArgs
    System.Windows.Forms.ConvertEventArgs
       System.Windows.Forms.DataGridViewCellFormattingEventArgs
       System.Windows.Forms.DataGridViewCellParsingEventArgs
       System.Windows.Forms.ListControlConvertEventArgs

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ConvertEventArgs 成员
System.Windows.Forms 命名空间
Binding 类
BindingManagerBase 类
BindingsCollection 类
Control.DataBindings 属性