C#入門教程里的一個例子
namespace Test
{
public class Form1 : Form
{
private TextBox textBox1;
private Button button1;
private Button button2;
private Container components = null;
public Form1()
{
InitializeComponent();//初始化窗體及子控件
}
protected override void Dispose(bool disposing) //釋放對組件的引用,以便垃圾回收器回收無用的內存
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new TextBox();
this.button1 = new Button();
this.button2 = new Button();
this.SuspendLayout();
//textBox1
this.textBox1.Location = new Point(74, 64);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(144, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "C#酷嗎?";
//button1
this.button1.Location = new Point(58, 160);
this.button1.Name = "button1";
this.button1.Size = new Size(72, 74);
this.button1.Text = "點擊我";
this.button1.Click += new System.EventHandler(this.button1OnClick);
//button2
this.button2.Location = new Point(162, 160);
this.button2.Name = "button2";
this.button2.Size = new Size(72, 74);
this.button2.Text = "終結者";
this.button2.Click += new System.EventHandler(this.button2OnClick);
//form1
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.Controls.AddRange(new Control[]{
this.button2,
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "我愛C#";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void main(){
Application.Run(new Form1());//啟動程序
}
//響應單擊按鈕“點擊我”的button1OnClick事件
private void button1OnClick(object sender, EventArgs e){
textBox1.Text = "你好!電腦報";
}
//響應單擊按鈕“終結者”的button2OnClick事件
private void button2OnClick(object sender, EventArgs e){
Application.Exit();//退出應用程序
}
}
} |