C# 程式設計/ .NET Framework/Windows 窗體
外觀
要建立 Windows 桌面應用程式,我們使用由 System.Windows.Forms 名稱空間表示的庫。此名稱空間中一些常用的類包括
- Control - 通用類,其他有用的類(如
Form、TextBox和下面列出的其他類)都派生自此類 - Form - 這是程式視窗的基本類。所有其他控制元件都直接放置在
Form上,或者間接放置在最終駐留在Form上的另一個容器(如TabPage或TabControl)上。在 Visual Studio 中自動建立時,它通常被子類化,稱為Form1。 - Button - 可點選按鈕
- TextBox - 單行或多行文字框,可用於顯示或輸入文字
- RichTextBox - 擴充套件的
TextBox,可以顯示格式化的文字,例如,文字的一部分顏色或使用指定的字型。RichTextBox 還可以顯示廣義的 RTF 文件,包括嵌入的影像。 - Label - 簡單的控制元件,允許顯示一行未格式化的文字,通常用於各種標題和標題
- ListBox - 控制元件顯示多個專案(文字行),可以選擇專案並滾動瀏覽它
- ComboBox - 與
ListBox相似,但類似於下拉選單 - TabControl 和 TabPage - 用於將控制元件分組在選項卡式介面中(非常類似於 Visual Studio 或 Mozilla Firefox 中的選項卡式介面)。
TabControl包含一個TabPage物件的集合。 - DataGrid - 資料網格/表格檢視
The Form 類 (System.Windows.Forms.Form) 是該名稱空間中一個特別重要的部分,因為窗體是 Windows 應用程式的關鍵圖形構建塊。它提供了一個視覺框架,將按鈕、選單、圖示和標題欄組合在一起。整合開發環境 (IDE) 如 Visual C# 和 SharpDevelop 可以幫助建立圖形應用程式,但瞭解如何手動執行這一點很重要
using System.Windows.Forms;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
{
public static void Main()
{
ExampleForm wikibooksForm = new ExampleForm();
wikibooksForm.Text = "I Love Wikibooks"; // specify title of the form
wikibooksForm.Width = 400; // width of the window in pixels
wikibooksForm.Height = 300; // height in pixels
Application.Run(wikibooksForm); // display the form
}
}
上面的示例建立了一個簡單的視窗,標題欄中顯示文字“我愛 Wikibooks”。類似上面的自定義窗體類繼承自 System.Windows.Forms.Form 類。設定任何屬性 Text、Width 和 Height 是可選的。即使您將這些程式碼註釋掉,程式也會編譯併成功執行,但它們允許我們對窗體新增額外的控制。
| 本節內容為空。 您可以透過擴充套件它來幫助 Wikibooks。 |
事件是在使用者或計算機執行操作(例如,單擊按鈕,滑鼠懸停在影像上等)時程式採取的操作。事件處理程式是一個物件,它決定在觸發事件時應採取的操作。
using System.Windows.Forms;
using System.Drawing;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
{
public ExampleForm()
{
this.Text = "I Love Wikibooks"; // specify title of the form
this.Width = 300; // width of the window in pixels
this.Height = 300; // height in pixels
Button HelloButton = new Button();
HelloButton.Location = new Point(20, 20); // the location of button in pixels
HelloButton.Size = new Size(100, 30); // the size of button in pixels
HelloButton.Text = "Click me!"; // the text of button
// When clicking the button, this event fires
HelloButton.Click += new System.EventHandler(WhenHelloButtonClick);
this.Controls.Add(HelloButton);
}
void WhenHelloButtonClick(object sender, System.EventArgs e)
{
MessageBox.Show("You clicked! Press OK to exit this message");
}
public static void Main()
{
Application.Run(new ExampleForm()); // display the form
}
}
| 本節內容為空。 您可以透過擴充套件它來幫助 Wikibooks。 |
Windows 窗體名稱空間包含許多非常有趣的類。其中最簡單且最重要的類之一是 Form 類。窗體是任何 Windows 應用程式的關鍵構建塊。它提供了一個視覺框架,將按鈕、選單、圖示和標題欄組合在一起。窗體可以是模態的和非模態的,所有者和被擁有的,父級和子級。雖然可以使用記事本建立窗體,但使用 VS.NET、C# Builder 或 Sharp Develop 之類的窗體編輯器可以使開發速度更快。在本課中,我們不會使用 IDE。相反,將下面的程式碼儲存到文字檔案並使用命令列編譯器編譯。
using System.Windows.Forms;
using System.Drawing;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
{
public ExampleForm()
{
this.Text = "I Love Wikibooks"; // specify title of the form
this.BackColor = Color.White;
this.Width = 300; // width of the window in pixels
this.Height = 300; // height in pixels
// A Label
Label TextLabel = new Label();
TextLabel.Text = "One Label here!";
TextLabel.Location = new Point(20, 20);
TextLabel.Size = new Size(150, 30);
TextLabel.Font = new Font("Arial", 12); // See! we can modify the font of text
this.Controls.Add(TextLabel); // adding the control to the form
// A input text field
TextBox Box = new TextBox(); // inherits from Control
Box.Location = new Point(20, 60); // then, it have Size and Location properties
Box.Size = new Size(100, 30);
this.Controls.Add(Box); // all class that inherit from Control can be added in a form
}
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ExampleForm()); // display the form
}
}