750 likes | 969 Views
บทที่ 3 ตอนที่ 1 คำสั่งเงื่อนไขและการตัดสินใจ (p.2-26) ตอนที่ 2 Standard Windows Forms Controls (27-74). คำสั่งเงื่อนไขและการตัดสินใจ. ในการทำงานของโปรแกรมทั่วๆไปจะมีการทำงาน 3 ประเภทหลัก การทำงานเป็นลำดับ (sequence) ก่อนและหลัง การตัดสินใจ( decision) มีเงื่อนไขให้ตรวจสอบการทำงาน
E N D
บทที่ 3ตอนที่ 1คำสั่งเงื่อนไขและการตัดสินใจ(p.2-26)ตอนที่ 2 Standard Windows Forms Controls(27-74)
คำสั่งเงื่อนไขและการตัดสินใจคำสั่งเงื่อนไขและการตัดสินใจ • ในการทำงานของโปรแกรมทั่วๆไปจะมีการทำงาน3 ประเภทหลัก • การทำงานเป็นลำดับ (sequence) ก่อนและหลัง • การตัดสินใจ(decision) มีเงื่อนไขให้ตรวจสอบการทำงาน • การทำงานที่ต้องทำซ้ำ ๆ หลายครั้งหรือการวนลูป(loop)
คำสั่งเงื่อนไขและการตัดสินใจคำสั่งเงื่อนไขและการตัดสินใจ • คำสั่งที่ใช้ในการตัดสินใจเลือก ถือเป็นคำสั่งควบคุมชนิดหนึ่ง • ภาษาโปรแกรมโดยทั่วไปจะมี 2 คำสั่ง คือ • if – statement • case หรือ switch
คำสั่ง IF • if เป็นการเปรียบเทียบเงื่อนไขว่าตรงตามที่เราต้องการหรือไม่ หากตรงก็จะทำตามคำสั่งที่กำหนด แต่หากไม่ตรงก็จะข้ามคำสั่งส่วนนั้นไป • กรณี การเขียนเงื่อนไขเดียว และกระทำกรณีเป็นจริง รูปแบบคำสั่ง • ตรวจสอบเงื่อนไข ถ้าเป็นจริงจะทำงานตาม statement if ( condition ) { statement (s) }
ตัวอย่าง การเขียนเงื่อนไขเดียว • if (x==y) { Console.Write("x equals to y"); } • if ((x>0)&& (y<>0)) { Z=x/y; }
คำสั่ง IF • หากหลัง if มีเพียง statement เดียวไม่จำเป็นต้องเขียน {} ครอบคำสั่งก็ได้เช่น • if (x<0) str strText = "It’s Negative“; • if (x>=0) str strText = "It’s Positive” ;
if-else statement if (expression ที่ทดสอบเงื่อนไข) { // ชุดคำสั่งเมื่อเงื่อนไขเป็นจริง } else { // ชุดคำสั่งเมื่อเงื่อนไขเป็นเท็จ }
การใช้ if-else แบบซ้อนกัน if (expression ที่ทดสอบเงื่อนไขที่ 1) { If (expression ที่ทดสอบเงื่อนไขที่ 2) { (ชุดคำสั่งเมื่อเงื่อนไขที่ 2 เป็นจริง) } else { (ชุดคำสั่งเมื่อเงื่อนไขที่ 2 เป็นเท็จ) } else { (expression ที่ทดสอบเงื่อนไขที่ 1) }
if (condition1) { … } else if (condition2) { … } else { … } else if & Nested if statement ตัวอย่าง char Result; if (Score > 80) Result = ‘A’; else if (Score > 50) Result = ‘B’; else Result = ‘F’;
if (condition1) { … if (condition2) { … if (condition3) { … } } } x=10; y=20 if (x>5) { if (y>30) { z=x*y; } else { z=x+y; } } else if & Nested if statement
โปรแกรมสำหรับคำนวณเกรดโปรแกรมสำหรับคำนวณเกรด if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else if (score >= 60) { grade = 'D'; } else { grade = 'F'; }
คำสั่ง switch..case • ใช้ในการตัดสินใจเลือกมากกว่า 2 ทางเลือก • จะทำการตรวจสอบเงื่อนไขในการเลือกก่อนว่าจะตรงกับตัวเลือกใด แล้วจึงทำงานตามคำสั่งที่ต่อท้ายตัวเลือกนั้น
คำสั่ง switch..case switch (variable) { case ค่าที่1 ของตัวแปร:// เงื่อนไขแรก statement1; // ทำงานตามเงื่อนไขแรก case ค่าที่2 ของตัวแปร:// เงื่อนไขที่สอง statement2; // ทำงานตามเงื่อนไขที่สอง … … case สุดท้าย:// เงื่อนไขที่สอง statement; // ทำงานตามเงื่อนไขสุดท้าย default : statementN; // เมื่อไม่ตรงกับเงื่อนไขใดๆ เลย }
ภาพแสดงลักษณะการตรวจสอบเงื่อนไข ของ switch-case
string str, color; Console.Write("Enter day of week:"); str = Console.ReadLine(); switch (str) { case "Sunday" : color = "red"; break; case "Monday" : color="yellow"; break; ... default : color="invalid"; break; } Console.WriteLine("color of " + str.ToUpper() + " is " + color.ToUpper()); Console.Read (); switch (expression) { case value1 :// เงื่อนไขแรก statement1; // คำสั่ง break; case value2 : statement2; break; case valueN : statementN; break; default : statement; break; } expression: ตัวเลข or string switch statement • string str, color; • Console.Write("Enter day of week:"); • str = Console.ReadLine(); • switch (str) { • case "Sunday" : color = "red"; • break; • case “monday" : • case "Monday" : color="yellow"; • break; • ... • default : color="invalid"; • break; • } • //แสดงผลบน lblResult
switch statement class DateDemo { public static void Main () { char d; Console.Write(“ Input number date 1 – 7 : “); d = (char) Console.Read(); Console.Write(“ Input value : “ + d+” = Date of week = “); Switch (d) { case ‘1’ : Console.WriteLine(“Sunday.”); break; case ‘2’ : Console.WriteLine(“Monday.”); break; case ‘3’ : Console.WriteLine(“Tuesday.”); break; case ‘4’ : Console.WriteLine(“Wednesday.”); break; case ‘5’ : Console.WriteLine(“Thursday.”); break; case ‘6’ : Console.WriteLine(“Friday.”); break; case ‘7’ : Console.WriteLine(“Saturday.”); break; default : Console.WriteLine(“Invalid number”); break; } }}
ข้อควรระวังในการใช้ switch คือ • ค่าของตัวแปรหรือนิพจน์ต้องเป็นเลขจำนวนเต็มหรือ char, byte เท่านั้น ไม่สามารถใช้แบบเป็นช่วง • ชนิดข้อมูลของตัวแปร จะต้องเป็นประเภทเดียวกับค่าข้อมูลที่กำหนดหลัง case • คำสั่ง switch จะประกอบด้วยคำสั่ง break หลังการทำงาน เพื่อควบคุมการทำงานของโปรแกรม ให้ทำงานเฉพาะกรณีผลการตรวจสอบเป็นจริง เพื่อลดเวลาในการประมวลผล แต่สามารถไม่ใส่คำสั่งนี้ได้ หากต้องการให้ทำงานต่อเนื่องทุก case
ในทางปฏิบัติจะเห็นว่าคำสั่ง switch และคำสั่ง if ให้ผลการทำงานเกือบเหมือนกัน • ผู้ใช้สามารถเลือกเขียนด้วยชุดคำสั่งใดก็ได้ • โดยทั่วไปคำสั่ง if จะครอบคลุมเงื่อนไขในการทดสอบได้มากกว่า เช่นสามารถทดสอบเป็นช่วงได้ สามารถทดสอบจำนวนจริงได้ แต่คำสั่ง switch จะช่วยให้โปรแกรมดูง่ายและสะดวกในการเขียนหรือแก้ไขโปรแกรม
for statement เป็นการทำงานซ้ำตามจำนวนครั้งที่กำหนด โดยเริ่มจากค่าแรกไปจนถึงค่าสุดท้าย รูปแบบคำสั่ง for (counter = first value; counter condition; adjust counter value) { … } เช่น for (i=0;i<=10;i++) { … } หมายถึง ให้วนลูปเริ่มตั้งแต่ค่า i=0 ไปเรื่อยๆ ตราบใดที่ค่า i ยังไม่เกิน 10 และให้เพิ่มค่า i ทีละ 1
Nest for…Loop • สามารถใช้การวนลูปซ้อนกันกี่ชั้นก็ได้ for (…) { … for (…) { … for (…) { … }}}
ตัวอย่าง for statement (2) • int num; string s; • Console.Write ("Please Enter number : "); • s=Console.ReadLine( ); • num=int.Parse(s); • for (int i=1; i<=num; i++){ • for (int j=1;j<=i;j++){ • Console.Write(j+" "); • } // end for j • Console.WriteLine(" "); • } // end for i • Console.Read( );
While • ในการใช้งาน for Loop จำเป็นต้องระบุค่าเริ่มต้นและค่าสุดท้ายของลูป • แต่ในกรณีที่ไม่ทราบค่าดังกล่าว แต่ทราบเงื่อนไขบางอย่าง สามารถใช้ลูป while แทนได้
while statementโครงสร้างของลูป while while (condition) { ….statement…. } ตัวอย่าง • int Result = 0; int i = 0; • while (i < 2) { • i += 1; • Result += i; • } // end while • Console.Write (Result + " " + i); // Result=____ i=____
do-while statement do { … } while (condition); ตัวอย่าง • int Result = 0; int i = 0; • do { • i += 1; • Result += i; • } while (i < 2); • Console.Write (Result + " " + i); // Result=____ i=____ while (condition) { … }
แบบฝึกหัด for & while statement (4) • พี่มอสมีแฟนคลับ 2,857 คน โดยแต่ละปีจะมีแฟนคลับเพิ่มขึ้นปีละ 8% จงเขียนโปรแกรมคำนวณว่าจะใช้เวลากี่ปี พี่มอสจึงจะมีแฟนคลับมากกว่า 5,000 คน
การสร้าง Event-Driven Programming • แนวคิดของการเขียนโปรแกรม คือต้องสามารถตอบสนองต่อเหตุการณ์(Event-handling) ที่เกิดขึ้น เช่น การคลิก(Click) การดับเบิ้ลคลิก(DoubleClick) การกดแป้นพิมพ์(KeyDown, KeyPress, KeyUp) การเคลื่อนที่ของเมาส์(MouseMove, MouseUp, MouseDown) และเหตุการณ์อื่นๆ
รูปแบบของ Event-Driven Programming private void ObjName_Event(object sender, EventArgs e) { . . . } ObjName :ชื่อของวัตถุที่เราต้องการควบคุมเหตุการณ์ Event : เหตุการณ์ที่เกิดขึ้นกับวัตถุ sender: ตัวแปรที่ส่งไปแทนวัตถุที่เกิดเหตุการณ์ขึ้น e: ตัวแปรที่ส่งไปแทนเหตุที่เกิดขึ้นกับวัตถุ private void textBox1_TextChanged(object sender, EventArgs e){ }
Form • อยู่ภายใต้ namespace : System.Windows.Forms.Form • ใช้เป็นcontainer สำหรับนำ controls อื่นๆมาวางเพื่อสร้างGUI (Graphic User Interface)
Text Font Opacity TopMost AcceptButton CancelButton AutoScroll Icon BackColor Enabled WindowState StartPosition Location Size MaximumSize MinimumSize Form Properties
Form Events • Activated and Deactivate • Load • Closing • Closed • Resize • Move • etc.
Form Event : Closing() • privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) • { • if(MessageBox.Show(“Close Application?", "Closing Event", MessageBoxButtons.YesNo) == DialogResult.Yes) • { • this.Dispose(); // ปิดform ตัวเอง • // Application.Exit(); // จบการทำงานของapplication • } • else • { • // Cancel the Closing event from closing the form. • e.Cancel = true; • } • }
การกำหนดControl Tab Order • เลือกView menu -> Tab Order • กำหนดลำดับtab ให้กับcontrol ต่างๆ • เลือกView menu -> Tab Order อีกครั้ง
Windows Forms Inheritance • เลือกAdd -> Add Inherited Form… • กำหนดชื่อform ใหม่ • เลือกform ต้นแบบ • การปรับปรุงที่form ต้นแบบส่งผลถึงform ใหม่ที่inherit มา
TextBox • อยู่ภายใต้ namespace : System.Windows.Forms.TextBox • สำหรับแสดงผลข้อความหรือรับข้อความจากคีย์บอร์ดขณะรันโปรแกรม
Text TextAlign Visible Font BackColor ForeColor MultiLine ScrollBar BorderStyle Enable ReadOnly PasswordChar MaxLength Anchor AutoSize SelectionStart SelectionLength TextLength TextBox Properties
TextBox Method • Focus () กำหนดcursorให้อยู่ที่TextBox • SelectAll () เลือกข้อความทั้งหมดภายใน TextBox • Clear () ลบข้อความในTextBox • Hide () ซ่อนTextBox • Show () แสดงTextBox
TextBox Events • TextChanged เกิดขึ้นเมื่อข้อความบน TextBox เปลี่ยนแปลง • KeyPress เกิดขึ้นเมื่อผู้ใช้กดตัวอักษรบนคีย์บอร์ด • etc.
การจัดการข้อความผ่านTextBoxการจัดการข้อความผ่านTextBox • กำหนดข้อความให้แสดงบนTextBox • ดึงข้อความบนTextBox มาใช้งาน textBox1.Text = "ข้อความ"; string str = textBox1.Text;
การcoding เพื่อกำหนดProperties • void myMethod() { • textBox1.Text = "123"; • textBox1.BackColor = Color.Red; • textBox1.Font=new Font("Tahoma",16,FontStyle.Bold); • }
Label • อยู่ภายใต้ namespace : System.Windows.Forms.Label • สำหรับแสดงข้อความคล้ายTextBox แต่ผู้ใช้ไม่สามารถแก้ไขหรือคัดลอกข้อความได้ (read only)
Button • อยู่ภายใต้ namespace : System.Windows.Forms.Button • เป็นcontrol พื้นฐานที่มีใช้งานสำหรับกดเพื่อทำงานบางอย่าง
Button Properties • Text ข้อความที่แสดงบนButton • Image รูปภาพที่แสดงบนButton • Anchor กำหนดตำแหน่งButtonตรึงบนForm
Button Events • Click เกิดขึ้นเมื่อclick ที่ปุ่ม • MouseHover เกิดขึ้นเมื่อนำเมาส์มาวางเหนือปุ่ม
แบบฝึกหัด(1) • จงเขียนโปรแกรมสำหรับคำนวณเกรดโดยรับคะแนนจากผู้ใช้โดยกำหนดว่า
แบบฝึกหัด(2) • จงเขียนโปรแกรมสำหรับสร้างเครื่องคิดเลขให้สามารถ +, -, *, / ได้โดยรับค่าจากผู้ใช้และแสดงค่าผ่านtextBox