Binding.Format 事件

当将某控件的属性绑定到某个数据值时发生。

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

语法

声明
Public Event Format As ConvertEventHandler
用法
Dim instance As Binding
Dim handler As ConvertEventHandler

AddHandler instance.Format, handler
public event ConvertEventHandler Format
public:
event ConvertEventHandler^ Format {
    void add (ConvertEventHandler^ value);
    void remove (ConvertEventHandler^ value);
}
/** @event */
public void add_Format (ConvertEventHandler value)

/** @event */
public void remove_Format (ConvertEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

Format 事件在数据从数据源推入控件时引发。可以处理 Format 事件,以将数据源中的未格式化数据转换成用于显示的格式化数据。当数据从控件拉入到数据源中时,首先会引发 Parse 事件以取消所显示的值的格式,然后会发生 Format 事件,以便重新设置数据的格式以便于显示。这样可以确保绑定控件正确显示格式化数据,而无论用户在控件中输入的是格式化数据还是未格式化数据。

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

每当 BindingManagerBaseCurrent 值更改时均会发生 Format 事件,这包括:

  • 第一次绑定属性时。

  • Position 更改的任意时刻。

  • 每当对数据绑定列表进行排序或筛选时,该操作在 DataView 提供列表时完成。

Format 事件还在 Parse 事件之后发生。例如,当控件失去焦点时,将分析其内容。之后,当新数据推入控件时,会发生 Format 事件,从而可以设置新内容的格式。

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

示例

下面的代码示例创建一个 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
      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 Not cevent.DesiredType Is GetType(Decimal) Then
      Exit Sub
   End If

   ' Converts the string back to decimal using the static ToDecimal method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString, _
   NumberStyles.Currency, nothing)
End Sub

Private Sub BindControl
   ' Creates the binding first. The OrderAmount is a Decimal type.
   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
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 a Decimal type.
   Binding b = new Binding
      ("Text", ds, "customers.custToOrders.OrderAmount");
   // Add the delegates to the event.
   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 a Decimal type.
      Binding^ b = gcnew Binding(
         "Text",ds,"customers.custToOrders.OrderAmount" );
      
      // Add the delegates to the event.
      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 a Decimal type.
    Binding b = new Binding("Text", ds, 
        "customers.custToOrders.OrderAmount");
    // Add the delegates to the event.
    b.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    b.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl
private function DecimalToCurrencyString(sender, cevent : ConvertEventArgs)
{
   // The method converts only to string type. Test this using the DesiredType.
   if(cevent.DesiredType != String.GetType()) return;

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

private function CurrencyStringToDecimal(sender, cevent : ConvertEventArgs)
{
   // The method converts only to decimal type. 
   if(cevent.DesiredType != Decimal.GetType()) return;

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

private function BindControl()
{
   // Creates the binding first. The OrderAmount is a Decimal type.
   var b : Binding = new Binding
      ("Text", ds, "Suppliers.CompanyName");
   // Add the delegates to the event.
   b.add_Format(DecimalToCurrencyString);
   b.add_Parse(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

平台

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

请参见

参考

Binding 类
Binding 成员
System.Windows.Forms 命名空间
Parse
OnFormat