Binding.Parse 事件

定义

在数据绑定控件的值更改时发生。

C#
public event System.Windows.Forms.ConvertEventHandler Parse;
C#
public event System.Windows.Forms.ConvertEventHandler? Parse;

事件类型

示例

下面的代码示例创建 ,Binding将 委托添加到 ConvertEventHandlerParseFormat 事件,并通过 DataBindings 属性将 添加到BindingBindingsCollection控件的 TextBoxDecimalToCurrencyString添加到 事件的事件Format委托使用 ToString 方法将绑定值 (Decimal类型) 格式化为货币。 CurrencyStringToDecimal添加到 事件的事件Parse委托将控件显示的值转换回 Decimal 类型。

C#
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);
}

注解

FormatParse 事件允许你创建自定义格式来显示数据。 例如,如果表中的数据的类型为 Decimal,则可以通过在 事件中将 的 ConvertEventArgs 属性设置为Value格式化值Format,以本地货币格式显示数据。 因此,必须在 事件中 Parse 取消显示的值格式。

事件发生 Parse 在以下情况下:

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅