共用方式為


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 加入 ConvertEventHandler 至 和 Format 事件,並使用 DataBindings 屬性將 加入 BindingBindingsCollection 至 控制項的 TextBoxDecimalToCurrencyString新增至 Format 事件的事件委派會 ToString 使用 方法,將系結值格式化 (Decimal 類型) 為貨幣。 新增 CurrencyStringToDecimalParse 事件的事件委派會將控制項所顯示的值轉換回 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 屬性值。

適用於

另請參閱