ConvertEventHandler 委托

表示将处理 BindingParseFormat 事件的方法。

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

语法

声明
Public Delegate Sub ConvertEventHandler ( _
    sender As Object, _
    e As ConvertEventArgs _
)
用法
Dim instance As New ConvertEventHandler(AddressOf HandlerMethod)
public delegate void ConvertEventHandler (
    Object sender,
    ConvertEventArgs e
)
public delegate void ConvertEventHandler (
    Object^ sender, 
    ConvertEventArgs^ e
)
/** @delegate */
public delegate void ConvertEventHandler (
    Object sender, 
    ConvertEventArgs e
)
JScript 支持使用委托,但不支持进行新的声明。

参数

  • sender
    事件源。

备注

当创建 ConvertEventHandler 委托时,将标识处理事件的方法。若要使该事件与事件处理程序相关联,请将该委托的一个实例添加到事件中。除非移除了该委托,否则每当发生该事件时就调用事件处理程序。有关事件处理程序委托的更多信息,请参见 事件和委托

示例

下面的代码

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

Private Sub DecimalToCurrency(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 


Private Sub CurrencyToDecimal(sender As Object, cevent As ConvertEventArgs)
    ' The method converts only to decimal type. 
    If Not cevent.DesiredType Is GetType(Decimal) Then
        Return
    End If 
    ' Converts the string back to decimal using the static ToDecimal method.
    cevent.Value = Convert.ToDecimal(cevent.Value.ToString())
End Sub 


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 DecimalToCurrency
    AddHandler b.Parse, AddressOf CurrencyToDecimal
    text1.DataBindings.Add(b)
End Sub 
private void DecimalToCurrency(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 CurrencyToDecimal(object sender, ConvertEventArgs cevent)
{
   // ' The method converts only to decimal type. 
   if (cevent.DesiredType != typeof(decimal)) return;

   // Converts the string back to decimal using the static ToDecimal method.
   cevent.Value = Convert.ToDecimal(cevent.Value.ToString());
}

private void BindControl()
{
   // Creates the binding first. The OrderAmount is typed as Decimal.
   Binding b = new Binding
      ("Text", ds, "customers.custToOrders.OrderAmount");
   // Add the delegates to the events.
   b.Format += new ConvertEventHandler(DecimalToCurrency);
   b.Parse += new ConvertEventHandler(CurrencyToDecimal);
   text1.DataBindings.Add(b);
}
private:
   void DecimalToCurrency( 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 CurrencyToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // ' The method converts only to decimal type. 
      if ( cevent->DesiredType != Decimal::typeid )
      {
         return;
      }

      // Converts the string back to decimal using the static ToDecimal method.
      cevent->Value = Convert::ToDecimal( cevent->Value->ToString() );
   }

   void BindControl()
   {
      // Creates the binding first. The OrderAmount is typed as Decimal.
      Binding^ b = gcnew Binding(
         "Text",ds,"customers.custToOrders.OrderAmount" );
      
      // Add the delegates to the events.
      b->Format += gcnew ConvertEventHandler(
         this, &Form1::DecimalToCurrency );
      b->Parse += gcnew ConvertEventHandler(
         this, &Form1::CurrencyToDecimal );
      text1->DataBindings->Add( b );
   }
private void DecimalToCurrency(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"));
} //DecimalToCurrency

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

private void BindControl()
{
    // Creates the binding first. The OrderAmount is typed as Decimal.
    Binding b = new Binding("Text", ds, 
        "customers.custToOrders.OrderAmount");
    // Add the delegates to the events.
    b.add_Format(new ConvertEventHandler(DecimalToCurrency));
    b.add_Parse(new ConvertEventHandler(CurrencyToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl

平台

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

请参见

参考

System.Windows.Forms 命名空间