Sdílet prostřednictvím


Binding.Format Událost

Definice

Nastane, když je vlastnost ovládacího prvku svázána s datovou hodnotou.

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 

Event Type

Příklady

Následující příklad kódu vytvoří Binding, přidá ConvertEventHandler delegáta do obou Parse i Format událostí a přidá Binding do TextBoxBindingsCollection ovládacího prvku prostřednictvím DataBindings vlastnosti. Delegát DecimalToCurrencyString události přidaný do Format události formátuje vázanou hodnotu ( Decimal typ) jako měnu pomocí ToString metody. Delegát CurrencyStringToDecimal události přidaný do Parse události převede hodnotu zobrazenou ovládacím prvku zpět na Decimal typ.

Tento příklad předpokládá přítomnost pojmenovaného DataSetds.

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

Poznámky

Událost Format se vyvolá, když se data nasdílí ze zdroje dat do ovládacího prvku. Událost můžete zpracovat Format a převést neformátovaná data ze zdroje dat na formátovaná data pro zobrazení. Při přetáhnutí dat z ovládacího prvku do zdroje dat je událost vyvolána tak, Parse aby nepřeformulovala zobrazenou hodnotu, pak Format dojde k přeformátování dat pro zobrazení. Tím se zajistí, že se vázaný ovládací prvek zobrazí správně naformátovaná data bez ohledu na to, jestli uživatel zadá do ovládacího prvku formátovaná nebo neformátovaná data.

Parse Události Format umožňují vytvářet vlastní formáty pro zobrazení dat. Pokud jsou například data v tabulce typu Decimal, můžete data zobrazit ve formátu místní měny nastavením Value vlastnosti ConvertEventArgs na formátovanou hodnotu v Format události. V události je proto nutné neformátovat zobrazenou hodnotu Parse .

K Format události dochází vždy, když Current se změní hodnota BindingManagerBase , která zahrnuje:

  • Při prvním vázání vlastnosti.

  • Kdykoli se Position změny změní.

  • Pokaždé, když je seznam svázaný s daty seřazený nebo filtrovaný, který se provede, když DataView seznam zadáte.

Událost Format také nastane po Parse události. Když například ovládací prvek ztratí fokus, jeho obsah se parsuje. Okamžitě po vložení nových dat do ovládacího prvku dojde k události, Format která umožňuje formátování nového obsahu.

Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také