次の方法で共有


ConvertEventArgs.Value プロパティ

ConvertEventArgs オブジェクトの値を取得または設定します。

Public Property Value As Object
[C#]
public object Value {get; set;}
[C++]
public: __property Object* get_Value();public: __property void set_Value(Object*);
[JScript]
public function get Value() : Object;public function set Value(Object);

プロパティ値

ConvertEventArgs オブジェクトの値。

解説

Value プロパティによって格納される値は、 ConvertEventArgs オブジェクトが返されるイベントによって異なります。 ConvertEventArgs オブジェクトは、 Format イベントまたは Parse イベントのいずれかで返されます。

ConvertEventArgs オブジェクトが Format イベントで返されると、 Value プロパティにはデータ ソースの書式化されていないプロパティ値が格納されます。 Format イベント内で、このプロパティ値を読み込み、その値に書式を設定して、 Value プロパティを新しい (書式設定された) 値にリセットします。この操作によって、データ連結コントロールで表示する値が設定されます。

ConvertEventArgs オブジェクトが Parse イベントで返されると、プロパティにはデータ連結コントロールの書式設定のカスタム値が格納されます。 Parse イベント内で、この書式設定された値を読み込み、解析して、データ ソースと同じデータ型に変換する必要があります。その後、 Value プロパティを書式化されていない値にリセットできます。この操作によってデータ ソースの値が設定されます。データ ソースの型を判断するには、 DesiredType プロパティ値を調べます。

使用例

[Visual Basic, C#, C++] 次の例では、 Binding を作成し、 ConvertEventHandler デリゲートを Parse イベントと Format イベントの両方に追加し、 DataBindings プロパティを使用して BindingTextBox コントロールの 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
      Return
   End If 
   ' Use the ToString method to format the value as currency ("c").
   cevent.Value = CDec(cevent.Value).ToString("c")
End Sub 'DecimalToCurrencyString
 
 
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 'CurrencyStringToDecimal
 
 
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 'BindControl

[C#] 
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);
}


[C++] 
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 = (dynamic_cast<Decimal*> (cevent->Value))->ToString(S"c");
}

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 = __box(Decimal::Parse(cevent->Value->ToString(),
   NumberStyles::Currency, 0));
}

void BindControl()
{
   // Creates the binding first. The OrderAmount is typed as Decimal.
   Binding* b = new Binding
   (S"Text", ds, S"customers.custToOrders.OrderAmount");
   // Adds the delegates to the events.
   b->Format += new ConvertEventHandler(this, &Form1::DecimalToCurrencyString);
   b->Parse += new ConvertEventHandler(this, &Form1::CurrencyStringToDecimal);
   text1->DataBindings->Add(b);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ConvertEventArgs クラス | ConvertEventArgs メンバ | System.Windows.Forms 名前空間 | Binding