Binding.Parse Événement
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Se produit lorsque la valeur d’un contrôle lié aux données change.
public:
event System::Windows::Forms::ConvertEventHandler ^ Parse;
public event System.Windows.Forms.ConvertEventHandler Parse;
public event System.Windows.Forms.ConvertEventHandler? Parse;
member this.Parse : System.Windows.Forms.ConvertEventHandler
Public Custom Event Parse As ConvertEventHandler
Type d'événement
Exemples
L’exemple de code suivant crée un BindingConvertEventHandler délégué à la fois aux Parse événements et Format aux événements, puis ajoute le Binding contrôle BindingsCollection par le biais de TextBox la DataBindings propriété. Le DecimalToCurrencyString délégué d’événement, ajouté à l’événement Format , met en forme la valeur liée (un Decimal type) comme devise à l’aide de la ToString méthode. Le CurrencyStringToDecimal délégué d’événement, ajouté à l’événement Parse , convertit la valeur affichée par le contrôle en Decimal type.
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" );
// 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 typed as Decimal.
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
' Convert 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
' Create the binding first. The OrderAmount is typed as Decimal.
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
Remarques
Les Format événements et Parse les événements vous permettent de créer des formats personnalisés pour l’affichage des données. Par exemple, si les données d’une table sont de type Decimal, vous pouvez afficher les données au format monétaire local en définissant la Value propriété de ConvertEventArgs la valeur mise en forme dans l’événement Format . Vous devez donc annuler la mise en forme de la valeur affichée dans l’événement Parse .
L’événement Parse se produit dans les conditions suivantes :
Lorsque la EndCurrentEdit méthode de l’objet BindingManagerBase est appelée.
Lorsque les CurrentBindingManagerBase modifications sont effectuées (en d’autres termes, lorsque les Position modifications sont apportées).
Pour plus d'informations sur la gestion des événements, voir gestion et déclenchement d’événements.