Binding.Format イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public: event ConvertEventHandler^ Format { void add (ConvertEventHandler^ value); void remove (ConvertEventHandler^ value); }

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); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からBinding.Format イベントを検索する場合は、下記のリンクをクリックしてください。

- Binding.Format イベントのページへのリンク