Binding.Format 事件 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当将某控件的属性绑定到某个数据值时发生。
public:
 event System::Windows::Forms::ConvertEventHandler ^ Format;public event System.Windows.Forms.ConvertEventHandler Format;public event System.Windows.Forms.ConvertEventHandler? Format;member this.Format : System.Windows.Forms.ConvertEventHandler Public Custom Event Format As ConvertEventHandler 事件类型
示例
下面的代码示例创建 一个 Binding,将委托添加到 ConvertEventHandlerParse 和 Format 事件,并通过 属性将 BindingsCollection 添加到Binding控件DataBindings的 TextBox 。 
              DecimalToCurrencyString添加到 Format 事件的事件委托使用 ToString 方法将类型 (Decimal绑定值) 格式化为货币。 
              CurrencyStringToDecimal添加到 事件的事件Parse委托将控件显示的值转换回 类型Decimal。
此示例假定存在名为 ds的 DataSet 。
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.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 Sub DecimalToCurrencyString(sender As Object, cevent As _
ConvertEventArgs)
   ' The method converts only to string type. Test this using the DesiredType.
   If cevent.DesiredType IsNot 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 cevent.DesiredType IsNot 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
注解
当 Format 数据从数据源推送到控件中时,将引发 该事件。 可以处理 事件, Format 将数据源中的无格式数据转换为格式化数据以供显示。 将数据从 控件拉取到数据源中时, Parse 事件将引发为未格式化显示值,然后 Format 发生事件以重新设置要显示的数据格式。 这可确保绑定控件显示格式正确的数据,无论用户是在控件中输入格式化数据还是未格式化数据。
Format和 Parse 事件允许你创建自定义格式来显示数据。 例如,如果表中的数据的类型为 Decimal,则可以通过在 事件中将 的 ConvertEventArgs 属性设置为Value格式化值Format,以本地货币格式显示数据。 因此,必须在 事件中 Parse 取消显示的值格式。
Format每当Current更改值BindingManagerBase时,将发生 该事件,其中包括:
事件 Format 也会在 Parse 事件之后发生。 例如,当控件失去焦点时,会分析其内容。 紧接着,当新数据推送到控件中时, Format 发生 允许设置新内容格式的事件。
有关处理事件的详细信息,请参阅 处理和引发事件。