ConvertEventArgs.Value Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o valor do 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
Valor de Propriedade
O valor do ConvertEventArgs.
Exemplos
O exemplo de código seguinte cria um Binding, adiciona um ConvertEventHandler delegado tanto aos Parse eventos e Format e usa a DataBindings propriedade para adicionar o Binding ao BindingsCollection de um TextBox controlo. O DecimalToCurrencyString delegado do evento, que é adicionado ao Format evento, usa o ToString método para formatar o valor vinculado (um Decimal tipo) como moeda. O CurrencyStringToDecimal delegado do evento, que é adicionado ao Parse evento, converte o valor apresentado pelo controlo de volta para o 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
Observações
O valor contido pela Value propriedade depende do evento em que o ConvertEventArgs é devolvido. Podem ConvertEventArgs ser devolvidos tanto no Format evento como no Parse evento.
Quando o ConvertEventArgs é devolvido no Format evento, a Value propriedade contém o valor da propriedade não formatada da fonte de dados. Dentro do Format evento, pode ler o valor da propriedade, formatá-lo e redefinir a Value propriedade para o novo valor (formatado), definindo assim o valor exibido no controlo data-bound.
Quando o ConvertEventArgs é devolvido no Parse evento, a propriedade contém o valor formatado personalizado do controlo vinculado aos dados. Dentro do Parse evento, deve ler o valor formatado, analisá-lo e convertê-lo de volta para o mesmo tipo de dado da fonte de dados. Pode então redefinir a Value propriedade para o valor não formatado e, assim, definir o valor da fonte de dados. Para determinar o tipo da fonte de dados, examine o valor da DesiredType propriedade.