Condividi tramite


ConvertEventArgs.Value Proprietà

Definizione

Ottiene o imposta il valore di ConvertEventArgs.

public:
 property System::Object ^ Value { System::Object ^ get(); void set(System::Object ^ value); };
public object Value { get; set; }
public object? Value { get; set; }
member this.Value : obj with get, set
Public Property Value As Object

Valore della proprietà

Valore di ConvertEventArgs.

Esempio

Nell'esempio di codice seguente viene creato un Bindingoggetto , viene aggiunto un ConvertEventHandler delegato agli Parse eventi e Format e viene utilizzata la DataBindings proprietà per aggiungere l'oggetto BindingBindingsCollection a di un TextBox controllo . Il DecimalToCurrencyString delegato dell'evento, che viene aggiunto all'evento Format , usa il ToString metodo per formattare il valore associato (un Decimal tipo) come valuta. Il CurrencyStringToDecimal delegato dell'evento, che viene aggiunto all'evento Parse , converte nuovamente il valore visualizzato dal controllo nel Decimal tipo .

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

Commenti

Il valore contenuto dalla Value proprietà dipende dall'evento in cui viene restituito l'oggetto ConvertEventArgs . Può ConvertEventArgs essere restituito nell'evento Format o nell'evento Parse .

Quando viene ConvertEventArgs restituito nell'evento Format , la Value proprietà contiene il valore della proprietà non formattata dell'origine dati. All'interno dell'evento Format è possibile leggere il valore della proprietà, formattare il valore e reimpostare la Value proprietà sul nuovo valore (formattato), impostando così il valore visualizzato nel controllo associato a dati.

Quando viene ConvertEventArgs restituito nell'evento Parse , la proprietà contiene il valore formattato personalizzato del controllo associato a dati. All'interno dell'evento Parse , è necessario leggere il valore formattato, analizzarlo e convertirlo nuovamente nello stesso tipo di dati dell'origine dati. È quindi possibile reimpostare la Value proprietà sul valore non formattato e quindi impostare il valore dell'origine dati. Per determinare il tipo dell'origine dati, esaminare il valore della DesiredType proprietà.

Si applica a

Vedi anche