Binding.Parse 事件

定義

發生於資料繫結控制項的值變更時。

public:
 event System::Windows::Forms::ConvertEventHandler ^ Parse;
public event System.Windows.Forms.ConvertEventHandler Parse;
public event System.Windows.Forms.ConvertEventHandler? Parse;
member this.Parse : System.Windows.Forms.ConvertEventHandler 
Public Custom Event Parse As ConvertEventHandler 

事件類型

範例

下列程式碼範例會 Binding 建立 、將委派新增 ConvertEventHandlerParseFormat 事件,並透過 屬性將 加入 Binding 控制項 BindingsCollectionTextBoxDataBindings 。 事件 DecimalToCurrencyString 委派已新增至 Format 事件,使用 方法將系結值格式化 (Decimal 類型) 為貨幣 ToString 。 新增 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" );
      // Add the delegates to the event.
      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");
   // Add the delegates to the event.
   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 cevent.DesiredType IsNot GetType(String) Then
        Exit Sub
    End If
    
    ' Use the ToString method to format the value as currency ("c").
    cevent.Value = CType(cevent.Value, decimal).ToString("c")
End Sub

Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
    ' The method converts back to decimal type only.
    If cevent.DesiredType IsNot GetType(Decimal) Then
        Exit Sub
    End If

    ' Convert 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
    ' Create the binding first. The OrderAmount is typed as Decimal.
    Dim b As Binding = New Binding _
        ("Text", ds, "Customers.custToOrders.OrderAmount")
    ' Add the delegates to the event.
    AddHandler b.Format, AddressOf DecimalToCurrencyString
    AddHandler b.Parse, AddressOf CurrencyStringToDecimal
    text1.DataBindings.Add(b)
End Sub

備註

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

Parse 事件會在下列情況下發生:

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

適用於

另請參閱