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作成し、 イベントと Format イベントのParse両方にデリゲートを追加し、 プロパティを使用して コントロールの TextBox に をDataBindings追加BindingBindingsCollectionします。 イベントに追加されたイベント デリゲートはDecimalToCurrencyString、 メソッドをFormat使用してToStringバインドされた値 (型) をDecimal通貨として書式設定します。 イベントにParse追加されたイベント デリゲートはCurrencyStringToDecimal、コントロールによって表示される値を 型に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 含まれます。

  • プロパティを初めてバインドする場合。

  • が変更されるたびに Position

  • データ バインド リストが並べ替えまたはフィルター処理されるたびに、リストが提供されたときに DataView 実行されます。

イベントは Format 、 イベントの後にも発生します Parse 。 たとえば、コントロールがフォーカスを失うと、その内容が解析されます。 その直後に、新しいデータがコントロールにプッシュされると、イベントが Format 発生し、新しいコンテンツの書式設定が可能になります。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください