运行界面:
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 BindingDemoForm6 : Form
...{
public BindingDemoForm6()
...{
InitializeComponent();
}
private void BindingDemoForm6_Load(object sender, System.EventArgs e)
...{
string[] DepartmentArray = ...{ "生產製造部", "行銷部", "財務部", "會計部", "業務部", "資訊部" };
myComboBox.DataSource = DepartmentArray;
}
private void myComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
...{
lblChoice.Text = "您選取的選項是: " + myComboBox.SelectedItem.ToString();
}
}
}
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 BindingDemoForm6 : Form
...{
public BindingDemoForm6()
...{
InitializeComponent();
}
private void BindingDemoForm6_Load(object sender, System.EventArgs e)
...{
string[] DepartmentArray = ...{ "生產製造部", "行銷部", "財務部", "會計部", "業務部", "資訊部" };
myComboBox.DataSource = DepartmentArray;
}
private void myComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
...{
lblChoice.Text = "您選取的選項是: " + myComboBox.SelectedItem.ToString();
}
}
}
窗体代码:
namespace
ch1
... {
partial class BindingDemoForm6
...{
/**//// <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.lblChoice = new System.Windows.Forms.Label();
this.myComboBox = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// lblChoice
//
this.lblChoice.ForeColor = System.Drawing.Color.Blue;
this.lblChoice.Location = new System.Drawing.Point(137, 145);
this.lblChoice.Name = "lblChoice";
this.lblChoice.Size = new System.Drawing.Size(288, 18);
this.lblChoice.TabIndex = 3;
//
// myComboBox
//
this.myComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.myComboBox.Location = new System.Drawing.Point(137, 33);
this.myComboBox.Name = "myComboBox";
this.myComboBox.Size = new System.Drawing.Size(288, 20);
this.myComboBox.TabIndex = 2;
this.myComboBox.SelectedIndexChanged += new System.EventHandler(this.myComboBox_SelectedIndexChanged);
//
// BindingDemoForm6
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(565, 256);
this.Controls.Add(this.lblChoice);
this.Controls.Add(this.myComboBox);
this.Name = "BindingDemoForm6";
this.Text = "BindingDemoForm6";
this.Load += new System.EventHandler(this.BindingDemoForm6_Load);
this.ResumeLayout(false);
}
#endregion
internal System.Windows.Forms.Label lblChoice;
internal System.Windows.Forms.ComboBox myComboBox;
}
}
... {
partial class BindingDemoForm6
...{
/**//// <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.lblChoice = new System.Windows.Forms.Label();
this.myComboBox = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// lblChoice
//
this.lblChoice.ForeColor = System.Drawing.Color.Blue;
this.lblChoice.Location = new System.Drawing.Point(137, 145);
this.lblChoice.Name = "lblChoice";
this.lblChoice.Size = new System.Drawing.Size(288, 18);
this.lblChoice.TabIndex = 3;
//
// myComboBox
//
this.myComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.myComboBox.Location = new System.Drawing.Point(137, 33);
this.myComboBox.Name = "myComboBox";
this.myComboBox.Size = new System.Drawing.Size(288, 20);
this.myComboBox.TabIndex = 2;
this.myComboBox.SelectedIndexChanged += new System.EventHandler(this.myComboBox_SelectedIndexChanged);
//
// BindingDemoForm6
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(565, 256);
this.Controls.Add(this.lblChoice);
this.Controls.Add(this.myComboBox);
this.Name = "BindingDemoForm6";
this.Text = "BindingDemoForm6";
this.Load += new System.EventHandler(this.BindingDemoForm6_Load);
this.ResumeLayout(false);
}
#endregion
internal System.Windows.Forms.Label lblChoice;
internal System.Windows.Forms.ComboBox myComboBox;
}
}