ConvertEventArgs.Value ConvertEventArgs.Value ConvertEventArgs.Value ConvertEventArgs.Value Property

定義

ConvertEventArgs の値を取得または設定します。Gets or sets the value of the ConvertEventArgs.

public:
 property System::Object ^ Value { System::Object ^ get(); void set(System::Object ^ value); };
public object Value { get; set; }
member this.Value : obj with get, set
Public Property Value As Object

プロパティ値

ConvertEventArgs の値です。The value of the ConvertEventArgs.

次のコード例ではBinding、を作成ConvertEventHandlerし、 Binding DataBindings Parse Format イベントとTextBoxイベントの両方にデリゲートを追加し、プロパティを使用して、のにを追加します。BindingsCollection制御.The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and uses the DataBindings property to add the Binding to the BindingsCollection of a TextBox control. イベントDecimalToCurrencyString ToStringに追加されるイベントデリゲートは、 Decimalメソッドを使用して、バインドされた値 (型) を通貨として書式設定します。 FormatThe DecimalToCurrencyString event delegate, which is added to the Format event, uses the ToString method to format the bound value (a Decimal type) as currency. イベントCurrencyStringToDecimal Decimalに追加されるイベントデリゲートは、コントロールによって表示される値を型に変換します。 ParseThe CurrencyStringToDecimal event delegate, which is added to the Parse event, converts the value displayed by the control back to the Decimal type.

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" );
      
      // Adds the delegates to the events.
      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");
   // Adds the delegates to the events.
   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 Not cevent.DesiredType Is GetType(String) Then
      Return
   End If 
   ' Use the ToString method to format the value as currency ("c").
   cevent.Value = CDec(cevent.Value).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
      Return
   End If 
   ' Converts 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()
   ' Creates the binding first. The OrderAmount is typed as Decimal.
   Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
   ' Adds the delegates to the events.
   AddHandler b.Format, AddressOf DecimalToCurrencyString
   AddHandler b.Parse, AddressOf CurrencyStringToDecimal
   text1.DataBindings.Add(b)
End Sub

注釈

Valueプロパティに含まれる値は、 ConvertEventArgsが返されるイベントによって異なります。The value contained by the Value property depends on the event in which the ConvertEventArgs is returned. は、 FormatイベントまたParseはイベントで返すことができます。ConvertEventArgsThe ConvertEventArgs can be returned in either the Format event or the Parse event.

イベントでが返されると、プロパティにValueは、データソースの書式設定されていないプロパティ値が含まれます。 ConvertEventArgs FormatWhen the ConvertEventArgs is returned in the Format event, the Value property contains the unformatted property value of the data source. イベント内では、プロパティ値を読み取り、値を書式設定し、 Valueプロパティを新しい (書式設定された) 値にリセットして、データバインドコントロールに表示される値を設定できます。 FormatWithin the Format event, you can read the property value, format the value, and reset the Value property to the new (formatted) value, thereby setting the value displayed in the data-bound control.

イベントでが返された場合、プロパティには、データバインドコントロールのカスタム書式設定された値が含まれます。 ConvertEventArgs ParseWhen the ConvertEventArgs is returned in the Parse event, the property contains the custom-formatted value of the data-bound control. Parseイベント内では、書式設定された値を読み取って解析し、データソースと同じデータ型に変換し直す必要があります。Within the Parse event, you must read the formatted value, parse it, and convert it back to the same data type as the data source. その後、 Valueプロパティを書式設定されていない値にリセットし、データソースの値を設定できます。You can then reset the Value property to the unformatted value, and thereby set the value of the data source. データソースの種類を確認するには、プロパティDesiredTypeの値を調べます。To determine the type of the data source, examine the DesiredType property value.

適用対象

こちらもご覧ください