语言

Binding.Format 活动

定义

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

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 

事件类型

示例

下面的代码示例创建一个BindingConvertEventHandler委托,将委托添加到Parse事件Format,并通过属性将委托添加到BindingBindingsCollectionTextBox控件DataBindings中。 添加到DecimalToCurrencyStringFormat事件的事件委托使用该方法将绑定值(类型Decimal)格式设置为货币ToString。 CurrencyStringToDecimal添加到Parse事件的事件委托将控件显示的值转换回Decimal类型。

此示例假定存在已命名DataSet的 ds 。

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,可以通过在事件中将格式化值Value的属性ConvertEventArgs设置为Format本地货币格式来显示数据。 因此,必须在事件中 Parse 取消显示值格式。

Format每当Current更改的值BindingManagerBase(包括:

  • 第一次绑定属性。

  • 随时 Position 更改。

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

该 Format 事件也会在事件发生后 Parse 发生。 例如,当控件失去焦点时,将分析其内容。 紧接着,当新数据推送到控件中时,事件发生 Format 时允许设置新内容的格式。

有关处理事件的详细信息,请参阅 处理和引发事件。

适用于

另请参阅