--- tags: 課程111ERP02 --- # 民國111.10.24 C# ## 第一節 專案 project prj 方案 solution sln 元件 component 控制項 control 屬性 property 類別 class 物件 object ``` private void button1_Click(object sender, EventArgs e) { MessageBox.Show(" HELLO "); } // 斜線 (slash) //是C語言的 註釋又稱註解或附註 (COMMENT) \\ 倒斜線 (back slash) private void button2_Click(object sender, EventArgs e) { MessageBox.Show("早安" + "午安" + "晚安"); MessageBox.Show("早安" +"\n"+ "午安" +"\n"+ "晚安"); // MessageBox.Show("早安" + "午安" + "晚安"); } \n n: new line ``` //作業1: ``` private void button4_Click(object sender, EventArgs e) { MessageBox.Show("床前明月光" +"\n"+ "疑似地上霜" +"\n" + "舉頭望明月"+"\n"+ "低頭思故鄉"); } ``` //作業2 使用 MessageBox.Show() 製作一個郵遞標籤(收件人是自己的資料) 包括: 1.郵遞區號 2.地址 3.收件人 收 4.手機號碼 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// C# 產生3個空格的方法: ``` private void button4_Click(object sender, EventArgs e) { MessageBox.Show("AAA" + string.Concat(Enumerable.Repeat(" ", 3)) + "BBB"); // AAA BBB } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// string space(int n) { return string.Concat(Enumerable.Repeat(" ", n)); } private void button5_Click(object sender, EventArgs e) { MessageBox.Show("AAA"+space(10)+"BBB"); } ``` ## 第二節 ``` private void button1_Click(object sender, EventArgs e) { MessageBox.Show("AAAAA"+"\n"+"BBBBB"); MessageBox.Show("春眠不覺曉"+"\n"+"處處聞啼鳥" +"\n"+"夜來風雨聲"+"\n"+ "花落知多少"); MessageBox.Show("春眠不覺曉"+"\t\t"+"處處聞啼鳥" +"\n"+"夜來風雨聲"+"\t\t"+ "花落知多少"); // comment 注釋 // \n newline // \t tab } ``` Tab鍵(製表鍵(tabulator key)或表格鍵(tabular key)的縮寫) 用以將游標推進到下一個定位點上(適用於所有的編輯器) 作業:上例使用標籤 lablel 作為輸出, 使用"\n"換行 ``` private void button1_Click(object sender, EventArgs e) { label1.Text = ""; label1.Text="春眠不覺曉" + "\n" + "處處聞啼鳥" + "\n" + "夜來風雨聲" + "\n" + "花落知多少"; } ``` //注意! 標籤 label1.Text 不接受 定位點字元 "\t" 作業:使用 MessageBox.Show() , 製作下列報表欄位 學 號 姓 名 \------ ------ 1001 JOHN 1002 MARY 1003 TOM 1004 JERRY ///////////////////////////////////////////////////////////////////////////////////////////////////////////// ``` private void button1_Click(object sender, EventArgs e) { byte max, min; max = 255; //極值要背 行業基本常識(1 bytes) min = 0; MessageBox.Show("MAX="+max.ToString()); MessageBox.Show("MIN=" + min); } // 0000 1111 ``` ``` private void button2_Click(object sender, EventArgs e) { sbyte max, min; //signed byte 有號位元組 //極值要背 行業基本常識(1 bytes) max = 127; min = -128; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } private void button3_Click(object sender, EventArgs e) { sbyte 最大值, 最小值; // C# 可以使用中文變數名稱, 但是不建議 最大值 = 127; //極值要背 行業基本常識 最小值 = -128; MessageBox.Show("MAX=" + 最大值); MessageBox.Show("MIN=" + 最小值); } ``` ``` private void button4_Click(object sender, EventArgs e) { sbyte 最大值, 最小值; // C# 可以使用中文變數名稱, 但是不建議 最大值 = sbyte.MaxValue; //MaxValue 是屬性, 代表最大值 最小值 = sbyte.MinValue; //MinValue 是屬性, 代表最小值 MessageBox.Show("MAX=" + 最大值); MessageBox.Show("MIN=" + 最小值); } ``` private void button5_Click(object sender, EventArgs e) { short max, min; //short 有號短整數(2 bytes) 有 s (signed) max = +32767; min = -32768; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } private void button6_Click(object sender, EventArgs e) { ushort max, min; // unsigned short(2 bytes) 無號短整數 max = 65535; //極值要背 行業基本常識 min = 0; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } 作業: 求下列資料型態 byte, sbyte, short, ushort 1.最大值的總和 2.最小值的總和 3.最大值的總和+最小值的總和 //參考解答 ``` private void button1_Click(object sender, EventArgs e) { int Max, Min, total; Max= byte.MaxValue + sbyte.MaxValue + short.MaxValue + ushort.MaxValue; Min = byte.MinValue + sbyte.MinValue + short.MinValue + ushort.MinValue; total = Max + Min; MessageBox.Show("MAX=" +Max.ToString()); //98684 MessageBox.Show("MIN=" +Min.ToString()); //-32896 MessageBox.Show("TOT=" + total.ToString()); //65788 } ``` ``` private void button5_Click(object sender, EventArgs e) { int max, min; // integer max = 2147483647; //極值約+- 21億 int 是最常使用的 整數 數值型態 integer min = -2147483648; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } ``` ``` private void button6_Click(object sender, EventArgs e) { uint max, min; //unsigned integer 無號整數 max = 4294967295; //極值約 + 42億 (2進位數字 左移1位數 相當於乘 2) min = 0; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } private void button7_Click(object sender, EventArgs e) { long max, min; //long integer (8 byte) max = 9223372036854775807; min = -9223372036854775808; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } private void button8_Click(object sender, EventArgs e) { ulong max, min; //unsigned long integer max = 18446744073709551615; min = 0; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); } ``` ``` private void button8_Click(object sender, EventArgs e) { int n=1; MessageBox.Show( n.GetType().ToString()); //System.Int32 32位元 long n2 = 2; MessageBox.Show(n2.GetType().ToString() ); //System.Int64 64位元 } ``` 作業: 模仿上例, 使用GetType()顯示下列資料型態: byte, sbyte, short, ushort, int, uint, long, ulong ``` private void button9_Click(object sender, EventArgs e) { float f1, f2; //有小數點的數 稱為浮點數 (float) 數字後面要加註 f 或 F f1 = 3.14f; //注意! 數字後面加註 f 表示這個數字佔4byte f2 = -1.0F; MessageBox.Show("f1=" + f1); MessageBox.Show("f2=" + f2); } ``` ``` private void button12_Click(object sender, EventArgs e) { float f1, f2; f1 = 0.3333333333F; // only 7 digit precision f2 =- 0.5555555555F; MessageBox.Show("f1=" + f1); // 0.3333333 MessageBox.Show("f2=" + f2); //-0.5555556 4捨5入 MessageBox.Show("type=" + f2.GetType().ToString()); // System.Single } ``` ``` private void button10_Click(object sender, EventArgs e) { double d1, d2; // double 是最常使用的 浮點數 數值型態, 數字後面不用加註 d, 用起來比float方便 d1 = 3.14E0; // e 科學記號表示法 3.14*10 的0次方 d2 = -0.314e1; // e 科學記號表示法 -0.314*10 的1次方 MessageBox.Show("d1=" + d1); MessageBox.Show("d2=" + d2); MessageBox.Show("d1+d2=" + (d1+d2) ); // 0 MessageBox.Show("type=" + (d1 + d2).GetType().ToString()); // System.Double } ``` ``` private void button15_Click(object sender, EventArgs e) { double d1, d2; d1 = 3.14d; // d 可以省略 d2 = -3.14; // double 是最常使用的 浮點數 數值型態 數字後面不用加註 d MessageBox.Show("d1=" + d1); MessageBox.Show("d2=" + d2); MessageBox.Show("d1+d2=" + (d1 + d2)); } ``` ``` private void button11_Click(object sender, EventArgs e) { decimal max, min; // decimal 是有號整數型態 貨幣 (16 Byte) max = 9223372036854775807; min = -9223372036854775808; MessageBox.Show("MAX=" + max); MessageBox.Show("MIN=" + min); MessageBox.Show("type=" + max.GetType()); // System.Decimal } ``` //總結心法:在ERP領域 1.整數使用 int 2.浮點數使用 double ``` private void button12_Click(object sender, EventArgs e) { char c1, c2; // char character 字元 c1 = 'A'; c2 = 'a'; MessageBox.Show("c1=" + c1); MessageBox.Show("c2=" + c2.ToString()); MessageBox.Show("c1+c2=" + c1.ToString()+c2.ToString()); //Aa } ``` 作業:使用字元, 拼出3種水果的英文名稱 例如: Apple, Banana, Cherry 參考解答: ``` private void button1_Click(object sender, EventArgs e) { char c1,c2,c3,c4,c5; //5 字元 c1 = 'A'; c2 = 'p'; c3='p'; c4 = 'l'; c5 = 'e'; MessageBox.Show("Apple=" + c1.ToString() + c2.ToString() + c3.ToString() + c4.ToString() + c5.ToString() ); //Apple } ``` ``` private void button13_Click(object sender, EventArgs e) { MessageBox.Show(1.ToString() ); // 物件.方法 } ``` ``` private void button14_Click(object sender, EventArgs e) { string s1, s2, s3; //字串 : 字元所形成的串列 s1 = "AAA"; s2 = "BBB"; s3 = s1 + s2; MessageBox.Show(s3); } ``` ``` private void button15_Click(object sender, EventArgs e) { bool b1, b2,b3,b4; //bool boolean 布林 b1 = true; //布林變數 儲存邏輯值( true , false) b2 = false; b3 = b1 && b2; // && and b4 = b1 || b2; // | | or MessageBox.Show(b3.ToString()); //false MessageBox.Show(b4.ToString()); //true // ! not MessageBox.Show((!b1).ToString()); // !true=false MessageBox.Show((!b2).ToString()); // !false=true } ``` //下列範例是基礎練習: 能夠在表單上布置元件,進行輸入(Input) ->處理(Process) ->輸出(Output) , IPO //系統(System)的定義:具有輸入(Input) ->處理(Process) ->輸出(Output) 作業: 求 1+2 = 3 參考解答 ``` private void button1_Click(object sender, EventArgs e) { MessageBox.Show((1 + 2).ToString() ); } ``` ``` // 使用textBox1 作為輸入元件 (textBox 文字盒) private void button1_Click(object sender, EventArgs e) { string s; s = textBox1.Text; MessageBox.Show("You input :"+s); } ``` // 作業: 求n3=n1+n2 ``` private void button1_Click(object sender, EventArgs e) { int n1, n2, n3; n1 = Convert.ToInt32( textBox1.Text); n2 = Convert.ToInt32(textBox2.Text); n3=n1+n2; MessageBox.Show("n3=:"+n3); } ``` 轉換數值字串為整數的方法: 1. Convert.ToInt32() 2. Int.Parse() 作業: 仿上例,布置textBox1, textBox2, label1, label2, 使用 Int.Parse() 做轉換, 求n3=n1+n2 //心得 1.數字輸入要使用 Convert.ToInt32() 或 Int.Parse() 2.數字輸入要使用 ToString() 轉成字串 // 也可以使用 Label 元件做輸出 ``` private void button1_Click(object sender, EventArgs e) { int n1, n2, n3; n1 = Convert.ToInt32(textBox1.Text); n2 = Convert.ToInt32(textBox2.Text); n3 = n1 + n2; label3.Text="n3=" + n3.ToString(); } ``` //Convert.ToDouble 把字串數字, 轉換為 浮點數 // double.Parse把字串數字, 轉換為 浮點數 ``` private void button1_Click(object sender, EventArgs e) { double n1, n2, n3; n1 = Convert.ToDouble(textBox1.Text); n2 = Convert.ToDouble(textBox2.Text); n1 = Double.Parse(textBox1.Text); n2 = Double.Parse(textBox2.Text); n3 = n1 + n2; label3.Text = "n3=" + n3.ToString("0.0000"); //指定顯示:小數點後4位數, 小數點後的第5位數4捨5入 } ``` //心得: Int , Double 都是資料型別, 有.Parse() 可以接龍 作業1: //小數點的位數控制(使用ToString("0.00")) ``` private void button2_Click(object sender, EventArgs e) { MessageBox.Show((3.14159265359).ToString("0.00")); //3.14 MessageBox.Show((3.14159265359).ToString("0.0000")); //3.1416 } ``` 作業2: //如何去除左邊所有的0 ? ``` private void button2_Click(object sender, EventArgs e) { string s; s = 3.14159.ToString("0000.0000"); MessageBox.Show(s); //0003.1416 s = s.TrimStart('0'); MessageBox.Show(s); //3.1416 } ``` 作業1: 用3個 textBox輸入1, 2, 3, 求 1+2+3 = 6 作業2: 輸入半徑(r) 求圓的周長(2*PI*r) , 面積(PI*r*r), 圓球體積v=4/3*PI*r*r*r 作業3: 輸入矩形的長,寬 求矩形的周長( 2*(長+寬) ), 面積(長*寬) 作業4: 輸入梯形的上底,下底, 高, 求梯形的面積 (上底+下底)*高 / 2 作業5:工程師開車, 由台北公司到高雄客戶, 維修機器, 車速和行駛時間如下: (台北<->新竹)={ 60KM/H, 1H } (新竹<->台中)={ 80KM/H, 1.5H } (台中<->台南)={ 90KM/H, 1.5H } (台南<->高雄)={ 70KM/H, 1H } 請用設計表單輸入資料, 計算 工程師 行駛的總里程數 和 所花的總時間 (距離=速率*時間) 作業6:計算庫存量 庫存量=期初存量(100)+本期進貨量(1000)-本期進貨退回量(50)-本期出貨量(900)+本期出貨退回量(50) =200 作業7:計算一天總共有幾秒? 作業8:輸入一週7天的溫度,計算一週的平均溫度 { 26.5, 28.0, 27.3, 30.6 , 32.1, 33.4, 31.9 } //29.9714 作業9:輸入身高, 體重, 計算BMI BMI = 體重 (公斤) / (身高 (公尺) x 身高 (公尺)) 作業10:火星1 天有 24 小時 37 分鐘, 火星一天總共有幾秒? // 老師教 TabControl 元件 , 以利整合小作業