Binding.Format 事件

定义

当将某控件的属性绑定到某个数据值时发生。Occurs when the property of a control is bound to a data value.

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,将 @no__t 委托添加到第 2 @no__t @no__t 和第3个事件中,并通过 DataBindings 属性将 @no__t 4 添加到 @no__t 控件的 @no__t。The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and adds the Binding to the BindingsCollection of a TextBox control through the DataBindings property. 添加到 @no__t 1 事件的 @no__t 0 事件委托将绑定值(@no__t 2 类型)设置为使用 ToString 方法的货币格式。The DecimalToCurrencyString event delegate, added to the Format event, formats the bound value (a Decimal type) as currency using the ToString method. 添加到 @no__t 1 事件的 @no__t 0 事件委托将控件显示的值转换回 @no__t 类型。The CurrencyStringToDecimal event delegate, added to the Parse event, converts the value displayed by the control back to the Decimal type.

此示例假定存在一个名为 @no__t @no__t。This example assumes the presence of a DataSet named 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 事件。The Format event is raised when data is pushed from the data source into the control. 可以处理 @no__t 0 事件,将未格式化的数据从数据源转换为要显示的格式化数据。You can handle the Format event to convert unformatted data from the data source into formatted data for display. 将数据从控件请求到数据源时,将引发 Parse 事件以 unformat 显示的值,然后发生 @no__t 1 事件以便重新设置要显示的数据的格式。When data is pulled from the control into the data source, the Parse event is raised to unformat the displayed value, then the Format event occurs to reformat the data for display. 这可确保绑定控件显示格式正确的数据,而不管用户是否在控件中输入已设置格式或未格式化的数据。This ensures that the bound control displays correctly formatted data regardless of whether the user enters formatted or unformatted data in the control.

@No__t-0 和 @no__t 1 事件允许您创建用于显示数据的自定义格式。The Format and Parse events allow you to create custom formats for displaying data. 例如,如果表中的数据类型为 Decimal,则可以通过将 ConvertEventArgs 的 @no__t 属性设置为 Format 事件中的格式化值,以本地货币格式显示数据。For example, if the data in a table is of type Decimal, you can display the data in the local currency format by setting the Value property of the ConvertEventArgs to the formatted value in the Format event. 因此,必须 unformat 事件 @no__t 中显示的值。You must consequently unformat the displayed value in the Parse event.

每当 BindingManagerBase 的 @no__t 值发生更改时,就会发生 @no__t 0 事件,其中包括:The Format event occurs whenever the Current value of the BindingManagerBase changes, which includes:

  • 第一次绑定属性时。The first time the property is bound.

  • @No__t-0 更改时。Any time the Position changes.

  • 只要对数据绑定列表进行排序或筛选,就会在 DataView 提供列表时完成此操作。Whenever the data-bound list is sorted or filtered, which is accomplished when a DataView supplies the list.

@No__t 事件也发生在 @no__t 1 事件之后。The Format event also occurs after the Parse event. 例如,当控件失去焦点时,将分析其内容。For example, when a control loses focus, its contents are parsed. 此后,当新数据推送到控件中时,就会发生 Format 事件,从而允许对新内容进行格式设置。Immediately afterward, as new data is pushed into the control, the Format event occurs allowing the new contents to be formatted.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅