{%hackmd theme-dark %} <h1><center><img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50> OOP 物件導向<img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50></center></h1> ## `CH1 認識C#` ### `Hello world` ![image](https://hackmd.io/_uploads/rkHhu6l5T.png) ### `C# 中的數字` ![image](https://hackmd.io/_uploads/SyL3u6lcT.png) ### `分支和迴圈` ![image](https://hackmd.io/_uploads/SyDhdTg5p.png) ### `清單集合` ![image](https://hackmd.io/_uploads/r1dnd6e56.png) ### `心得` 從學習 C# 的物件導向程式設計,我發現它是一種非常有條理且結構化的程式語言。它提供了許多有用的特徵,例如類別、物件、繼承、多型等,這些特徵可以幫助程式設計師將程式碼組織成更易於理解和維護的結構。 但是,我個人不太喜歡 C#,主要有幾個原因。語法方面,它的語法太過複雜,而且有很多不必要的關鍵字,這使得程式碼看起來很雜亂。速度方面,它的編譯速度非常慢,尤其是當程式碼量大的時候,編譯時間可能會長達數分鐘。 ## `CH2 Visual C#與.NET` ### `以.NET 6.0產生主控台程式` 程式碼 ```csharp= Console.WriteLine("我是.NET 6.0"); ``` ![image](https://hackmd.io/_uploads/HJUCFrxnp.png) 執行結果 ![image](https://hackmd.io/_uploads/HyBgpSl2a.png) ### 資料做輸入和輸出的處理 程式碼&結果 ```csharp= Console.Write("請輸入你的名字: "); Console.WriteLine("Good Day! " + Console.ReadLine()); ``` ![image](https://hackmd.io/_uploads/SJrde8x3p.png) ### 格式化輸出 程式碼&結果 ```csharp= Console.Write("請輸入你的名字: "); var name = Console.ReadLine(); Console.Write("請輸入提款金額: "); var money = Convert.ToInt32(Console.ReadLine()); Console.WriteLine($"Hi! {name}, 提款金額: {money:c0}"); ``` ![image](https://hackmd.io/_uploads/SkKMWIxnT.png) ### 視窗應用程式(參考第一個 Windows Form 應用程式 PPT) 程式設計: ![image](https://hackmd.io/_uploads/SklWKg9hp.png) 程式碼 ```csharp= namespace school //1206221 張家笛 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text = DateTime.Now.ToLongDateString(); } private void button2_Click(object sender, EventArgs e) { label1.Text = "你好"; } } } ``` 執行結果: 點擊'你好'按鈕後 ![image](https://hackmd.io/_uploads/ByH8Fgqnp.png) 點擊'現在時間'按鈕後 ![image](https://hackmd.io/_uploads/By2Dtx9np.png) ## `CH3資料型別與變數` ### 資料型別轉換 由於Visual C# 語法嚴謹,不允許不同資料型別間的資料直接做運算,若碰到兩個不同資料型別的資料需做運算時,必須將資料做轉換成同一資料型別才能做運算。 **1.自動轉型** 當兩個資料要做運算時,若運算結果的目的資料型別有效範圍大於來源資料型別。運算前來源資料事先不必轉換資料型別,電腦系統會在運算前將之「自動轉型」再進行處理。 例如:int(來源資料型別)→long(目的資料型別)、float→double、int→float、long→float、long→double、… **2.明確轉型** Visual C# 提供一種型別轉換(Type Cast)方法,能強迫資料轉換成其他資料型別。 **3.Parse方法** 資料的內容是文數字,如:"1234"、"3.14"。文數字是字串資料,非數值資料,不能直接拿來做數值四則運算。 ### 隨堂練習(0226) #### 1.數值格式化練習 程式碼 ```csharp= using static System.Console; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _0226___1 { internal class Program { static void Main(string[] args) { Console.WriteLine("我的學號是1206221"); Console.WriteLine("我的bmi是14.5"); Console.WriteLine("我的每周伙食花費NT$700"); } } } ``` 執行結果: ![image](https://hackmd.io/_uploads/SJHzjUQaT.png) #### 2.貸款試算練習 ![image](https://hackmd.io/_uploads/H1jpjDQTa.png) ```csharp= namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); pictureBox1.Image = Properties.Resources.pad; } private void button1_Click(object sender, EventArgs e) { try { int money, loan, year, month; double rate, payRate, mRate; loan = int.Parse(textBox1.Text); rate = int.Parse(textBox2.Text) / 100.0; year = int.Parse(textBox3.Text); mRate = rate / 12; month = year * 12; payRate = ((Math.Pow((1 + mRate), month) * mRate)) / (Math.Pow((1 + mRate), month) - 1); money = (int)(loan * payRate + 0.5); textBox4.Text = money.ToString(); pictureBox1.Image = Properties.Resources.thonk; } catch { textBox4.Text = "輸入錯誤,請重新輸入:"; pictureBox1.Image = Properties.Resources.loading; } } private void button2_Click_1(object sender, EventArgs e) { textBox1.Text = " "; textBox2.Text = " "; textBox3.Text = " "; textBox4.Text = " "; pictureBox1.Image = Properties.Resources.loading2; } private void Form1_Load(object sender, EventArgs e) { } } } ``` 執行結果 1.初始畫面: ![image](https://hackmd.io/_uploads/H1Al3DXaa.png) 2.計算畫面: ![image](https://hackmd.io/_uploads/B1Of2v7pT.png) 3.錯誤畫面: ![image](https://hackmd.io/_uploads/ry6EnDmTT.png) 4.清除畫面: ![image](https://hackmd.io/_uploads/HksQ2vQ6a.png) ### 隨堂練習(0304) #### 1.login 程式碼: ```csharp= namespace login { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string Name = Microsoft.VisualBasic.Interaction.InputBox("請輸入姓名", "輸入"); MessageBox.Show(Name + "歡迎您!", "歡迎", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Text = Name; } private void button1_Click_1(object sender, EventArgs e) { try { int money = int.Parse(textBox2.Text); label3.Text = $"提款金額:{money:n}元"; } catch { MessageBox.Show("請輸入整數", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("感謝使用程式", "謝謝", MessageBoxButtons.OK, MessageBoxIcon.None); textBox1.Text = ""; textBox2.Text = ""; label3.Text = ""; } } } ``` ![image](https://hackmd.io/_uploads/HyZz6g2a6.png) ![image](https://hackmd.io/_uploads/Byirpenpa.png) ![image](https://hackmd.io/_uploads/HkTBaxn6p.png) ![image](https://hackmd.io/_uploads/SyJUpg36T.png) ![image](https://hackmd.io/_uploads/Hkg86g2ap.png) ![image](https://hackmd.io/_uploads/BJG26g266.png) #### 2.四則運算 ## CH5 身份證字號產生器/檢查器 ```csharp= namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string idChar = "abcdefghjklmnpqrstuvxywzio"; string[] idCry ={"台北市","台中市","基隆市","台南市","高雄市","新北市","宜蘭縣","桃園縣", "新竹縣","苗栗國","台中市","南投縣","彰化縣","雲林縣","嘉義縣","台南市","高雄市", "屏東縣","花蓮國","台東縣","澎湖縣","陽明山","金門縣","連江縣","嘉義市","新竹市"}; string msg; string id, sex; Random r = new Random(); private void btnGen_Click(object sender, EventArgs e) { int sum = 0, idPos = 0; idPos = r.Next(26); id = idChar.Substring(idPos, 1).ToUpper(); sum = (idPos + 10) / 10 + ((idPos + 10) % 10) * 9; idPos = r.Next(2) + 1; id = id + idPos.ToString(); sum += idPos * 8; for (int i = 2; i < 9; i++) { idPos = r.Next(10); id = id + idPos.ToString(); sum += idPos * (9 - i); } if (sum % 10 == 0) idPos = 0; else idPos = 10 - (sum % 10); id = id + idPos.ToString(); sum += idPos; txtld.Text = id; } private void btnCheck_Click(object sender, EventArgs e) { if (txtld.Text.Trim().Length == 10) { int sum = 0, idPos = 0; id = txtld.Text; idPos = idChar.IndexOf(id.Substring(0, 1).ToLower()); sum = (idPos + 10) / 10 + (idPos % 10) * 9; sex = id.Substring(1, 1); for (int i = 1; i < 9; i++) sum += Convert.ToInt32(id.Substring(i, 1)) * (9 - i); sum = sum + Convert.ToInt32(id.Substring(9, 1)); if (sum % 10 == 0) msg = sum.ToString() + ",正確," + idCry[idPos]; else msg = sum.ToString() + ",錯誤,"; if (sex == "1") msg += ",MAN"; else msg += "GIRL"; } else msg = "錯誤"; IbIShow.Text = msg; } } } ``` 預設 ![image](https://hackmd.io/_uploads/S1lHxXHRa.png) 產生 ![image](https://hackmd.io/_uploads/BybHxmHR6.png) 驗證 ![image](https://hackmd.io/_uploads/rJzHgQBCp.png) 驗證錯誤 ![image](https://hackmd.io/_uploads/r17Blmr06.png) ## 0318 ### 數字三角形 ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace _0318_1 { internal class Program { static void Main(string[] args) { Write("輸入一整數N :"); int N = int.Parse(ReadLine()); if (N % 2 == 0 ) //偶數 { for (int i = 1;i < N + 1;i++) { for (int j = i;j <= i;j++) { Write(j); } WriteLine();//換行 } } else { for (int i = N;i > 0;i--) { for (int j = i;j > 0; j--) { Write(j); } WriteLine(); } } } } } ``` 執行畫面: ![upload_82fa1afcbd9dca18c74bdf582892fe8e](https://hackmd.io/_uploads/HyMlaMxgR.png) ![HkuFDmrRT](https://hackmd.io/_uploads/HyBlaGgl0.png) ### 5進制轉換 ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace _0318_2 { internal class Program { static void Main(string[] args) { Write("輸入N:"); int N = int.Parse(ReadLine()); if (N < 5) { WriteLine(N); } else { int a = N / 5; string c = Convert.ToString(a); int b = N % 5; string d = Convert.ToString(b); WriteLine(c + d); } } } } ``` ![SJdX57HR6](https://hackmd.io/_uploads/HyzfTzgg0.png) ![upload_5febfc284c30e5f13dbeeab4fdf45869](https://hackmd.io/_uploads/BybVTMeeA.png) ### 計算字元出現頻率程式碼: ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { while (true){ // 訊一乙20康景棠 Console.Write("請輸入一個字串: "); string n = Console.ReadLine(); string output = "結果:"; for (int i = 0; i < 128; i++) { char c = (char)i; int count = n.Split(c).Length - 1; if (count > 0) { output += "\n" + c +": "+Convert.ToString(count)+'次'; } } Console.WriteLine(output); } } } } ``` ![H1CrzXSAa](https://hackmd.io/_uploads/ryikzXxlC.png) ### 考慮如下圖的演算法,並將數列印出 ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace _0318_5 { internal class Program { static void Main(string[] args) { Write("輸入N:"); int N = int.Parse(ReadLine()); while(N != 1) { if (N % 2 == 1) { N = 3 * N + 1; WriteLine(N); } else { N /= 2; WriteLine(N); } } } } } ``` ![S1U7hmHRp](https://hackmd.io/_uploads/r1V-M7ee0.png) ## 20240318 加分題 ### 質數判斷 程式碼: ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace _0318_plus_score_1 { internal class Program { static void Main(string[] args) { Write("輸入N:"); int N = int.Parse(ReadLine()); for (int i =2; i <= N - 1; i++) { if (N % i == 0) { WriteLine("此數不是質數"); break; } else { WriteLine("此數是質數"); break; } } } } } ``` ![image](https://hackmd.io/_uploads/SJ_OLDb4R.png) ### BMI程式 程式碼: ```csharp= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; namespace _0318_plus_score_2 { internal class Program { static void Main(string[] args) { Write("輸入體重:"); int w = int.Parse(ReadLine()); Write("身高(公分):"); float h = float.Parse(ReadLine()); float h1 = h / 100; float h2 = h1 * h1; double BMI = w / h2; double BMI1 = (Math.Round(BMI)); if (BMI1 < 18.5) { WriteLine("體重過輕"); WriteLine(BMI1); } else if (18.5 <= BMI1 & BMI1 < 24) { WriteLine("健康體位"); WriteLine(BMI1); } else if (24 <= BMI1 & BMI1 < 27) { WriteLine("過重"); WriteLine(BMI1); } else if (27 <= BMI1 & BMI1 < 30) { WriteLine("輕度肥胖"); WriteLine(BMI1); } else if (30 <= BMI1 & BMI1 < 35) { WriteLine("中度肥胖"); WriteLine(BMI1); } else { WriteLine("重度肥胖"); WriteLine(BMI1); } } } } ``` ![image](https://hackmd.io/_uploads/SJ8lPDbN0.png) ## 0401 ### IDGenerator ```csharp= using System.Windows.Forms; namespace IDGenerator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string idChar = "abcdefghjklmnpqrstuvxywzio"; string[] idCry = {"台北市","台中市","基隆市","台南市","高雄市","新北市","宜蘭縣","桃園縣", "新竹縣","苗栗國","台中市","南投縣","彰化縣","雲林縣","嘉義縣","台南市","高雄市", "屏東縣","花蓮國","台東縣","澎湖縣","陽明山","金門縣","連江縣","嘉義市","新竹市"}; string[] idSex = { "任意", "男", "女" }; string msg, idBorn; int num, sex; Random r = new Random(); private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("任意縣市"); for (int i = 0; i < 26; i++) { listBox1.Items.Add(idCry[i]); } listBox1.SetSelected(0, true); sex = 1; num = 1; textBox1.Text = num.ToString(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { idBorn = listBox1.SelectedItem.ToString(); msg = string.Format("{0}_{1}_{2}", idBorn, idSex[sex], num); textBox2.Text = msg; } private void chkRadio() { if (radioButton1.Checked) { sex = 1; } if (radioButton2.Checked) { sex = 2; } if (radioButton3.Checked) { sex = 0; } showMsg(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { chkRadio(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { chkRadio(); } private void radioButton3_CheckedChanged(object sender, EventArgs e) { chkRadio(); } private void showMsg() { msg = string.Format("{0}_{1}_{2}", idBorn, idSex[sex], num); textBox2.Text = msg + Environment.NewLine; } string idGen() { int sum = 0, idPos = 0; string id; if (listBox1.SelectedItem.ToString() == "任意縣市") idPos = r.Next(26); else idPos = listBox1.SelectedIndex - 1; id = idChar.Substring(idPos, 1).ToUpper(); sum = (idPos + 10 / 10) + (idPos % 10) * 9; if (sex == 0) idPos = r.Next(2) + 1; else idPos = sex; id = id + idPos.ToString(); sum += idPos * 8; for (int i = 2; i < 9; i++) { idPos = r.Next(10); id = id + idPos.ToString(); sum += idPos * (9 - i); } if (sum % 10 == 0) idPos = 0; else idPos = 10 - (sum % 10); id = id + idPos.ToString(); sum += idPos; return id; } private void button1_Click(object sender, EventArgs e) { textBox2.Text = ""; num = Convert.ToInt32(textBox1.Text); for (int i = 0; i < num; i++) { textBox2.Text += idGen() + Environment.NewLine; } } private void button2_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "txt|*.txt"; saveFileDialog1.FileName = msg; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { StreamWriter sr = new StreamWriter(saveFileDialog1.FileName); sr.Write(textBox2.Text); sr.Close(); } } } } ``` ![image](https://hackmd.io/_uploads/BkNSz7llC.png) ![image](https://hackmd.io/_uploads/Hk-IzQll0.png) ![image](https://hackmd.io/_uploads/BkZDMQxxC.png) ![image](https://hackmd.io/_uploads/r1CPfQglC.png) ## 0408 ### 課後練習 練習一 ```csharp= namespace _0408a { public partial class Form1 : Form { public Form1() { InitializeComponent(); label3.Text = "Molly, Eric, Johseph, Peter, Iron\n24, 26, 24, 26, 28, 25"; label1.Text = ""; label2.Text = ""; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string[] name = { "Molly", "Eric", "Johseph", "Peter", "Iron", "Priyanka" }; int[] age = { 24, 26, 24, 26, 28, 25 }; int index = Array.IndexOf(age, 24); label1.Text = "24歲的人:"; while (index >= 0) { label1.Text += name[index]; index = Array.IndexOf(age, 24, index + 1); } Array.Sort(age); var key = Array.BinarySearch(age, 25); if (key >= 0) { label2.Text = $"\n找到年齡25! 位置 = {key}"; } else { label2.Text = $"\n未找到年齡25! 位置 = {key}"; } } } } ``` ![image](https://hackmd.io/_uploads/HyH49JbgA.png) ### 課後練習 練習二 ```csharp= namespace _0408_practice_2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); label1.Text = ""; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { int outer, inner; int[] sum = new int[3]; string[] name = { "Mary", "Tomas", "John" }; foreach(string item in name) { label1.Text += item + " "; } label1.Text += "\n"; int[,] score = { { 75, 64, 96 }, { 55, 67, 39 }, { 45, 92, 85 }, { 71, 69, 81 } }; int row = score.GetLength(0); int column = score.GetLength(1); for(outer = 0; outer < row; outer++) { for(inner = 0; inner < column; inner++) { label1.Text += $"{score[outer, inner], 7}"; } label1.Text += "\n"; sum[0] += score[outer, 0]; sum[1] += score[outer, 1]; sum[2] += score[outer, 2]; } label1.Text += "\n" + "--------------------\n"; label1.Text += $"Sum : {sum[0]} {sum[1], 5} {sum[2], 6}"; } } } ``` ![image](https://hackmd.io/_uploads/SklQdD-V0.png) ### 自我練習 練習一 ```csharp= namespace _0408b { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string[] stu = new string[] { "趙一", "林二", "張三", "李四", "王五" }; int[] score = new int[] { 95, 100, 100, 92, 100 }; string msg = "一百分學生:"; int index = Array.IndexOf(score, 100); while (index >= 0) { msg += stu[index] + ","; index = Array.IndexOf(score, 100, index + 1); } MessageBox.Show(msg); } } } ``` ![image](https://hackmd.io/_uploads/SJI05kZg0.png) ## 0429 ### 練習三 程式碼: ```csharp= namespace _0429_1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] song = new string[] { "不為誰而作的歌", "餘波盪漾","後來的我們", "不該", "年輪說", "滿座", "關鍵詞", "天真有邪","獨善其身", "一次幸福的機會" }; //singer字串陣列存放歌手姓名 string[] singer = new string[] { "林俊傑", "田馥甄", "五月天", "周杰倫", "楊丞琳", "李榮浩", "林俊傑", "林宥嘉","田馥甄", "蕭敬騰" }; int[] no = new int[10]; // no整數陣列存放排名 private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < no.Length; i++) { no[i] = i + 1; } button1_Click(sender, e); } private void button1_Click(object sender, EventArgs e) { int[] no2 = new int[no.Length]; no.CopyTo(no2, 0); Array.Sort(no, song); Array.Sort(no2, singer); string msg = "排名\t歌手\t歌曲" + Environment.NewLine; for (int i = 0; i < song.Length; i++) { msg += $"{no[i]}\t{singer[i]}\t{song[i]}" + Environment.NewLine; } textBox1.Text = msg; } private void button2_Click(object sender, EventArgs e) { string[] song2 = new string[song.Length]; song.CopyTo(song2, 0); Array.Sort(song, no); Array.Sort(song2, singer); string msg = "排名\t歌手\t歌曲" + Environment.NewLine; for (int i = 0; i < song.Length; i++) { msg += $"{no[i]}\t{singer[i]}\t{song[i]}" + Environment.NewLine; } textBox1.Text = msg; } private void button3_Click(object sender, EventArgs e) { string search = textBox2.Text; string msg = $"找不到{search}"; int index = Array.IndexOf(singer, search); if(index >= 0) { msg = "排名\t歌手\t歌曲" + Environment.NewLine; while(index >= 0) { msg += $"{no[index]}\t{singer[index]}\t{song[index]}" + Environment.NewLine; index = Array.IndexOf(singer, search, index + 1); } } textBox1.Text = msg; } } } ``` ![image](https://hackmd.io/_uploads/ByZnUO0WR.png) ![image](https://hackmd.io/_uploads/H1O2LuRbR.png) ![image](https://hackmd.io/_uploads/BkGT8_RZA.png) ### 練習四 程式碼: ```csharp= namespace _0429_2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string[] books = new string[] { "三國演義", "水滸傳", "西遊記", "紅樓夢", "聊齋志異", "鏡花緣" }; ClstBooks.Items.AddRange(books); ClstBooks.CheckOnClick = true; ClstBorrow.CheckOnClick = true; } private void ClstBooks_SelectedIndexChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (ClstBooks.CheckedItems.Count + ClstBorrow.Items.Count >= 3) { MessageBox.Show("最多借兩本書!", "注意!"); for (int i = 0; i < ClstBooks.Items.Count; i++) { ClstBooks.SetItemChecked(i, false); } } else { for (int i = ClstBooks.Items.Count - 1; i >= 0; i--) { if (ClstBooks.GetItemChecked(i) == true) { ClstBorrow.Items.Add(ClstBooks.Items[i]); ClstBooks.Items.RemoveAt(i); } } } } private void button2_Click(object sender, EventArgs e) { foreach (int i in ClstBorrow.CheckedIndices) { ClstBooks.Items.Add(ClstBorrow.Items[i]); } for (int i = ClstBorrow.Items.Count - 1; i >= 0; i--) { if (ClstBorrow.GetItemChecked(i) == true) { ClstBorrow.Items.RemoveAt(i); } } } } } ``` 畫面 ![image](https://hackmd.io/_uploads/rJBZyuRW0.png) ![image](https://hackmd.io/_uploads/B1uMJOA-R.png) ![image](https://hackmd.io/_uploads/SyMmku0Z0.png) ![image](https://hackmd.io/_uploads/Sk3mkORZC.png) ![image](https://hackmd.io/_uploads/HyVS1dAZC.png) ![image](https://hackmd.io/_uploads/ryyO1_0WA.png) ![image](https://hackmd.io/_uploads/Hkc_kO0bA.png) ```csharp= namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { PicShow.SizeMode = PictureBoxSizeMode.StretchImage; PicShow.Height = 90; PicShow.Width = 90; // 設定圖片高度和寬度 PicShow.Image = Image.FromFile("D:\\image\\pic1.png"); // 載入圖檔 TkbPic.Maximum = 4; TkbPic.Minimum = 1;// 設定TkbPic的最大、最小值 VsbHeight.Maximum = 180; VsbHeight.Minimum = 1; // 設定最大、最小值 VsbHeight.LargeChange = 1; // 設定VsbHeight的快動值 = 1 VsbHeight.Value = PicShow.Height; // VsbHeight的Value值=圖片高度 HsbWidth.Maximum = 180; HsbWidth.Minimum = 1; // 設定最大、最小值 HsbWidth.LargeChange = 1; // 設定HsbWidth的快動值 = 1 HsbWidth.Value = PicShow.Width; // HsbWidth的Value值=圖片寬度 Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); Ttip.SetToolTip(TkbPic, "圖片1"); } private void VsbHeight_Scroll(object sender, ScrollEventArgs e) { PicShow.Height = VsbHeight.Value; Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); } private void HsbWidth_Scroll(object sender, ScrollEventArgs e) { PicShow.Width = HsbWidth.Value; Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); } private void TkbPic_Scroll(object sender, EventArgs e) { PicShow.Image = Image.FromFile($"D:\\image\\pic{TkbPic.Value}.png"); Ttip.SetToolTip(TkbPic, $"圖片{TkbPic.Value}"); } } } ``` ![image](https://hackmd.io/_uploads/BJUn7MkXR.png) ![image](https://hackmd.io/_uploads/Sk-pmGy70.png) ![image](https://hackmd.io/_uploads/BkTTmM17A.png) ![image](https://hackmd.io/_uploads/BJU0XfkXR.png)