程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 在C#中調試Windows窗體程序

在C#中調試Windows窗體程序

編輯:關於C#

在控制台應用程序中Main函數是程序的入口點。同樣地,在Windows窗體應用程序中,Main函數也是程序入口點。這可以通過調試看出來,方法如下所示。

(1)打開或新建一個Windows窗體應用程序,如前面的FormsTest應用程序。

(2)單擊“調試”|“逐句調試”命令,也可以按快捷鍵F11。可以看到,程序會跳轉到Program.cs文件。指示運行的黃色箭頭指向Main函數的起始位置。

(3)繼續按F11鍵,直到運行箭頭移動到函數最後一句,代碼如下所示。


Application.Run(new Form1());

該語句表示,開始應用程序消息循環。其參數new Form1()用於實例化Form1類,這個類就是窗體類Form的一個派生類。對界面設計和事件的處理代碼都放在了Form1類中。

(4)再按F11鍵,程序運行至Form1.Designer.cs文件。該文件中存放了有關界面設計的初始化內容,如控件、控件布局、控件事件(或者叫委托)的處理程序等,代碼如下。

/* Form1.Designer.cs文件 */
namespace FormsTest
{
partial class Form1
{
/// <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);
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 211);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(167, 211);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}

一般來說,程序員不必直接編寫上面的代碼。因為Form類會自動處理這些事情。前面講過,這些事情包括了控件、控件布局、控件事件的處理程序等。程序員也不必關心事件何時會被調用,何時結束。程序員所需要做的就是編寫響應事件的具體代碼。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved