BindingsCollection 类
代表某控件的 Binding 对象的集合。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Class BindingsCollection
Inherits BaseCollection
用法
Dim instance As BindingsCollection
public class BindingsCollection : BaseCollection
public ref class BindingsCollection : public BaseCollection
public class BindingsCollection extends BaseCollection
public class BindingsCollection extends BaseCollection
备注
简单数据绑定可通过向 BindingsCollection 添加 Binding 对象来实现。从 Control 类继承的所有对象均可通过 DataBindings 属性访问 BindingsCollection。有关支持数据绑定的 Windows 控件的列表,请参见 Binding 类。
示例
下面的示例将某个 TextBox 控件的 Text 属性绑定到数据库中的一个字段。
Private Sub BindTextBoxControl()
Dim myDataSet As New DataSet()
' Insert code to populate the DataSet with tables, columns, and data.
' Creates a new Binding object.
Dim myBinding As New Binding("Text", myDataSet, _
"customers.custToOrders.OrderAmount")
' Adds event delegates for the Parse and Format events.
AddHandler myBinding.Parse, AddressOf CurrencyToDecimal
AddHandler myBinding.Format, AddressOf DecimalToCurrency
' Adds the new Binding to the BindingsCollection.
text1.DataBindings.Add(myBinding)
End Sub
Private Sub DecimalToCurrency(sender As Object, _
cevent As ConvertEventArgs)
' This method is the Format event handler. Whenever the
' control displays a new value, the value is converted from
' its native Decimal type to a string. The ToString method
' then formats the value as a Currency, by using the
' formatting character "c".
cevent.Value = CDec(cevent.Value).ToString("c")
End Sub
Private Sub CurrencyToDecimal(sender As Object, _
cevent As ConvertEventArgs)
' This method is the Parse event handler. The Parse event
' occurs whenever the displayed value changes. The static
' Parse method of the Decimal structure converts the
' string back to its native Decimal type.
cevent.Value = Decimal.Parse(cevent.Value.ToString(), _
NumberStyles.Currency, nothing)
End Sub
private void BindTextBoxControl()
{
DataSet myDataSet = new DataSet();
/* Insert code to populate the DataSet with tables,
columns, and data. */
// Creates a new Binding object.
Binding myBinding = new Binding
("Text", myDataSet, "customers.custToOrders.OrderAmount");
// Adds event delegates for the Parse and Format events.
myBinding.Parse += new ConvertEventHandler(CurrencyToDecimal);
myBinding.Format += new ConvertEventHandler(DecimalToCurrency);
// Adds the new Binding to the BindingsCollection.
text1.DataBindings.Add(myBinding);
}
private void DecimalToCurrency(object sender,
ConvertEventArgs cevent)
{
/* This method is the Format event handler. Whenever the
control displays a new value, the value is converted from
its native Decimal type to a string. The ToString method
then formats the value as a Currency, by using the
formatting character "c". */
cevent.Value = ((decimal) cevent.Value).ToString("c");
}
private void CurrencyToDecimal(object sender,
ConvertEventArgs cevent)
{
/* This method is the Parse event handler. The Parse event
occurs whenever the displayed value changes. The static
Parse method of the Decimal structure converts the
string back to its native Decimal type. */
cevent.Value = Decimal.Parse(cevent.Value.ToString(),
NumberStyles.Currency, null);
}
private:
void BindTextBoxControl()
{
DataSet^ myDataSet = gcnew DataSet;
/* Insert code to populate the DataSet with tables,
columns, and data. */
// Creates a new Binding object.
Binding^ myBinding = gcnew Binding(
"Text",myDataSet,"customers.custToOrders.OrderAmount" );
// Adds event delegates for the Parse and Format events.
myBinding->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyToDecimal );
myBinding->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrency );
// Adds the new Binding to the BindingsCollection.
text1->DataBindings->Add( myBinding );
}
void DecimalToCurrency( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
/* This method is the Format event handler. Whenever the
control displays a new value, the value is converted from
its native Decimal type to a string. The ToString method
then formats the value as a Currency, by using the
formatting character "c". */
cevent->Value = safe_cast<Decimal ^>(cevent->Value)->ToString( "c" );
}
void CurrencyToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
/* This method is the Parse event handler. The Parse event
occurs whenever the displayed value changes. The static
Parse method of the Decimal structure converts the
string back to its native Decimal type. */
cevent->Value = Decimal::Parse( cevent->Value->ToString(),
NumberStyles::Currency, nullptr );
}
private void BindTextBoxControl()
{
DataSet myDataSet = new DataSet();
/* Insert code to populate the DataSet with tables,
columns, and data.
*/
// Creates a new Binding object.
Binding myBinding = new Binding("Text", myDataSet,
"customers.custToOrders.OrderAmount");
// Adds event delegates for the Parse and Format events.
myBinding.add_Parse(new ConvertEventHandler(CurrencyToDecimal));
myBinding.add_Format(new ConvertEventHandler(DecimalToCurrency));
// Adds the new Binding to the BindingsCollection.
text1.get_DataBindings().Add(myBinding);
} //BindTextBoxControl
private void DecimalToCurrency(Object sender, ConvertEventArgs cevent)
{
/* This method is the Format event handler. Whenever the
control displays a new value, the value is converted from
its native Decimal type to a string. The ToString method
then formats the value as a Currency, by using the
formatting character "c".
*/
cevent.set_Value(((System.Decimal)(cevent.get_Value())).ToString("c"));
} //DecimalToCurrency
private void CurrencyToDecimal(Object sender, ConvertEventArgs cevent)
{
/* This method is the Parse event handler. The Parse event
occurs whenever the displayed value changes. The static
Parse method of the Decimal structure converts the
string back to its native Decimal type.
*/
cevent.set_Value(Decimal.Parse(cevent.get_Value().ToString(),
NumberStyles.Currency, null));
} //CurrencyToDecimal
继承层次结构
System.Object
System.MarshalByRefObject
System.Windows.Forms.BaseCollection
System.Windows.Forms.BindingsCollection
System.Windows.Forms.ControlBindingsCollection
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
BindingsCollection 成员
System.Windows.Forms 命名空间
CurrencyManager