运行界面:
cs代码:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ch1
... {
public partial class BindingDemoForm14 : Form
...{
// DataTable 物件的類別層級建立
DataTable colorSourceTable = MakeColorTable();
public BindingDemoForm14()
...{
InitializeComponent();
}
private void BindingDemoForm14_Load(object sender, System.EventArgs e)
...{
BindControlProperties();
}
private void BindControlProperties()
...{
// 將 ComboBox 控制項繫結至 colorSourceTable 資料表的「色彩描述」欄位
ComboBoxColor.DataSource = colorSourceTable;
ComboBoxColor.DisplayMember = "色彩描述";
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
TextBox1.DataBindings.Clear();
// 將 TextBox 控制項的 Text、BackColor 與 ForeColor 屬性繫結至 DataTable 的欄位
TextBox1.DataBindings.Add("Text", colorSourceTable, "色彩描述");
TextBox1.DataBindings.Add("BackColor", colorSourceTable, "背景色");
TextBox1.DataBindings.Add("ForeColor", colorSourceTable, "前景色");
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
lblFormCaption.DataBindings.Clear();
// 將 Label 控制項的 Text 屬性繫結至表單的 Text 屬性
lblFormCaption.DataBindings.Add("Text", this, "Text");
// 將 Label 控制項的 BackColor 屬性繫結至 TextBox 控制項的 ForeColor 屬性
lblFormCaption.DataBindings.Add("BackColor", TextBox1, "ForeColor");
// 將 Label 控制項的 ForeColor 屬性繫結至 TextBox 控制項的 BackColor 屬性
lblFormCaption.DataBindings.Add("ForeColor", TextBox1, "BackColor");
}
private static DataTable MakeColorTable()
...{
// 建立一個內含三個欄位的 DataTable 物件, 其中兩個欄位內含 Color 物件。
DataTable colorSourceTable = new DataTable("colorSourceTable");
colorSourceTable.Columns.Add("色彩描述");
colorSourceTable.Columns.Add("背景色", typeof(Color));
colorSourceTable.Columns.Add("前景色", typeof(Color));
// 於資料表中加入三筆資料記錄
DataRow myDataRow;
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "紅底黃字";
myDataRow["背景色"] = Color.Red;
myDataRow["前景色"] = Color.Yellow;
colorSourceTable.Rows.Add(myDataRow);
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "白底藍字";
myDataRow["背景色"] = Color.White;
myDataRow["前景色"] = Color.Blue;
colorSourceTable.Rows.Add(myDataRow);
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "紫底水藍字";
myDataRow["背景色"] = Color.Purple;
myDataRow["前景色"] = Color.Cyan;
colorSourceTable.Rows.Add(myDataRow);
return colorSourceTable;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ch1
... {
public partial class BindingDemoForm14 : Form
...{
// DataTable 物件的類別層級建立
DataTable colorSourceTable = MakeColorTable();
public BindingDemoForm14()
...{
InitializeComponent();
}
private void BindingDemoForm14_Load(object sender, System.EventArgs e)
...{
BindControlProperties();
}
private void BindControlProperties()
...{
// 將 ComboBox 控制項繫結至 colorSourceTable 資料表的「色彩描述」欄位
ComboBoxColor.DataSource = colorSourceTable;
ComboBoxColor.DisplayMember = "色彩描述";
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
TextBox1.DataBindings.Clear();
// 將 TextBox 控制項的 Text、BackColor 與 ForeColor 屬性繫結至 DataTable 的欄位
TextBox1.DataBindings.Add("Text", colorSourceTable, "色彩描述");
TextBox1.DataBindings.Add("BackColor", colorSourceTable, "背景色");
TextBox1.DataBindings.Add("ForeColor", colorSourceTable, "前景色");
// 在加入新的 Binding 物件之前先移除集合內所有的 Binding 物件
lblFormCaption.DataBindings.Clear();
// 將 Label 控制項的 Text 屬性繫結至表單的 Text 屬性
lblFormCaption.DataBindings.Add("Text", this, "Text");
// 將 Label 控制項的 BackColor 屬性繫結至 TextBox 控制項的 ForeColor 屬性
lblFormCaption.DataBindings.Add("BackColor", TextBox1, "ForeColor");
// 將 Label 控制項的 ForeColor 屬性繫結至 TextBox 控制項的 BackColor 屬性
lblFormCaption.DataBindings.Add("ForeColor", TextBox1, "BackColor");
}
private static DataTable MakeColorTable()
...{
// 建立一個內含三個欄位的 DataTable 物件, 其中兩個欄位內含 Color 物件。
DataTable colorSourceTable = new DataTable("colorSourceTable");
colorSourceTable.Columns.Add("色彩描述");
colorSourceTable.Columns.Add("背景色", typeof(Color));
colorSourceTable.Columns.Add("前景色", typeof(Color));
// 於資料表中加入三筆資料記錄
DataRow myDataRow;
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "紅底黃字";
myDataRow["背景色"] = Color.Red;
myDataRow["前景色"] = Color.Yellow;
colorSourceTable.Rows.Add(myDataRow);
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "白底藍字";
myDataRow["背景色"] = Color.White;
myDataRow["前景色"] = Color.Blue;
colorSourceTable.Rows.Add(myDataRow);
myDataRow = colorSourceTable.NewRow();
myDataRow["色彩描述"] = "紫底水藍字";
myDataRow["背景色"] = Color.Purple;
myDataRow["前景色"] = Color.Cyan;
colorSourceTable.Rows.Add(myDataRow);
return colorSourceTable;
}
}
}
窗体代码:
namespace
ch1
... {
partial class BindingDemoForm14
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Label2 = new System.Windows.Forms.Label();
this.lblFormCaption = new System.Windows.Forms.Label();
this.ComboBoxColor = new System.Windows.Forms.ComboBox();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// Label2
//
this.Label2.AutoSize = true;
this.Label2.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.Label2.Location = new System.Drawing.Point(107, 27);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(124, 16);
this.Label2.TabIndex = 8;
this.Label2.Text = "請選取色彩設定:";
//
// lblFormCaption
//
this.lblFormCaption.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblFormCaption.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.lblFormCaption.Location = new System.Drawing.Point(55, 152);
this.lblFormCaption.Name = "lblFormCaption";
this.lblFormCaption.Size = new System.Drawing.Size(520, 18);
this.lblFormCaption.TabIndex = 11;
this.lblFormCaption.Text = "lblFormCaption";
//
// ComboBoxColor
//
this.ComboBoxColor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ComboBoxColor.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.ComboBoxColor.Location = new System.Drawing.Point(275, 27);
this.ComboBoxColor.Name = "ComboBoxColor";
this.ComboBoxColor.Size = new System.Drawing.Size(248, 24);
this.ComboBoxColor.TabIndex = 9;
//
// TextBox1
//
this.TextBox1.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.TextBox1.Location = new System.Drawing.Point(55, 115);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(520, 27);
this.TextBox1.TabIndex = 10;
this.TextBox1.Text = "TextBox1";
//
// BindingDemoForm14
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(637, 223);
this.Controls.Add(this.Label2);
this.Controls.Add(this.lblFormCaption);
this.Controls.Add(this.ComboBoxColor);
this.Controls.Add(this.TextBox1);
this.Name = "BindingDemoForm14";
this.Text = "BindingDemoForm14";
this.Load += new System.EventHandler(this.BindingDemoForm14_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label lblFormCaption;
internal System.Windows.Forms.ComboBox ComboBoxColor;
internal System.Windows.Forms.TextBox TextBox1;
}
}
... {
partial class BindingDemoForm14
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Label2 = new System.Windows.Forms.Label();
this.lblFormCaption = new System.Windows.Forms.Label();
this.ComboBoxColor = new System.Windows.Forms.ComboBox();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// Label2
//
this.Label2.AutoSize = true;
this.Label2.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.Label2.Location = new System.Drawing.Point(107, 27);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(124, 16);
this.Label2.TabIndex = 8;
this.Label2.Text = "請選取色彩設定:";
//
// lblFormCaption
//
this.lblFormCaption.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblFormCaption.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.lblFormCaption.Location = new System.Drawing.Point(55, 152);
this.lblFormCaption.Name = "lblFormCaption";
this.lblFormCaption.Size = new System.Drawing.Size(520, 18);
this.lblFormCaption.TabIndex = 11;
this.lblFormCaption.Text = "lblFormCaption";
//
// ComboBoxColor
//
this.ComboBoxColor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ComboBoxColor.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.ComboBoxColor.Location = new System.Drawing.Point(275, 27);
this.ComboBoxColor.Name = "ComboBoxColor";
this.ComboBoxColor.Size = new System.Drawing.Size(248, 24);
this.ComboBoxColor.TabIndex = 9;
//
// TextBox1
//
this.TextBox1.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.TextBox1.Location = new System.Drawing.Point(55, 115);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(520, 27);
this.TextBox1.TabIndex = 10;
this.TextBox1.Text = "TextBox1";
//
// BindingDemoForm14
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(637, 223);
this.Controls.Add(this.Label2);
this.Controls.Add(this.lblFormCaption);
this.Controls.Add(this.ComboBoxColor);
this.Controls.Add(this.TextBox1);
this.Name = "BindingDemoForm14";
this.Text = "BindingDemoForm14";
this.Load += new System.EventHandler(this.BindingDemoForm14_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label lblFormCaption;
internal System.Windows.Forms.ComboBox ComboBoxColor;
internal System.Windows.Forms.TextBox TextBox1;
}
}