1. 程式人生 > >C# 下利用程式碼建立按鈕、定時器和標籤

C# 下利用程式碼建立按鈕、定時器和標籤

一般在Form1.Designer.cs 檔案下的 InitializeComponent 函式中進行   this .SuspendLayout();//公有,在窗體建立時自動建立

Timer控制元件 1  申明 private System.Windows.Forms. Timer Timer_j;
2  例項化 this .components =
new System.ComponentModel. Container (); this .Timer_j = new System.Windows.Forms. Timer ( this .components);
3  設定 // // Timer_j // this .Timer_j.Interval = 5000;//定時器初始化 this .Timer_j.Enabled =
true ;//定時器使能 this .Timer_j.Tick += new System. EventHandler ( this .Timer_j_Tick);//新增觸發時間


Button控制元件 1  申明 private System.Windows.Forms. Button button_start; private System.Windows.Forms.
Button button_stop;
2  例項化 this .button_start = new System.Windows.Forms. Button (); this .button_stop = new System.Windows.Forms. Button ();
3  設定 // // button_start // this .button_start.Location = new System.Drawing. Point (200, 200); this .button_start.Name = "button_start" ; this .button_start.Size = new System.Drawing. Size (50, 20); this .button_start.Text = "開始" ; this .button_start.TabIndex = 1; // // button_stop // this .button_stop.Location = new System.Drawing. Point (400, 200); this .button_stop.Name = "button_stop" ; this .button_stop.Size = new System.Drawing. Size (50, 20); this .button_stop.Text = "結束" ; this .button_stop.TabIndex = 2;
4  加入視窗 this .Controls.Add( this .button_start); this .Controls.Add( this .button_stop);

Label控制元件 1  申明 private System.Windows.Forms. Label label_dis;
2  例項化 this .label_dis = new System.Windows.Forms. Label ();
3  設定 // // label_dis // this .label_dis.Location = new System.Drawing. Point (200, 100); this .label_dis.Name = "label_dis" ; this .label_dis.AutoSize = true ; //自動調節大小 this .label_dis.Text = "ljj" ; this .label_dis.TabIndex = 4;
4  加入視窗 this .Controls.Add( this .label_dis);