Binding.Format Zdarzenie

Definicja

Występuje, gdy właściwość kontrolki jest powiązana z wartością danych.

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 

Typ zdarzenia

Przykłady

Poniższy przykład kodu tworzy Bindingelement , dodaje delegata ConvertEventHandler zarówno do Parse zdarzeń, jak i Format i dodaje Binding do BindingsCollectionTextBox kontrolki za pośrednictwem DataBindings właściwości . Delegat DecimalToCurrencyString zdarzenia, dodany do Format zdarzenia, formatuje powiązaną wartość ( Decimal typ) jako walutę przy użyciu ToString metody . CurrencyStringToDecimal Delegat zdarzenia dodany do Parse zdarzenia konwertuje wartość wyświetlaną przez kontrolkę Decimal z powrotem na typ.

W tym przykładzie założono obecność nazwanego DataSetdsobiektu .

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

Uwagi

Zdarzenie Format jest wywoływane, gdy dane są wypychane ze źródła danych do kontrolki. Zdarzenie można obsługiwać Format w celu konwertowania niesformatowanych danych ze źródła danych na sformatowane dane do wyświetlania. Gdy dane są pobierane ze kontrolki do źródła danych, Parse zdarzenie jest wywoływane w celu niesformatowania wyświetlanej wartości, a następnie Format zdarzenie ma miejsce w celu sformatowania danych do wyświetlenia. Dzięki temu powiązana kontrolka wyświetla poprawnie sformatowane dane niezależnie od tego, czy użytkownik wprowadza sformatowane lub niesformatowane dane w kontrolce.

Zdarzenia Format i Parse umożliwiają tworzenie niestandardowych formatów wyświetlania danych. Jeśli na przykład dane w tabeli są typu Decimal, można wyświetlić dane w formacie waluty lokalnej, ustawiając Value właściwość ConvertEventArgs na wartość sformatowaną w zdarzeniu Format . W związku z tym należy usunąć formatowanie wyświetlanej wartości w zdarzeniu Parse .

Zdarzenie Format odbywa się za każdym razem, gdy Current wartość BindingManagerBase zmian obejmuje:

  • Przy pierwszym powiązaniu właściwości.

  • Za każdym razem, gdy zmiany są Position zmieniane.

  • Za każdym razem, gdy lista powiązana z danymi zostanie posortowana lub przefiltrowana, co jest wykonywane, gdy DataView lista jest dostarczana.

Zdarzenie Format występuje również po zdarzeniu Parse . Na przykład gdy kontrolka traci fokus, jego zawartość jest analizowana. Natychmiast potem, gdy nowe dane są wypychane do kontrolki, Format zdarzenie ma na celu sformatowanie nowej zawartości.

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też