Binding.Parse イベント
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ バインド コントロールの値が変更されたときに発生します。
public:
event System::Windows::Forms::ConvertEventHandler ^ Parse;
public event System.Windows.Forms.ConvertEventHandler Parse;
public event System.Windows.Forms.ConvertEventHandler? Parse;
member this.Parse : System.Windows.Forms.ConvertEventHandler
Public Custom Event Parse As ConvertEventHandler
イベントの種類
例
次のコード例では、Bindingを作成し、Parse イベントとFormat イベントの両方にConvertEventHandler デリゲートを追加し、DataBindings プロパティを使用してTextBox コントロールのBindingsCollectionにBindingを追加します。
Format イベントに追加された DecimalToCurrencyString イベント デリゲートは、ToString メソッドを使用して、バインドされた値 (Decimal型) を通貨として書式設定します。
CurrencyStringToDecimal イベント デリゲートは、Parse イベントに追加され、コントロールによって表示される値をDecimal型に戻します。
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 typed as Decimal.
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 typed as Decimal.
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
' Convert the string back to decimal using the shared Parse method.
cevent.Value = Decimal.Parse(cevent.Value.ToString, _
NumberStyles.Currency, nothing)
End Sub
Private Sub BindControl
' Create the binding first. The OrderAmount is typed as Decimal.
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イベントとParseイベントを使用すると、データを表示するためのカスタム形式を作成できます。 たとえば、テーブル内のデータがDecimal型の場合は、ConvertEventArgsの Value プロパティを Format イベントの書式設定された値に設定することで、現地通貨形式でデータを表示できます。 その結果、 Parse イベントに表示される値の書式設定を解除する必要があります。
Parse イベントは、次の条件下で発生します。
BindingManagerBaseのEndCurrentEdit メソッドが呼び出されたとき。
BindingManagerBaseのCurrentが変更されたとき (つまり、Positionが変更されたとき)。
イベントの処理の詳細については、「処理とイベントの発生」を参照してください。