ConvertEventArgs.Value プロパティ

定義

ConvertEventArgs の値を取得します。値の設定も可能です。

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

プロパティ値

ConvertEventArgs の値です。

次のコード例では、 Bindingを作成し、 イベントと Format イベントの両方Parseにデリゲートを追加ConvertEventHandlerし、 プロパティをDataBindings使用して コントロールの TextBox に をBindingBindingsCollection追加します。 イベント デリゲートは DecimalToCurrencyString 、イベントに Format 追加され、 メソッドを 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" );
      
      // 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 によって異なります。 はConvertEventArgs、 イベントまたは Parse イベントのいずれかでFormat返すことができます。

ConvertEventArgsが イベントでFormat返されると、 Value プロパティにはデータ ソースの書式設定されていないプロパティ値が含まれます。 Formatイベント内では、プロパティ値の読み取り、値の書式設定、プロパティのValue新しい (書式設定された) 値へのリセットを行い、データ バインド コントロールに表示される値を設定できます。

ConvertEventArgsが イベントでParse返されると、 プロパティには、データ バインド コントロールのカスタム形式の値が含まれます。 イベント内で Parse 、書式設定された値を読み取り、解析し、データ ソースと同じデータ型に変換する必要があります。 その後、プロパティを Value 書式設定されていない値にリセットして、データ ソースの値を設定できます。 データ ソースの種類を確認するには、プロパティ値を DesiredType 調べます。

適用対象

こちらもご覧ください