ConvertEventArgs Osztály

Definíció

Adatokat biztosít a Format és Parse eseményekhez.

public ref class ConvertEventArgs : EventArgs
public class ConvertEventArgs : EventArgs
type ConvertEventArgs = class
    inherit EventArgs
Public Class ConvertEventArgs
Inherits EventArgs
Öröklődés
ConvertEventArgs
Származtatott

Példák

Az alábbi példakód létrehoz egyBinding, a ConvertEventHandler delegáltat az eseményekhez és Parse az Format eseményekhez is, és a DataBindings tulajdonság használatával adja hozzá a BindingBindingsCollectionTextBox vezérlőhöz. Az DecimalToCurrencyString eseményhez Format hozzáadott eseménydelegált a ToString metódussal pénznemként formázza a kötött értéket (típust Decimal ). Az CurrencyStringToDecimal eseményhez Parse hozzáadott eseménymegbízott a vezérlőelem által megjelenített értéket visszaalakítja a Decimal típusra.

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

Megjegyzések

A ConvertEventArgs egy Windows Forms vezérlőelem által megjelenített értékek formázására és formázására szolgál, amelyek egy Binding objektumon keresztül kötődnek az adatokhoz. Az Format esemény akkor fordul elő, ha egy vezérlőtulajdonság egy értékhez van kötve, és az Parse esemény akkor következik be, amikor a kötött érték megváltozik.

Az Format események lehetővé Parse teszik egyéni formátumok létrehozását az adatok megjelenítéséhez. Ha például egy táblában lévő adatok típusa Decimalvan, megadhatja, hogy az adatok a helyi pénznemformátumban jelenjenek meg– a tulajdonságot az esemény formázott értékére ValueConvertEventArgs állíthatjaFormat. Ennek következtében meg kell szüntetnie a megjelenített érték formázását az Parse eseményben.

Az események kezelésével kapcsolatos további információkért lásd : Események kezelése és emelése.

Konstruktorok

Name Description
ConvertEventArgs(Object, Type)

Inicializálja a ConvertEventArgs osztály új példányát.

Tulajdonságok

Name Description
DesiredType

Lekéri a kívánt érték adattípusát.

Value

Lekéri vagy beállítja a ConvertEventArgs.

Metódusok

Name Description
Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

A következőre érvényes:

Lásd még