共用方式為


ConvertEventArgs 類別

定義

提供 FormatParse 事件的資料。

public ref class ConvertEventArgs : EventArgs
public class ConvertEventArgs : EventArgs
type ConvertEventArgs = class
    inherit EventArgs
Public Class ConvertEventArgs
Inherits EventArgs
繼承
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

備註

ConvertEventArgs是用來格式化和取消格式化由透過 物件系結至資料 Binding 之Windows Forms控制項所顯示的值。 Format每當控制項屬性系結至值,而且每當系結值變更時,就會 Parse 發生此事件。

FormatParse 事件可讓您建立自訂格式來顯示資料。 例如,如果資料表中的資料屬於 類型 Decimal ,您可以指定應該以本機貨幣格式顯示資料,方法是將 的 屬性 ConvertEventArgs 設定 Value 為 事件中的 Format 格式化值。 因此,您必須取消格式化事件中顯示的 Parse 值。

如需處理事件的詳細資訊,請參閱 處理和引發事件

建構函式

ConvertEventArgs(Object, Type)

初始化 ConvertEventArgs 類別的新執行個體。

屬性

DesiredType

取得您要的值的資料類型。

Value

取得或設定 ConvertEventArgs 的值。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱