## Chapter10 表單與基礎控制項
:::success
在本章節中,因為老師沒有教到,所以我會用我自己的方式,將我所學到的統整起來,其中標籤和屬性我就不多贅述,接下來會用到的我才會大概講解,有興趣的可以參閱課本,如有錯誤都歡迎糾正。
:::
### 一、開發環境建置
1.請利用篩選器找出「Windows Forms App」

2.建立專案後顯示如下

3.對表單快速按兩下後會跳出後台可進行編輯

(環境建置結束)
### 二、Label標籤控制項(對應課本10.2)
#### (一)表單的基本屬性和方法
| 外觀 | 預設值 |
| -------- | ---------------- |
| Text(標題) | Form1 |
| BackColor(背景色) | Control |
| Font(字型) | 新細明體9pt |
| Font/Name(字體名稱) | 新細明體 |
| Font/Size(字體大小) | 9 |
| Font/Bold | False |
| Font/Italic | False |
#### (二)實作範例
接下來我會直接用例題的方式來讓大家了解表單的設計與製作
範例:

` ` ` `▲表單載入時` ` ` ` ` ` ` ` ` `▲按一下表單時` ` ` ` ` ` ` ` ▲按兩下表單時
1.在工具列中載入Label標籤
2.以下是完整的程式碼,與課本不同的是,我在程式碼中手動綁定事件處理器,也就是public Form1()裡的方法
```C#=
using System;
using System.Drawing;
using System.Windows.Forms;
namespace form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.Click += new EventHandler(Form1_Click);
this.DoubleClick += new EventHandler(Forml_DoubleClick);
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "表單載入時...";
label1.Font = new Font
("標楷體", 30, FontStyle.Bold | FontStyle.Italic);
this.BackColor = Color.Red;
}
private void Form1_Click(object sender, EventArgs e)
{
label1.Text = "按一下表單...";
this.BackColor = Color.Yellow;
}
private void Forml_DoubleClick(object sender, EventArgs e)
{
label1.Text = "按兩下表單...";
this.BackColor = Color.Aqua;
}
}
}
```
### 三、表單的控制項(10.3~10.7)
#### (一)ToolTip提示控制項
用於懸停在另一個控件時顯示幫助提示或其他信息
| 屬性 | 說明 |
|--------------------|--------------------------------------------------------------|
| Active | 取得或設定值,指出工具提示目前是否在作用中。 |
| AutomaticDelay | 取得或設定工具提示的自動延遲。 |
| AutoPopDelay | 取得或設定當指標靜止於含指定工具提示文字的控制項上時,工具提示保持可見的時間。 |
| BackColor | 取得或設定工具提示的背景色彩。 |
| CanRaiseEvents | 取得值,指出元件是否能引發事件。 |
| Container | 取得包含 IContainer 的 Component。 |
| CreateParams | 取得工具提示視窗的建立參數。 |
| DesignMode | 取得值,指出 Component 目前是否處於設計模式。 |
| Events | 取得附加在這個 Component 上的事件處理常式清單。 |
| ForeColor | 取得或設定工具提示的前景色彩。 |
| InitialDelay | 取得或設定在工具提示出現之前,所經過的時間。 |
| IsBalloon | 取得或設定值,指出工具提示是否應該使用汽球樣式的視窗。 |
| OwnerDraw | 取得或設定值,指出要由作業系統繪製工具提示或是由您提供的程式碼繪製。 |
| ReshowDelay | 取得或設定當指標從某個控制項移動到另一個控制項時,在後續工具提示視窗出現之前,必須經過的時間長度。 |
| ShowAlways | 取得或設定值,指出即使父控制項為非現用時,是否也會顯示工具提示視窗。 |
| Site | 取得或設定 Component 的 ISite。 |
| StripAmpersands | 取得或設定值,以便判斷連字號 (&) 字元的處理方式。 |
| Tag | 取得或設定物件,其中含有與 ToolTip 關聯之程式設計人員提供的資料。 |
| ToolTipIcon | 取得或設定值,以便定義要顯示在工具提示文字旁的圖示類型。 |
| ToolTipTitle | 取得或設定工具提示視窗的標題。 |
| UseAnimation | 取得或設定值,以便判斷顯示工具提示時是否應該使用動畫效果。 |
| UseFading | 取得或設定值,以便判斷顯示工具提示時是否應該使用淡出效果。 |
#### (二)Button (按鈕)
用於觸發操作或事件。

* Text:設置按鈕上的文本。
* Click:按鈕被點擊時觸發的事件。
#### (三)TextBox (文本框)
讓用戶輸入和編輯文本。

* Text:獲取或設置文本框中的文本。
* Multiline:設置是否允許多行輸入。
* PasswordChar:設置密碼字符,用於密碼輸入框。
#### (四)CheckBox (復選框)
讓用戶選擇或取消選擇某個選項。
* Checked:指示復選框是否被選中。
* CheckStateChanged:當復選框狀態改變時觸發的事件。
#### (五)RadioButton (單選按鈕)
讓用戶在一組互斥的選項中選擇一個。
* Checked:指示單選按鈕是否被選中。
* CheckedChanged:當單選按鈕狀態改變時觸發的事件。
### (四)綜合練習
創建一個表單,其中有一個TextBox可以輸入文字,下方的按鈕按下提交以後(幫滑鼠移到上方時,會執行ToolTip),跳出一個提示框顯(顯示你好xxx),範例如下:

1.版面配置
首先,要先從工具箱中拖出lable、TextBox、Button和ToolTip

放好以後可以用手動的排版,也能寫在程式碼中來排版
以下是程式碼:
```C#=
using System;
using System.Windows.Forms;
namespace form1
{
public partial class Form1 : Form
{
public Form1()
{
// 設置表單屬性
this.Text = "輸入姓名";
this.Size = new System.Drawing.Size(400, 300);
// 創建並設置標籤
Label label = new Label();
label.Text = "輸入你的名字:";
label.Location = new System.Drawing.Point(10, 10);
this.Controls.Add(label);
// 創建並設置文本框
TextBox textBox = new TextBox();
textBox.Location = new System.Drawing.Point(120, 10);
this.Controls.Add(textBox);
// 創建並設置按鈕
Button button = new Button();
button.Text = "提交";
button.Location = new System.Drawing.Point(10, 50);
button.Click += (sender, e) => MessageBox.Show($"你好, {textBox.Text}!");
this.Controls.Add(button);
ToolTip toolTip1 = new ToolTip();
toolTip1.SetToolTip(button, "送出資料");
}
[STAThread]
public static void Form()
{
Application.Run(new Form1());
}
}
}
```
做完這些就能執行來看成果嘍!
以上是Chapter 10的內容,如有問題都歡迎詢問~
製作者:陳章銓、蔡佳誼
Email: jerry20050125@gmail.com