Binding.Parse Evento

Definizione

Si verifica quando viene modificato il valore di un controllo associato a dati.Occurs when the value of a data-bound control changes.

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 

Tipo evento

ConvertEventHandler

Esempio

Nell'esempio di codice seguente viene creato un oggetto Binding , viene aggiunto un ConvertEventHandler delegato a entrambi gli Parse Format eventi e e viene aggiunto l'oggetto Binding all'oggetto BindingsCollection di un TextBox controllo tramite la DataBindings Proprietà.The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and adds the Binding to the BindingsCollection of a TextBox control through the DataBindings property. Il DecimalToCurrencyString delegato dell'evento, aggiunto all' Format evento, formatta il valore associato (un Decimal tipo) come valuta usando il ToString metodo.The DecimalToCurrencyString event delegate, added to the Format event, formats the bound value (a Decimal type) as currency using the ToString method. Il CurrencyStringToDecimal delegato dell'evento, aggiunto all' Parse evento, converte il valore visualizzato dal controllo di nuovo nel Decimal tipo.The CurrencyStringToDecimal event delegate, added to the Parse event, converts the value displayed by the control back to the Decimal type.

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

Commenti

Gli Format Parse eventi e consentono di creare formati personalizzati per la visualizzazione dei dati.The Format and Parse events allow you to create custom formats for displaying data. Se, ad esempio, i dati di una tabella sono di tipo Decimal , è possibile visualizzare i dati nel formato di valuta locale impostando la Value proprietà dell'oggetto sul ConvertEventArgs valore formattato nell' Format evento.For example, if the data in a table is of type Decimal, you can display the data in the local currency format by setting the Value property of the ConvertEventArgs to the formatted value in the Format event. È necessario quindi deformattare il valore visualizzato nell' Parse evento.You must consequently unformat the displayed value in the Parse event.

L' Parse evento si verifica nelle condizioni seguenti:The Parse event occurs under the following conditions:

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.For more information about handling events, see Handling and Raising Events.

Si applica a

Vedi anche