共用方式為


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 

事件類型

範例

以下程式碼範例建立 Binding,為 ConvertEventHandler 和 事件加入代理ParseFormat,並透過屬性DataBindingsBinding 加入控制項TextBox的 。BindingsCollection DecimalToCurrencyString事件Format代理加入事件後,會利用該ToString方法將綁定值(型Decimal態)格式化為貨幣。 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 事件重新格式化資料以供顯示。 這確保綁定控制項無論使用者輸入格式化或未格式化資料,都能正確顯示格式化的資料。

FormatParse事件允許你建立自訂的顯示格式。 例如,如果資料表中的資料型別為 Decimal,你可以透過將 的ConvertEventArgs屬性設定Value為事件中的Format格式值來顯示當地貨幣格式。 因此,你必須取消事件中顯示的數值 Parse 格式化。

當變動值BindingManagerBase發生Current時,Format事件發生,包括:

  • 第一次財產被綁定。

  • 隨時 Position 都有變動。

  • 每當資料綁定清單被排序或篩選時,即當 a DataView 提供清單時,這會被完成。

Format事件發生在事件之後Parse。 例如,當控制項失去焦點時,其內容會被解析。 緊接著,當新資料被推入控制項時, Format 事件會發生,允許格式化新內容。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱