ConvertEventArgs.Value ConvertEventArgs.Value ConvertEventArgs.Value ConvertEventArgs.Value Property

定义

获取或设置 ConvertEventArgs 的值。Gets or sets the value of the ConvertEventArgs.

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

属性值

示例

下面的代码示例将创建Binding,添加ConvertEventHandler委托ParseFormat事件,并使用DataBindings属性将添加BindingBindingsCollectionTextBox控件。The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and uses the DataBindings property to add the Binding to the BindingsCollection of a TextBox control. DecimalToCurrencyString添加到的事件委托Format事件,使用ToString要绑定的值的格式设置方法 (Decimal类型) 为货币。The DecimalToCurrencyString event delegate, which is added to the Format event, uses the ToString method to format the bound value (a Decimal type) as currency. CurrencyStringToDecimal添加到的事件委托Parse事件,将由控件显示的值转换回Decimal类型。The CurrencyStringToDecimal event delegate, which is added to the Parse event, converts the value displayed by the control back to the 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" );
      
      // 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 'DecimalToCurrencyString
 
 
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 'CurrencyStringToDecimal
 
 
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 'BindControl

注解

所包含的值Value属性依赖于在其中事件ConvertEventArgs返回。The value contained by the Value property depends on the event in which the ConvertEventArgs is returned. ConvertEventArgs可以在返回Format事件或Parse事件。The ConvertEventArgs can be returned in either the Format event or the Parse event.

ConvertEventArgs中返回Format事件,Value属性包含的数据源未格式化的属性值。When the ConvertEventArgs is returned in the Format event, the Value property contains the unformatted property value of the data source. Format事件,可以读取属性值、 值的格式设置和重置Value属性设置为新的 (格式化) 值,从而将数据绑定控件中显示的值。Within the Format event, you can read the property value, format the value, and reset the Value property to the new (formatted) value, thereby setting the value displayed in the data-bound control.

ConvertEventArgs中返回Parse事件,该属性包含的数据绑定控件的自定义格式值。When the ConvertEventArgs is returned in the Parse event, the property contains the custom-formatted value of the data-bound control. Parse事件,您必须读取带格式的值,分析它,并将其转换回数据源的数据类型相同。Within the Parse event, you must read the formatted value, parse it, and convert it back to the same data type as the data source. 然后可以重置Value到未格式化的属性值,并因此设置数据源的值。You can then reset the Value property to the unformatted value, and thereby set the value of the data source. 若要确定数据源的类型,请检查DesiredType属性值。To determine the type of the data source, examine the DesiredType property value.

适用于

另请参阅