Freigeben über


Binding.Parse-Ereignis

Tritt ein, wenn der Wert eines datengebundenen Steuerelements geändert wird.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event Parse As ConvertEventHandler
'Usage
Dim instance As Binding
Dim handler As ConvertEventHandler

AddHandler instance.Parse, handler
public event ConvertEventHandler Parse
public:
event ConvertEventHandler^ Parse {
    void add (ConvertEventHandler^ value);
    void remove (ConvertEventHandler^ value);
}
/** @event */
public void add_Parse (ConvertEventHandler value)

/** @event */
public void remove_Parse (ConvertEventHandler value)
JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Das Format-Ereignis und das Parse-Ereignis ermöglichen das Erstellen benutzerdefinierter Formate für die Anzeige von Daten. Wenn die Daten in einer Tabelle z. B. vom Typ Decimal sind, können Sie diese im lokalen Währungsformat anzeigen, indem Sie die Value-Eigenschaft des ConvertEventArgs im Format-Ereignis auf den formatierten Wert festlegen. Sie müssen die Formatierung des angezeigten Werts folglich im Parse-Ereignis aufheben.

Das Parse-Ereignis tritt unter folgenden Bedingungen ein:

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird ein Binding erstellt. Außerdem wird dem Parse-Ereignis und dem Format-Ereignis ein ConvertEventHandler-Delegat hinzugefügt, und das Binding wird über die DataBindings-Eigenschaft der BindingsCollection eines TextBox-Steuerelements hinzugefügt. Der DecimalToCurrencyString-Ereignisdelegat formatiert, wenn er dem Format-Ereignis hinzugefügt wird, mithilfe der ToString-Methode den gebundenen Wert (ein Decimal-Typ) als Währung. Der CurrencyStringToDecimal-Ereignisdelegat konvertiert, wenn er dem Parse-Ereignis hinzugefügt wird, den vom Steuerelement angezeigten Wert zurück in den Decimal-Typ.

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
        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 Not cevent.DesiredType Is 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
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:
   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.get_DesiredType().Equals(String.class.ToType())) {
        return;
    }
    // Use the ToString method to format the value as currency ("c").
    cevent.set_Value(((System.Decimal)(cevent.get_Value())).ToString("c"));
} //DecimalToCurrencyString

private void CurrencyStringToDecimal(Object sender, ConvertEventArgs cevent)
{
    // The method converts back to decimal type only.
    if (!cevent.get_DesiredType().Equals(System.Decimal.class.ToType())) {
        return;
    }
    // Converts the string back to decimal using the static Parse method.
    cevent.set_Value(Decimal.Parse(cevent.get_Value().ToString(), 
        NumberStyles.Currency, null));
} //CurrencyStringToDecimal

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.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    b.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl
private function DecimalToCurrencyString(sender, cevent : ConvertEventArgs)
{
   // The method converts only to string type. Test this using the DesiredType.
   if(cevent.DesiredType != String.GetType()) return;

   cevent.Value = (Decimal(cevent.Value)).ToString("c"); 
}

private function CurrencyStringToDecimal(sender, cevent : ConvertEventArgs)
{
   // The method converts back to decimal type only. 
   if(cevent.DesiredType != Decimal.GetType()) return;

   // Converts the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

private function BindControl()
{
   // Creates the binding first. The OrderAmount is a Decimal type.
   var b : Binding = new Binding
      ("Text", ds, "Suppliers.CompanyName");
   // Add the delegates to the event.
   b.add_Format(DecimalToCurrencyString);
   b.add_Parse(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Binding-Klasse
Binding-Member
System.Windows.Forms-Namespace
Binding.Format-Ereignis
OnParse
PositionChanged
CurrentChanged