Binding.Format イベントとは? わかりやすく解説

Binding.Format イベント

コントロールプロパティデータ値にバインドすると発生します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event Format As ConvertEventHandler
Dim instance As Binding
Dim handler As ConvertEventHandler

AddHandler instance.Format, handler
public event ConvertEventHandler Format
public:
event ConvertEventHandler^ Format {
    void add (ConvertEventHandler^ value);
    void remove (ConvertEventHandler^ value);
}
/** @event */
public void add_Format (ConvertEventHandler
 value)

/** @event */
public void remove_Format (ConvertEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説

Format イベントは、データデータ ソースからコントロールプッシュされるときに発生しますFormat イベント処理することにより、データ ソース上の書式化されていないデータを、表示用の書式化されデータ変換できますデータコントロールかデータ ソースプルされるときには、Parse イベント発生し表示された値の書式解除されます。その後データ表示用に再度書式化するために Format イベント発生します。これにより、ユーザーコントロール入力したデータ書式化されているかどうかかかわらずバインド先のコントロールには正しく書式化されデータ表示されます。

Format イベントParse イベントによって、データ表示用のカスタム書式作成できます。たとえば、テーブル内のデータの型が Decimal場合は、ConvertEventArgs の Value プロパティに、Format イベント書式設定された値を設定することにより、ローカル通貨書式データ表示できますその後Parse イベントで、表示した値の書式解除する必要があります

BindingManagerBase の Current 値が変更されると、必ず Format イベント発生します。この値は、たとえば次のような場合変更されます。

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

イベント処理詳細については、「イベント利用」を参照してください

使用例使用例

Binding作成し、ConvertEventHandler デリゲートParse イベントFormat イベント両方追加し作成した Binding を DataBindings プロパティ使用して TextBox コントロールの BindingsCollection に追加する例を次に示しますDecimalToCurrencyString イベント デリゲートFormat イベント追加されToString メソッド使用してバインドされた値 (Decimal 型) を通貨として書式設定ます。CurrencyStringToDecimal イベント デリゲートParse イベント追加されコントロールによって表示される値を Decimal 型に変換します

Private Sub DecimalToCurrencyString(sender
 As Object, cevent As _
ConvertEventArgs)
   ' The method converts only to string type. Test this using the DesiredType.
   If Not cevent.DesiredType Is
 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 Not cevent.DesiredType Is
 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
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:
   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.get_DesiredType().Equals(String.class.ToType()))
 {
        return;
    }
    // Use the ToString method to format the value as currency ("c").
    cevent.set_Value(((System.Decimal)(cevent.get_Value())).ToString("c"));
} //DecimalToCurrencyString

private void CurrencyStringToDecimal(Object
 sender, ConvertEventArgs cevent)
{
    // The method converts back to decimal type only. 
    if (!cevent.get_DesiredType().Equals(System.Decimal.class.ToType()))
 {
        return;
    }
    // Converts the string back to decimal using the static Parse method.
    cevent.set_Value(Decimal.Parse(cevent.get_Value().ToString(), 
        NumberStyles.Currency, null));
} //CurrencyStringToDecimal

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.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    b.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl
private function DecimalToCurrencyString(sender,
 cevent : ConvertEventArgs)
{
   // The method converts only to string type. Test this using the DesiredType.
   if(cevent.DesiredType != String.GetType()) return;

   cevent.Value = (Decimal(cevent.Value)).ToString("c"); 
}

private function CurrencyStringToDecimal(sender,
 cevent : ConvertEventArgs)
{
   // The method converts only to decimal type. 
   if(cevent.DesiredType != Decimal.GetType()) return;

   // Converts the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

private function BindControl()
{
   // Creates the binding first. The OrderAmount is a Decimal type.
   var b : Binding = new Binding
      ("Text", ds, "Suppliers.CompanyName");
   // Add the delegates to the event.
   b.add_Format(DecimalToCurrencyString);
   b.add_Parse(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からBinding.Format イベントを検索した結果を表示しています。
Weblioに収録されているすべての辞書からBinding.Format イベントを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からBinding.Format イベント を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Binding.Format イベント」の関連用語

Binding.Format イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Binding.Format イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS