ConvertEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public ref class ConvertEventArgs : EventArgs
public class ConvertEventArgs : EventArgs
type ConvertEventArgs = class
inherit EventArgs
Public Class ConvertEventArgs
Inherits EventArgs
- 继承
- 派生
示例
下面的代码示例创建 一个 Binding,将委托ConvertEventHandler添加到 Parse 和 Format 事件,并使用 DataBindings 属性将 BindingsCollection 添加到Binding控件的 TextBox 。
DecimalToCurrencyString
添加到 Format 事件的事件委托使用 ToString 方法将绑定值格式化 (Decimal类型) 为货币。
CurrencyStringToDecimal
添加到 事件的事件Parse委托将控件显示的值转换回 类型Decimal。
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
注解
ConvertEventArgs用于格式化和取消格式化由通过 Binding 对象绑定到数据的Windows 窗体控件显示的值。 Format每当控件属性绑定到值时,将发生事件,Parse每当绑定值更改时,该事件就发生。
Format和 Parse 事件允许你创建自定义格式来显示数据。 例如,如果表中的数据的类型为 Decimal,则可以通过在 事件中Format将 的 ConvertEventArgs 属性设置为Value格式化值,指定应以本地货币格式显示数据。 因此,必须在 事件中 Parse 取消显示的值格式。
有关处理事件的详细信息,请参阅 处理和引发事件。
构造函数
ConvertEventArgs(Object, Type) |
初始化 ConvertEventArgs 类的新实例。 |
属性
DesiredType |
获取所需值的数据类型。 |
Value |
获取或设置 ConvertEventArgs 的值。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |