Condividi tramite


Binding.Format Evento

Definizione

Si verifica quando la proprietà di un controllo è associata a un valore di dati.

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

Tipo evento

Esempio

Nell'esempio di codice seguente viene creato un Bindingoggetto , viene aggiunto un ConvertEventHandler delegato a entrambi gli Parse eventi e Format e viene aggiunto all'oggetto BindingBindingsCollection di un TextBox controllo tramite la DataBindings proprietà . Il DecimalToCurrencyString delegato dell'evento, aggiunto all'evento Format , formatta il valore associato (un Decimal tipo) come valuta usando il ToString metodo . Il CurrencyStringToDecimal delegato dell'evento, aggiunto all'evento Parse , converte di nuovo il valore visualizzato dal controllo nel Decimal tipo .

In questo esempio si presuppone la presenza di un oggetto DataSet denominato ds.

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 a Decimal type.
      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 a Decimal type.
   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

   ' Converts the string back to decimal using the static ToDecimal method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString, _
   NumberStyles.Currency, nothing)
End Sub

Private Sub BindControl
   ' Creates the binding first. The OrderAmount is a Decimal type.
   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

L'evento Format viene generato quando i dati vengono inseriti dall'origine dati nel controllo . È possibile gestire l'evento Format per convertire i dati non formattati dall'origine dati in dati formattati per la visualizzazione. Quando i dati vengono estratti dal controllo nell'origine dati, l'evento Parse viene generato per annullare la formattazione del valore visualizzato, quindi l'evento Format si verifica per riformattare i dati per la visualizzazione. In questo modo, il controllo associato visualizza dati formattati correttamente indipendentemente dal fatto che l'utente immetta dati formattati o non formattati nel controllo.

Gli Format eventi e Parse consentono di creare formati personalizzati per la visualizzazione dei dati. Ad esempio, se i dati di una tabella sono di tipo Decimal, è possibile visualizzare i dati nel formato valuta locale impostando la Value proprietà di ConvertEventArgs sul valore formattato nell'evento Format . Di conseguenza, è necessario annullare la formattazione del valore visualizzato nell'evento Parse .

L'evento Format si verifica ogni volta che il Current valore delle BindingManagerBase modifiche, che include:

  • La prima volta che la proprietà è associata.

  • Ogni volta che cambia Position .

  • Ogni volta che l'elenco associato a dati viene ordinato o filtrato, che viene eseguito quando un DataView oggetto fornisce l'elenco.

L'evento Format si verifica anche dopo l'evento Parse . Ad esempio, quando un controllo perde lo stato attivo, il relativo contenuto viene analizzato. Immediatamente dopo, man mano che vengono inseriti nuovi dati nel controllo, l'evento Format si verifica consentendo la formattazione del nuovo contenuto.

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.

Si applica a

Vedi anche