Control.Controls 属性参考

本文介绍如何使用Control.Controls属性来获取并操作控件集合。通过示例展示了如何在Visual Basic、C#和C++中检查控件是否存在,并将其从集合中移除。
Control.Controls 属性[C#]
获取包含在控件内的控件的集合。

[Visual Basic]
Public ReadOnly Property Controls As Control.ControlCollection

[C#]
public Control.ControlCollection Controls {get;}

[C++]
public: __property Control.ControlCollection* get_Controls();

[JScript]
public function get Controls() : Control.ControlCollection;

属性值
一个 Control.ControlCollection,它表示控件内包含的控件的集合。

备注
Control 可以充当控件集合的父级。例如,当将多个控件添加到 Form 时,每一个控件都是 Control.ControlCollection
对象的成员,该对象被分配给窗体的 Controls 属性,窗体从 Control 类派生而来。 可以使用 Control.ControlCollection 类的方法,在分配给 Controls 属性的 Control.ControlCollection
对象中操作控件。 将多个控件添加到父控件时,建议在初始化要添加的控件之前调用 SuspendLayout 方法。将控件添加到父控件之后,
调用 ResumeLayout 方法。这样就可以提高带有许多控件的应用程序的性能。 示例 [Visual Basic, C#, C++] 下面的示例从派生类 Panel 的 Control.ControlCollection 中移除一个
Control(如果它是该集合的成员)。该示例假定您已在 Form 上创建了一个 Panel、一个 Button 以及至少一个
RadioButton 控件。将 RadioButton 控件添加到 Panel 控件,而将 Panel 控件添加到 Form。单击该按钮
时,从 Control.ControlCollection 中移除名为 radioButton2 的单选按钮。 [Visual Basic] ' Remove the RadioButton control if it exists. Private Sub RemoveButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles RemoveButton.Click If Panel1.Controls.Contains(RadioAddRangeButton) Then Panel1.Controls.Remove(RadioAddRangeButton) End If End Sub [C#] // Remove the RadioButton control if it exists. private void removeButton_Click(object sender, System.EventArgs e) { if(panel1.Controls.Contains(removeButton)) { panel1.Controls.Remove(removeButton); } } [C++] // Remove the RadioButton control if it exists. private: void removeButton_Click(Object* sender, System::EventArgs* e) { if (panel1->Controls->Contains(removeButton)) { panel1->Controls->Remove(removeButton); } }
在 WinForm 中,`Control.DataBinding.Add()` 方法用于将控件的属性绑定到数据源的属性,实现数据的同步显示和更新。以下结合参考内容详细介绍其使用方法: ### 基本使用示例 参考内容中给出了一个扩展方法 `BindText`,它封装了 `Control.DataBinding.Add()` 方法,用于将控件的 `Text` 属性绑定到数据源的指定属性: ```csharp public static class ControlEx { public static void BindText(this Control control, object dataSource, string dataMember) { control.DataBindings.Add("Text", dataSource, dataMember, false, DataSourceUpdateMode.OnPropertyChanged); } } ``` 此扩展方法可在调用时少写代码,使用方式如下: ```csharp // 假设存在一个 TextBox 控件 textBox1 和一个数据源对象 dataSource,以及数据源的属性名 "PropertyName" textBox1.BindText(dataSource, "PropertyName"); ``` 在上述代码中,`control.DataBindings.Add()` 方法的参数含义如下: - `"Text"`:表示要绑定的控件属性,这里是 `Text` 属性。 - `dataSource`:数据源对象,即数据的来源。 - `dataMember`:数据源对象的属性名,控件的 `Text` 属性将与该属性绑定。 - `false`:表示是否格式化显示数据,这里为 `false` 表示不进行格式化。 - `DataSourceUpdateMode.OnPropertyChanged`:表示数据源更新模式,当数据源的属性值发生变化时,会自动更新控件的显示。 ### 更详细的使用场景 在将 WinForm 中的 `DataBinding` 信息取出并赋值给 `DataRow` 的代码中,也涉及到了 `Control.DataBinding.Add()` 方法的使用: ```csharp foreach (Control cl in this.Controls) { foreach (System.Windows.Forms.Binding bd in cl.DataBindings) { // 获取绑定字段、数据源和属性名 string bindingField = bd.BindingMemberInfo.BindingField; object ds = bd.DataSource; string propertyName = bd.PropertyName; // 获取控件的属性信息 Type myType = cl.GetType(); PropertyInfo myPropInfo = myType.GetProperty(propertyName); // 将控件的属性值赋给 DataRow dr[bindingField] = myPropInfo.GetValue(cl, null); } } ``` 在这个场景中,通过遍历控件的 `DataBindings` 集合,可以获取每个绑定的详细信息,包括绑定字段、数据源和属性名,然后将控件的属性值赋给 `DataRow`。 ### 与 ListView 结合使用 在使用 `ListView` 进行数据绑定时,也会用到 `Control.DataBinding.Add()` 方法。首先需要创建一个实体类,因为 `DataBinding` 需要实体类作为数据源: ```csharp public class ListviewItemTestDomain { private String text; public ListviewItemTestDomain(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } } ``` 然后创建 `ListView` 的 `Item` 并进行数据绑定,虽然参考内容中未给出具体绑定代码,但基本思路是将 `ListView` 的相关属性(如 `Text`)绑定到实体类的属性。 ### 总结 `Control.DataBinding.Add()` 方法是 WinForm 中实现数据绑定的重要方法,通过合理使用该方法,可以将控件的属性与数据源的属性进行绑定,实现数据的同步显示和更新。在使用时,需要明确要绑定的控件属性、数据源对象和数据源属性名,并根据需要设置格式化和更新模式等参数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值