次の方法で共有


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を作成し、Parse イベントとFormat イベントの両方にConvertEventHandler デリゲートを追加し、DataBindings プロパティを使用して、TextBox コントロールのBindingsCollectionBindingを追加します。 Format イベントに追加される DecimalToCurrencyString イベント デリゲートは、ToString メソッドを使用して、バインドされた値 (Decimal型) を通貨として書式設定します。 Parse イベントに追加される CurrencyStringToDecimal イベント デリゲートは、コントロールによって表示される値を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は、Format イベントまたはParse イベントのいずれかで返すことができます。

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

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

適用対象

こちらもご覧ください