ConvertEventArgs Třída

Definice

Poskytuje data pro Format události a Parse .

public ref class ConvertEventArgs : EventArgs
public class ConvertEventArgs : EventArgs
type ConvertEventArgs = class
    inherit EventArgs
Public Class ConvertEventArgs
Inherits EventArgs
Dědičnost
ConvertEventArgs
Odvozené

Příklady

Následující příklad kódu vytvoří Binding, přidá ConvertEventHandler delegáta do Parse událostí a Format a používá DataBindings vlastnost k přidání Binding do BindingsCollectionTextBox ovládacího prvku . Delegát DecimalToCurrencyString události, který je přidán do Format události, používá metodu ToString k formátování vázané hodnoty ( Decimal typu) jako měny. Delegát CurrencyStringToDecimal události, který je přidán do Parse události, převede hodnotu zobrazenou ovládacím prvku zpět na Decimal typ.

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" );
      
      // Adds the delegates to the events.
      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");
   // Adds the delegates to the events.
   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 Not cevent.DesiredType Is GetType(String) Then
      Return
   End If 
   ' Use the ToString method to format the value as currency ("c").
   cevent.Value = CDec(cevent.Value).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
      Return
   End If 
   ' Converts 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()
   ' Creates the binding first. The OrderAmount is typed as Decimal.
   Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
   ' Adds the delegates to the events.
   AddHandler b.Format, AddressOf DecimalToCurrencyString
   AddHandler b.Parse, AddressOf CurrencyStringToDecimal
   text1.DataBindings.Add(b)
End Sub

Poznámky

Slouží ConvertEventArgs k formátování a zrušení formátování hodnot zobrazených ovládacím prvek model Windows Forms, který je svázán s daty prostřednictvím objektuBinding. Událost Format nastane vždy, když je vlastnost ovládacího prvku vázána na hodnotu a Parse událost nastane při každé změně vázané hodnoty.

Události Format a Parse umožňují vytvářet vlastní formáty pro zobrazení dat. Pokud jsou například data v tabulce typu Decimal, můžete určit, že se mají zobrazit ve formátu místní měny – nastavením Value vlastnosti ConvertEventArgs na formátovanou hodnotu v Format události. V důsledku toho je nutné zrušit formátování zobrazené hodnoty v Parse události.

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

Konstruktory

ConvertEventArgs(Object, Type)

Inicializuje novou instanci ConvertEventArgs třídy .

Vlastnosti

DesiredType

Získá datový typ požadované hodnoty.

Value

Získá nebo nastaví hodnotu objektu ConvertEventArgs.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Type Získá z aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také