136
1 LAB REPORT CSHAPE 1 Student:Nguyễn Văn Tùng ID:09210071 Class:CDTH11

Lab Report

Embed Size (px)

Citation preview

Page 1: Lab Report

1

LAB REPORT

CSHAPE 1

Student:Nguyễn Văn Tùng

ID:09210071

Class:CDTH11

Page 2: Lab Report

2

Contents

MODULE 1 ................................................................................................................... 9

EXERCISE 1 .................................................................................................................. 9

Problem .................................................................................................................... 9

Code Listing ............................................................................................................. 9

Execution Result ...................................................................................................... 9

EXERCISE 2 ................................................................................................................ 10

Problem .................................................................................................................. 10

Code Listing ........................................................................................................... 10

Execution Result .................................................................................................... 10

EXERCISE 3 ................................................................................................................ 11

Problem .................................................................................................................. 11

Code Listing ........................................................................................................... 11

Execution Result .................................................................................................... 12

EXERCISE 4 ................................................................................................................ 12

Problem .................................................................................................................. 12

Code Listing ........................................................................................................... 12

Execution Result .................................................................................................... 13

EXERCISE 5 ................................................................................................................ 13

Problem .................................................................................................................. 13

Code Listing ........................................................................................................... 13

Execution Result .................................................................................................... 14

EXERCISE 6 ................................................................................................................ 15

Problem .................................................................................................................. 15

Code Listing ........................................................................................................... 15

Execution Result .................................................................................................... 17

EXERCISE 7 ................................................................................................................ 17

Problem .................................................................................................................. 17

Code Listing ........................................................................................................... 17

Execution Result .................................................................................................... 18

EXERCISE 8 ................................................................................................................ 18

Problem .................................................................................................................. 18

Code Listing ........................................................................................................... 18

Execution Result .................................................................................................... 20

EXERCISE 9 ................................................................................................................ 20

Page 3: Lab Report

3

Problem .................................................................................................................. 20

Code Listing ........................................................................................................... 20

Execution Result .................................................................................................... 22

EXERCISE 10 .............................................................................................................. 22

Problem .................................................................................................................. 22

Code Listing ........................................................................................................... 23

Execution Result .................................................................................................... 23

MODULE 2 ................................................................................................................. 24

EXERCISE 1 ................................................................................................................ 24

Problem .................................................................................................................. 24

Code Listing ........................................................................................................... 24

Execution Result .................................................................................................... 25

EXERCISE 2 ................................................................................................................ 26

Problem .................................................................................................................. 26

Code Listing ........................................................................................................... 26

Execution Result .................................................................................................... 27

EXERCISE 3 ................................................................................................................ 27

Problem .................................................................................................................. 27

Code Listing ........................................................................................................... 27

Execution Result .................................................................................................... 29

EXERCISE 5 ................................................................................................................ 31

Problem .................................................................................................................. 31

Code Listing ........................................................................................................... 31

Execution Result .................................................................................................... 33

EXERCISE 6 ................................................................................................................ 34

Problem .................................................................................................................. 34

Code Listing ........................................................................................................... 34

Execution Result .................................................................................................... 35

EXERCISE 7 ................................................................................................................ 35

Problem .................................................................................................................. 35

Code Listing ........................................................................................................... 36

Execution Result .................................................................................................... 37

EXERCISE 8 ................................................................................................................ 37

Problem .................................................................................................................. 37

Code Listing ........................................................................................................... 37

Execution Result .................................................................................................... 39

EXERCISE 9 ................................................................................................................ 39

Problem .................................................................................................................. 39

Code Listing ........................................................................................................... 39

Page 4: Lab Report

4

Execution Result .................................................................................................... 41

EXERCISE 10 .............................................................................................................. 41

Problem .................................................................................................................. 41

Code Listing ........................................................................................................... 41

Execution Result .................................................................................................... 42

EXERCISE 11 .............................................................................................................. 42

Problem .................................................................................................................. 42

Code Listing ........................................................................................................... 42

Execution Result .................................................................................................... 44

EXERCISE 12 .............................................................................................................. 44

Problem .................................................................................................................. 44

Code Listing ........................................................................................................... 44

Execution Result .................................................................................................... 45

EXERCISE 13 .............................................................................................................. 46

Problem .................................................................................................................. 46

Code Listing ........................................................................................................... 46

Execution Result .................................................................................................... 48

EXERCISE 14 .............................................................................................................. 49

Problem .................................................................................................................. 49

Code Listing ........................................................................................................... 49

Execution Result .................................................................................................... 51

EXERCISE 15 .............................................................................................................. 52

Problem .................................................................................................................. 52

Code Listing ........................................................................................................... 52

Execution Result .................................................................................................... 54

EXERCISE 16 .............................................................................................................. 55

Problem .................................................................................................................. 55

Code Listing ........................................................................................................... 55

Execution Result .................................................................................................... 56

EXERCISE 18 .............................................................................................................. 56

Problem .................................................................................................................. 56

Code Listing ........................................................................................................... 56

Execution Result .................................................................................................... 58

EXERCISE 19 .............................................................................................................. 60

Problem .................................................................................................................. 60

Code Listing ........................................................................................................... 60

Execution Result .................................................................................................... 60

EXERCISE 20 .............................................................................................................. 60

Problem .................................................................................................................. 60

Code Listing ........................................................................................................... 61

Page 5: Lab Report

5

Execution Result .................................................................................................... 61

EXERCISE 21 .............................................................................................................. 61

Problem .................................................................................................................. 61

Code Listing ........................................................................................................... 61

Execution Result .................................................................................................... 61

EXERCISE 22 .............................................................................................................. 61

Problem .................................................................................................................. 61

Code Listing ........................................................................................................... 61

Execution Result .................................................................................................... 61

MODULE 3 ................................................................................................................. 62

EXERCISE 1 ................................................................................................................ 62

Problem .................................................................................................................. 62

Code Listing ........................................................................................................... 62

Execution Result .................................................................................................... 64

EXERCISE 2 ................................................................................................................ 64

Problem .................................................................................................................. 64

Code Listing ........................................................................................................... 64

Execution Result .................................................................................................... 66

EXERCISE 3 ................................................................................................................ 66

Problem .................................................................................................................. 66

Code Listing ........................................................................................................... 66

Execution Result .................................................................................................... 67

EXERCISE 4 ................................................................................................................ 68

Problem .................................................................................................................. 68

Code Listing ........................................................................................................... 68

Execution Result .................................................................................................... 71

EXERCISE 5 ................................................................................................................ 71

Problem .................................................................................................................. 71

Code Listing ........................................................................................................... 71

Execution Result .................................................................................................... 74

EXERCISE 6 ................................................................................................................ 75

Problem .................................................................................................................. 75

Code Listing ........................................................................................................... 75

Execution Result .................................................................................................... 78

EXERCISE 7 ................................................................................................................ 79

Problem .................................................................................................................. 79

Code Listing ........................................................................................................... 79

Execution Result .................................................................................................... 81

EXERCISE 8 ................................................................................................................ 81

Page 6: Lab Report

6

Problem .................................................................................................................. 81

Code Listing ........................................................................................................... 81

Execution Result .................................................................................................... 83

EXERCISE 9 ................................................................................................................ 85

Problem .................................................................................................................. 85

Code Listing ........................................................................................................... 85

Execution Result .................................................................................................... 86

EXERCISE 10 .............................................................................................................. 87

Problem .................................................................................................................. 87

Code Listing ........................................................................................................... 87

Execution Result .................................................................................................... 89

EXERCISE 11 .............................................................................................................. 90

Problem .................................................................................................................. 90

Code Listing ........................................................................................................... 91

Execution Result .................................................................................................... 91

EXERCISE 12 .............................................................................................................. 91

Problem .................................................................................................................. 91

Code Listing ........................................................................................................... 92

Execution Result .................................................................................................... 92

MODULE 4 ................................................................................................................. 92

EXERCISE 1 ................................................................................................................ 92

Problem .................................................................................................................. 92

Code Listing ........................................................................................................... 92

Execution Result .................................................................................................... 95

EXERCISE 2 ................................................................................................................ 97

Problem .................................................................................................................. 97

Code Listing ........................................................................................................... 97

Execution Result .................................................................................................... 97

EXERCISE 3 ................................................................................................................ 97

Problem .................................................................................................................. 97

Code Listing ........................................................................................................... 97

Execution Result .................................................................................................... 98

EXERCISE 4 ................................................................................................................ 98

Problem .................................................................................................................. 98

Code Listing ........................................................................................................... 98

Execution Result .................................................................................................... 98

EXERCISE 5 ................................................................................................................ 98

Problem .................................................................................................................. 98

Code Listing ........................................................................................................... 98

Page 7: Lab Report

7

Execution Result .................................................................................................... 98

EXERCISE 6 ................................................................................................................ 99

Problem .................................................................................................................. 99

Code Listing ........................................................................................................... 99

Execution Result .................................................................................................... 99

MODULE 5 ................................................................................................................. 99

EXERCISE 1 ................................................................................................................ 99

Problem .................................................................................................................. 99

Code Listing ........................................................................................................... 99

Execution Result .................................................................................................. 100

EXERCISE 2 .............................................................................................................. 100

Problem ................................................................................................................ 100

Code Listing ......................................................................................................... 100

Execution Result .................................................................................................. 100

EXERCISE 3 .............................................................................................................. 100

Problem ................................................................................................................ 100

Code Listing ......................................................................................................... 100

Execution Result .................................................................................................. 104

EXERCISE 4 .............................................................................................................. 107

Problem ................................................................................................................ 107

Code Listing ......................................................................................................... 108

Execution Result .................................................................................................. 108

EXERCISE 5 .............................................................................................................. 108

Problem ................................................................................................................ 108

Code Listing ......................................................................................................... 108

Execution Result .................................................................................................. 111

MODULE 6 ............................................................................................................... 112

EXERCISE 1 .............................................................................................................. 112

Problem ................................................................................................................ 112

Code Listing ......................................................................................................... 112

Execution Result .................................................................................................. 117

EXERCISE 2 .............................................................................................................. 118

Problem ................................................................................................................ 118

Code Listing ......................................................................................................... 118

Execution Result .................................................................................................. 122

MODULE 7 ............................................................................................................... 123

EXERCISE 1 .............................................................................................................. 123

Page 8: Lab Report

8

Problem ................................................................................................................ 123

Code Listing ......................................................................................................... 124

Execution Result .................................................................................................. 126

EXERCISE 2 .............................................................................................................. 130

Problem ................................................................................................................ 130

Code Listing ......................................................................................................... 130

Execution Result .................................................................................................. 130

EXERCISE 3 .............................................................................................................. 130

Problem ................................................................................................................ 130

Code Listing ......................................................................................................... 130

Execution Result .................................................................................................. 131

EXERCISE 4 .............................................................................................................. 132

Problem ................................................................................................................ 132

Code Listing ......................................................................................................... 132

Execution Result .................................................................................................. 134

EXERCISE 5 .............................................................................................................. 134

Problem ................................................................................................................ 134

Code Listing ......................................................................................................... 134

Execution Result .................................................................................................. 136

Page 9: Lab Report

9

Module 1

Exercise 1

Problem

Mục đích: Sử dụng các properties và methods của các đối tượng trong ứng dụng Console. Yêu cầu: Viết ứng dụng dạng Console xuất lời chào. Yêu cầu xuất ra lời chào sau: Welcome to C# 2010 Programming! Programming by your name

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai01 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to"); Console.WriteLine("C# 2010 Programming!"); Console.WriteLine("Programming by Tung"); Console.ReadLine(); } } }

Execution Result

Page 10: Lab Report

10

Exercise 2

Problem

Mục đích: Sử dụng các Properties và Methods của các đối tượng trong ứng dụng Console Kết hợp định dạng xuất dữ liệu. Yêu cầu:

+ Rào các statement trong hàm Main của bài 1 chuyển sang dạng comment.

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai02 { class Program { static void Main(string[] args) { Console.WriteLine("{0}\n{1}","Welcome to","C# 2010 Programming!"); Console.WriteLine("Programming by Tung"); Console.ReadLine(); } } }

Execution Result

Page 11: Lab Report

11

Exercise 3

Problem

Mục đích: Sử dụng một số hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu. Yêu cầu: + Tạo Project mới với tên là ThaoTacSo: Khi chạy chư ng t nh xuất ra yêu cầu nhập vào hai số từ bàn phím xuất ra kết quả tổng của hai số vừa nhập. Xuất ra mà h nh yêu cầu có dạng: Nhap So Thu Nhat A: ?? Nhap So Thu Hai B: ?? Tong cua ?? Va ?? La: ??

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai03 { class Program { static void Main(string[] args) { int a, b, nTong = 0; Console.Clear(); Console.Write("Nhap So Thu Nhat A: "); a= Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap So Thu Hai B: "); b = Convert.ToInt32(Console.ReadLine()); nTong = a + b; Console.WriteLine("Tong Cua "+a+" Va "+b+" La "+nTong); Console.WriteLine("Programming by Tung"); Console.ReadLine(); } } }

Page 12: Lab Report

12

Execution Result

Exercise 4

Problem

Mục đích: Sử dụng một số hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu. Yêu cầu: + Tạo Project mới với tên là TinhDienTich: + Khi chạy chư ng t nh xuất ra yêu cầu nhập vào hai cạnh của một tứ giácvuông xuất ra kết quả chu vi, diện tích….

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai04 { class Program { static void Main(string[] args) { int dai, rong, chuvi = 0,dt=0; Console.Clear(); Console.WriteLine("==========Tinh Chu Vi & Dien Tich HCN=========== "); Console.Write("Nhap Chieu Dai: "); dai = Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap Chieu Rong: "); rong = Convert.ToInt32(Console.ReadLine()); chuvi = (dai + rong) * 2; dt = dai * rong; Console.WriteLine("Chu Vi = "+chuvi+"\nDien Tich = "+dt); Console.WriteLine("\nProgramming by Tung"); Console.ReadLine(); } } }

Page 13: Lab Report

13

Execution Result

Exercise 5

Problem

Mục đích: Sử dụng một số hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu. Yêu cầu:

+ Tạo Project mới với tên là PhuongTrinhBac1: + Giải và biện luận phư n g t nh bậc 1 : ax+b=0 + Sinh viên chú ý rằng phư ng t nh có 3 trường hợp: vô nghiệm, vô số nghiệm và có một

nghiệm

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai05 { class Program { static void Main(string[] args) { int a, b; float x; Console.Clear(); Console.WriteLine("==========Giai Va Bien Luan Phuong Trinh Bac Nhat=========== "); Console.Write("Nhap a: "); a = Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap b: "); b = Convert.ToInt32(Console.ReadLine()); GiaiPTBacNhat(a, b); //Console.WriteLine(""); Console.WriteLine("\nProgramming by Tung");

Page 14: Lab Report

14

Console.ReadLine(); } public static void GiaiPTBacNhat(int a, int b) { double x; if (a == 0) { if (b == 0) { Console.WriteLine("Phuong Trinh Vo So Nghiem"); } else { Console.WriteLine("Phuong Trinh Vo Nghiem"); } } else { x = -b*1.0 / a; Console.WriteLine("Phuong Trinh Co 1 Nghiem = " + x); } } } }

Execution Result

Page 15: Lab Report

15

Exercise 6

Problem

Mục đích: Sử dụng một số hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu.( làm quen class Math) Math.Pow(x,y) ,Math.Sqrt(x) ,Math.Round(x,2) Yêu cầu:

+ Tạo Project mới với tên là PhuongTrinhBac2: + Giải và biện luận phư n g t nh bậc 2

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai06 { class Program { static void Main(string[] args) { int a=0, b=0,c=0; double x1,x2; Console.Clear(); Console.WriteLine("==========Giai Va Bien Luan Phuong Trinh Bac Hai=========== "); Console.Write("Nhap a: "); a = Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap b: "); b = Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap c: "); c = Convert.ToInt32(Console.ReadLine()); if (a == 0) { GiaiPTBacNhat(a, b);

Page 16: Lab Report

16

} else { double delta = b*b-4*a*c; if (delta < 0) { Console.WriteLine("Phuong Trinh Vo Nghiem"); } else if (delta == 0) { x1 = -b * 1.0 / (2*a); Console.WriteLine("Phuong Trinh Co Nghiem Kep \nx1 = x2 = " + x1); } else { x1 = (-b-Math.Sqrt(delta))/(2*a); x2 = (-b + Math.Sqrt(delta)) / (2 * a); Console.WriteLine("Phuong Trinh Co 2 Nghiem Phan Biet \nx1 = "+x1+"\nx2 = "+x2); } } Console.WriteLine("\nProgramming by Tung"); Console.ReadLine(); } public static void GiaiPTBacNhat(int a, int b) { double x; if (a == 0) { if (b == 0) { Console.WriteLine("Phuong Trinh Vo So Nghiem"); } else { Console.WriteLine("Phuong Trinh Vo Nghiem"); } } else { x = -b * 1.0 / a; Console.WriteLine("Phuong Trinh Co 1 Nghiem = " + x); } } } }

Page 17: Lab Report

17

Execution Result

Exercise 7

Problem

Mục đích: tiếp tục làm quen với class Math Yêu cầu: + Nhập 1 góc x. Tính và xuất sinx, cosx, tgx, cotgx.

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai07 { class Program { static void Main(string[] args) { double a = 0, rad;

Page 18: Lab Report

18

Console.Clear(); Console.WriteLine("==========Cac Ham Luong Giac=========== "); Console.Write("Nhap Vao 1 Goc: "); a = Convert.ToInt32(Console.ReadLine()); rad = a * Math.PI / 180; Console.Write("Sin({0}) = {1}", a, Math.Sin(rad)); Console.Write("\nCos({0}) = {1}", a, Math.Cos(rad)); Console.Write("\nTan({0}) = {1}", a, Math.Tan(rad)); Console.Write("\nCogTan({0}) = {1}", a, 1 / Math.Tan(rad)); Console.WriteLine("\n\nProgramming by Tung"); Console.ReadLine(); } } }

Execution Result

Exercise 8

Problem

Mục đích: Sử dụng một số hàm lượng giác và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu.( tiếp tục làm quen với class Math) Yêu cầu: + Tạo Project mới với tên là ChuViDienTichTamGiac: +Nhập cạnh a>0, b>0, c>0. Nếu a, b, c tạo thành tam giác th h y tính và xuất chu vi, diện tích h nh tam giác. Ngược lại thông báo “Không tạo thành tam giác” HD: cv=a+b+c, p=cv/2 và dt=√p* (p-a) (p-b) (p-c)

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text;

Page 19: Lab Report

19

namespace Bai08 { class Program { static void Main(string[] args) { double a,b,c; Console.Clear(); Console.WriteLine("==========Tinh Chu Vi Va Dien Tich Tam Giac=========== "); a = Input('a'); b = Input('b'); c = Input('c'); if (!KiemTraTamGiac(a,b,c)) { Console.WriteLine("\nKhong Tao Thanh Tam Giac"); } else { Console.WriteLine("\nChu Vi = {0}",a+b+c); double p = (a + b + c) / 2; Console.WriteLine("\nDien Tich = {0}", Math.Sqrt(p*(p-a)*(p-b)*(p-c))); } Console.WriteLine("\n\nProgramming by Tung"); Console.ReadLine(); } public static double Input(char bien) { double x; do { Console.Write("\nNhap {0}: ",bien); x = Convert.ToDouble(Console.ReadLine()); } while (x <= 0); return x; } public static bool KiemTraTamGiac(double a, double b, double c) { if (a + b <= c || a + c <= b || b + c <= a) return false; else return true; } } }

Page 20: Lab Report

20

Execution Result

Exercise 9

Problem

Mục đích: Sử dụng hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu. Yêu cầu: + Tạo Project mới với tên là TinhTongChuoiSo: + Nhập x, n để tính tổng chuỗi số sau (làm

tròn 3 chữ số): S(x,n) = x+x2/2! + x3/3! + … + xn/n!

Code Listing

using System;

Page 21: Lab Report

21

using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai09 { class Program { static void Main(string[] args) { int x, n; double Tong = 0; Console.Clear(); Console.WriteLine("==========Tinh Tong Chuoi So=========== "); x = Input('x'); n = Input('n'); for (int i = 1; i <= n; i++) { Tong += (Math.Pow(x, i) / GiaiThua(i)); } Console.WriteLine("\nS({0},{1}) = {2}", x, n, Math.Round(Tong, 3)); Console.WriteLine("\n\nProgramming by Tung"); Console.ReadLine(); } public static int Input(char bien) { int x; do { Console.Write("\nNhap {0}: ", bien); x = Convert.ToInt32(Console.ReadLine()); } while (x <= 0); return x; } public static int GiaiThua(int x) { int n=1; for (int i = 1; i <= x; i++) { n *= i; } return n; } } }

Page 22: Lab Report

22

Execution Result

Exercise 10

Problem

Mục đích: Sử dụng hàm toán học và nhập dữ liệu từ bàn phím Kết hợp định dạng xuất dữ liệu. Yêu cầu:

+ Tạo Project mới với tên là SoGaSoCho: + Tìm số gà và số chó, biết:

Vừa gà vừa chó Bó lại cho tròn

Page 23: Lab Report

23

Ba mư i sáu con Một tr m chân chẵn Gợi ý:

X + Y = 36

2𝑋 + 4𝑌 = 100

Với X là số gà, Y là số Chó

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai10 { class Program { static void Main(string[] args) { int ga, cho; Console.Clear(); Console.WriteLine("============================== "); Console.WriteLine("Vua Ga Vua Cho"); Console.WriteLine("Bo Lai Cho Tron"); Console.WriteLine("Ba Muoi Sau Con"); Console.WriteLine("Mot Tram Chan Chan"); Console.WriteLine("============================== "); //100 = 72-2*cho+4*cho = 72 +2*cho cho = (100 - 72) / 2; ga = 36 - cho; Console.WriteLine("So Ga = "+ga); Console.WriteLine("So Cho ="+cho); Console.WriteLine("\n\nProgramming by Tung"); Console.ReadLine(); } } }

Execution Result

Page 24: Lab Report

24

Module 2

Exercise 1

Problem

Yêu Cầu Thiết kế form như hình

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

Page 25: Lab Report

25

using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnHienThi_Click(object sender, EventArgs e) { txtHienThi.Text = txtNhapPass.Text; } private void btnTiep_Click(object sender, EventArgs e) { txtHienThi.Text = ""; txtNhapPass.Clear(); txtNhapPass.Focus(); } private void btnDong_Click(object sender, EventArgs e) { this.Close(); } } }

Execution Result

Page 26: Lab Report

26

Exercise 2

Problem

Mục Đích: Sử dụng các sự kiện (event) c bản trên Form Yêu Cầu Thiết kế form chỉ chứa 1 button (xem hình)

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("Hi,Welcome to C# Programming!"); } private void btnClickMe_Click(object sender, EventArgs e) { MessageBox.Show("Button was clicked"); } private void Form1_Click(object sender, EventArgs e) { MessageBox.Show("Form was clicked"); } } }

Page 27: Lab Report

27

Execution Result

Exercise 3

Problem

Thiết kế giao diện như hình

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

Page 28: Lab Report

28

using System.Windows.Forms; namespace Bai03 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button4_Click(object sender, EventArgs e) { this.Close(); } private void btnHoLot_Click(object sender, EventArgs e) { lblHoTen.Text = txtHo.Text; } private void btnTen_Click(object sender, EventArgs e) { lblHoTen.Text = txtTen.Text; } private void btnHoVaTen_Click(object sender, EventArgs e) { lblHoTen.Text = txtHo.Text +" "+ txtTen.Text; } } }

Page 29: Lab Report

29

Execution Result

Page 30: Lab Report

30

Page 31: Lab Report

31

Exercise 5

Problem

Yêu Cầu: Thiết kế giao diện như hình

Yêu cầu 1. khi Form load. a. Radi obut t on Red được chọn mặc định b. Con trỏ xuất hiện ngay tại ô Nhập tên. 2. Khi gõ tên vào ô Nhập tên (gõ kí tự nào thì lable lập trình chạy song song cùng giá trị

trong ô nhập tên)

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai05 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)

Page 32: Lab Report

32

{ pictureBox1.Image = imageList1.Images[0]; pictureBox2.Image = imageList1.Images[1]; //lblLapTrinh.ForeColor = Color.Red; //txtNhapTen.ForeColor = Color.Red; } private void pictureBox1_Click(object sender, EventArgs e) { pictureBox2.Visible = true; pictureBox1.Visible = false; } private void pictureBox2_Click(object sender, EventArgs e) { pictureBox2.Visible = false; pictureBox1.Visible = true; } private void txtNhapTen_TextChanged(object sender, EventArgs e) { lblLapTrinh.Text = txtNhapTen.Text; } private void rbnRed_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Red; txtNhapTen.ForeColor = Color.Red; } private void rbnGreen_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Green; txtNhapTen.ForeColor = Color.Green; } private void rbnBlue_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Blue; txtNhapTen.ForeColor = Color.Blue; } private void rbnBlack_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Black; txtNhapTen.ForeColor = Color.Black; } private void cbxBold_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size,lblLapTrinh.Font.Style ^ FontStyle.Bold); } private void cbxItalic_CheckedChanged(object sender, EventArgs e) {

Page 33: Lab Report

33

lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size, lblLapTrinh.Font.Style ^ FontStyle.Italic); } private void cbxUnderline_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size, lblLapTrinh.Font.Style ^ FontStyle.Underline); } private void btnThoat_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat y/n?","thong bao",MessageBoxButtons.YesNo)==DialogResult.Yes) { this.Close(); } } } }

Execution Result

Page 34: Lab Report

34

Exercise 6

Problem

Viết lệnh để chư ng t nh hoạt động như sau:

Khi form vừa load lên thì: Con trỏ được đặt tại vị trí ô Name Hình CDRom lớn hiện ngược lại hình nhỏ ẩn. Hướng dẫn: hiệu chỉnh property Visible của picSmall thành false (trong lúc thiết kế) Đưa chụôt vào hình CDRom (lớn, nhỏ) hiện lên tooltip “Click Me”

Hiệu chỉnh TabIndex sao cho hợp lý (Tham khảo hình vào Menu View\Tab Order)

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai06 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnDisplay_Click(object sender, EventArgs e) { lblMessage.Text = txtName.Text +" :" +txtMessage.Text; } private void btnClear_Click(object sender, EventArgs e) { txtMessage.Clear(); txtName.Clear(); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void pictureBox1_Click(object sender, EventArgs e) { pictureBox1.Visible = false; pictureBox2.Visible = true; }

Page 35: Lab Report

35

private void pictureBox2_Click(object sender, EventArgs e) { pictureBox1.Visible = true; pictureBox2.Visible = false; } private void rbnRed_CheckedChanged(object sender, EventArgs e) { lblMessage.ForeColor = Color.Red; } private void rbnGreen_CheckedChanged(object sender, EventArgs e) { lblMessage.ForeColor = Color.Green; } private void rbnBlue_CheckedChanged(object sender, EventArgs e) { lblMessage.ForeColor = Color.Blue; } private void rbnBlack_CheckedChanged(object sender, EventArgs e) { lblMessage.ForeColor = Color.Black; } private void cbxMessageVisible_CheckedChanged(object sender, EventArgs e) { lblMessage.Visible = cbxMessageVisible.Checked; } } }

Execution Result

Exercise 7

Problem

Mục đích: Kết hợp dử dụng component ToolTip trên các control Yêu cầu

1. Khi chư ng trình vừa thực thi. a. Form được hiển thị giữa màn hình b. Các đối tượng được “neo” sao cho khi người dùng thay đổi kích thước form thì các đối tượng được resize kích thước sao cho “dễ nhìn” c. picTurnOn được hiển thị, picTurnOff được ẩn ngay bên dưới picTurnOn.

Page 36: Lab Report

36

Khi người dùng đưa chuột vào picTurnOn sẽ xuất hiện tip “Click me to Turn OFF the Light!”, và ngược lại trên picTurnOFF. d. Hiện tên mặc định vào ô Name e. Lable ngay bên dưới pictureBox hiển thị thông báo

“Jack. Turn Off the Light ,please! ”

Code Listing

Page 37: Lab Report

37

Execution Result

Exercise 8

Problem

Mục đích: Lập trình biến cố trên các đối tượng được tạo động lúc thực thi(run time)

Yêu cầu Thiết kế form gồm 2 button Add và Remove. Khi người sử dụng Click và button Add, chư ng trình sẽ phát sinh động 1 button trên form (mỗi button là một Element thứ n). Khi button Remove được Click, thì button trên cùng sẽ được remove. (Có thể hình dung đây là một Stack). Khi người dùng Click vào một Element nào thì sẽ hiển thị trạng thái của Element đó

lên một label ở phía trên form.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

Page 38: Lab Report

38

using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai08 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int i = 0; private void btnAdd_Click(object sender, EventArgs e) { if (i <= 7) { Button b = new Button(); b.Size = new Size(60, 25); b.Text = "button " + i.ToString(); i++; b.Name = "button " + i.ToString(); b.Location = new System.Drawing.Point((panel1.Width - b.Width) / 2, panel1.Height + 25 - (i+1) * 25); b.Click += new EventHandler(this.button_click); panel1.Controls.Add(b); } else { MessageBox.Show("het them dc roi cha noi"); } } public void button_click(object sender, EventArgs e) { Button b = (Button)sender; lblStatus.Text ="Status : "+b.Text+" was clicked!!!"; } private void btnRemove_Click(object sender, EventArgs e) { if (i > 0) { panel1.Controls.RemoveAt(i-1); i--; } else { MessageBox.Show("het xoa dc roi cha noi"); } } } }

Page 39: Lab Report

39

Execution Result

Exercise 9

Problem

Mục đích: Sử dụng các dialog được cung cấp bởi C#. Sử dụng RichTextBox Yêu cầu Ex1: Using Common Dialog Boxes

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai09 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) {

Page 40: Lab Report

40

openFileDialog1.Filter = "(*.txt)|*.txt|(*.rtf)|*.rft"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { rtbDisplay.Clear(); rtbDisplay.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText); } } private void btnSave_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "(*.txt)|*.txt|(*.doc)|*.doc"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { rtbDisplay.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnColor_Click(object sender, EventArgs e) { if (colorDialog1.ShowDialog() == DialogResult.OK) { rtbDisplay.SelectionColor = colorDialog1.Color; } } private void btnFont_Click(object sender, EventArgs e) { if (fontDialog1.ShowDialog() == DialogResult.OK) { rtbDisplay.Font = fontDialog1.Font; } } } }

Page 41: Lab Report

41

Execution Result

Exercise 10

Problem

Mục đích: Sử dụng đối tượng LinkLable liên kết đến một WebSite, mở ứng dụng Yêu cầu: Using LinkLabel Controls (page 456 )

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai10 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void lnkWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("IExplore", "http://www.google.com.vn");

Page 42: Lab Report

42

} private void lnkCalculator_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("calc.exe"); } private void lnlOpenC_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("c://"); } } }

Execution Result

Exercise 11

Problem

Mục đích: Sử dụng GroupBox và Panel Yêu cầu : Using GroupBox and Panel Controls

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;

Page 43: Lab Report

43

namespace Bai11 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //FileInfo f = null; private void Form1_Load(object sender, EventArgs e) { //f= new FileInfo(pictureBox1.n //f = new FileInfo(pictureBox1.Image.ToString()); //lblFileSize.Text = "File Size: "+f.Length.ToString(); //MessageBox.Show( pictureBox1.Image. //FileInfo f = new FileInfo(pictureBox1.ImageLocation.ToString()); //MessageBox.Show(pictureBox1.ImageLocation); lblDateLastModified.Text = "Date last modified : "; lblDateLastAccessed.Text = "Date last accessed : "; } private void btnBrowse_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo f = new FileInfo(openFileDialog1.FileName); lblFileSize.Text = "File Size: " + f.Length.ToString(); lblDateLastModified.Text = "Date last modified : "+f.LastWriteTime.ToString(); lblDateLastAccessed.Text = "Date last accessed : "+f.LastAccessTime.ToString(); pictureBox1.Image = new Bitmap(openFileDialog1.FileName); //MessageBox.Show(openFileDialog1.FileName.ToString()); } } } }

Page 44: Lab Report

44

Execution Result

Exercise 12

Problem

Mục đích: Sử dụng ListBox, CheckListBox, Combobox

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai12 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {

Page 45: Lab Report

45

string s = checkedListBox1.SelectedItem.ToString(); if(e.NewValue == CheckState.Checked) { lbxListBox.Items.Add(s); } else { lbxListBox.Items.Remove(s); } } private void btnDone_Click(object sender, EventArgs e) { StringBuilder str = new StringBuilder(); if(lbxListBox.SelectedItems.Count >0) { str.Append("Ban da chon \n\n"); foreach (string s in lbxListBox.SelectedItems) { str.Append(s + "\n"); } } else { str.Append("Ban ko chon muc nao"); } MessageBox.Show(str.ToString(), "tinh trang chon", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }

Execution Result

Page 46: Lab Report

46

Exercise 13

Problem

Mục đích: Thao tác dữ liệu số trên Listbox

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCapNhat_Click(object sender, EventArgs e) { lstA.Items.Add(txtNhap.Text); txtNhap.Clear(); txtNhap.Focus(); } private void btnTong_Click(object sender, EventArgs e) { int tong = 0; for (int i = 0; i < lstA.Items.Count; i++) { tong += int.Parse(lstA.Items[i].ToString()); }

Page 47: Lab Report

47

MessageBox.Show("tong danh sach = "+tong.ToString()); } private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } private void btnXoaDauCuoi_Click(object sender, EventArgs e) { if (lstA.Items.Count > 1) { lstA.Items.RemoveAt(lstA.Items.Count - 1); lstA.Items.RemoveAt(0); } else { MessageBox.Show("danh sach chi con mot phan tu hoac ko co phan tu nao"); } } private void btnXoaDangChon_Click(object sender, EventArgs e) { for (int i = 0; i < lstA.Items.Count;i++ ) { if (lstA.GetSelected(i)) { lstA.Items.RemoveAt(i); i--; } } } private void btnTangLen2_Click(object sender, EventArgs e) { if (lstA.Items.Count > 0) { for (int i = 0; i < lstA.Items.Count; i++) { int a = int.Parse(lstA.Items[i].ToString()) + 2; lstA.Items.RemoveAt(i); lstA.Items.Insert(i, a.ToString()); } } } private void btnThayBinhPhuong_Click(object sender, EventArgs e) { if (lstA.Items.Count > 0) { for (int i = 0; i < lstA.Items.Count; i++) { int a = int.Parse(lstA.Items[i].ToString()); a = a * a; lstA.Items.RemoveAt(i); lstA.Items.Insert(i, a.ToString()); } } }

Page 48: Lab Report

48

private void btnChonSoChan_Click(object sender, EventArgs e) { lstA.ClearSelected(); for (int i = 0; i < lstA.Items.Count; i++) { int a = int.Parse(lstA.Items[i].ToString()); if (a % 2 == 0) lstA.SetSelected(i, true); } } private void btnChonSoLe_Click(object sender, EventArgs e) { lstA.ClearSelected(); for (int i = 0; i < lstA.Items.Count; i++) { int a = int.Parse(lstA.Items[i].ToString()); if (a % 2 != 0) lstA.SetSelected(i, true); } } } }

Execution Result

Page 49: Lab Report

49

Exercise 14

Problem

Yêu cầu Thiết kế form và thực hiện các chức n ng sau

- Quy định form hiễn thị giữa màn hình. Không cho người sử dụng thay đổi kích thước form. - Quy định việc di chuyển Tab hợp lý. - Các ListBox được phép chọn nhiều mục (kết hợp giữa phím Shift, Control và chuột) - Khi người sử dụng Click nút “Cập nhật” hoặc nhấn phím Enter thì mặt định nhập tên sinh viên từ textbox vào danh sách lớp A (không chấp nhận dữ liệu rỗng). Theo đó người sử dụng có thể chọn lớp để cập nhật từ combobox lớp. - Các nút “>” và “<” khi được Click sẽ di chuyển tất cả các mục đang chọn sang ListBox

bên kia tư ng ứng. - Các nút “>>” và “<<” khi được Click sẽ di chuyển toàn các mục sang Listbox bên kia tư ng

ứng. - Nút lệnh “Xóa lớp A”, “Xóa lớp B” cho phép xóa các mục đang chọn trong list hiện hành.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai14 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btbCapNhat_Click(object sender, EventArgs e) { if (txtNhap.Text != "") { lstA.Items.Add(txtNhap.Text);

Page 50: Lab Report

50

txtNhap.Clear(); txtNhap.Focus(); } } private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } private void btnRight_Click(object sender, EventArgs e) { for(int i = 0 ; i < lstA.Items.Count ; i++) { if(lstA.GetSelected(i)) { lstB.Items.Add(lstA.Items[i].ToString()); lstA.Items.RemoveAt(i); i--; } } } private void btnDoubleRight_Click(object sender, EventArgs e) { for (int i = 0; i < lstA.Items.Count; i++) { lstB.Items.Add(lstA.Items[i].ToString()); lstA.Items.RemoveAt(i); i--; } } private void btnLeft_Click(object sender, EventArgs e) { for (int i = 0; i < lstB.Items.Count; i++) { if (lstB.GetSelected(i)) { lstA.Items.Add(lstB.Items[i].ToString()); lstB.Items.RemoveAt(i); i--; } } } private void btnDoubleLeft_Click(object sender, EventArgs e) { for (int i = 0; i < lstB.Items.Count; i++) { lstA.Items.Add(lstB.Items[i].ToString()); lstB.Items.RemoveAt(i); i--; } } private void btnXoaLopA_Click(object sender, EventArgs e) { lstA.Items.Clear(); }

Page 51: Lab Report

51

private void btnXoaLopB_Click(object sender, EventArgs e) { lstB.Items.Clear(); } private void cậpNhậtLớpBToolStripMenuItem_Click(object sender, EventArgs e) { if (txtNhap.Text != "") { lstB.Items.Add(txtNhap.Text); txtNhap.Clear(); txtNhap.Focus(); } } } }

Execution Result

Page 52: Lab Report

52

Exercise 15

Problem

Mục đích: Sử dụng các control của Visual Studio

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai15_3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MessageBoxButtons button; MessageBoxIcon icon; private void btnShow_Click(object sender, EventArgs e) { MessageBox.Show(txtMessage.Text, txtCaption.Text,button,icon); }

Page 53: Lab Report

53

private void Form1_Load(object sender, EventArgs e) { txtMessage.SelectAll(); } private void rbnOk_CheckedChanged(object sender, EventArgs e) { RadioButton btn = (RadioButton)sender; if (btn == rbnOk) button = MessageBoxButtons.OK; if (btn == rbnOkAndCancel) button = MessageBoxButtons.OKCancel; if (btn == rbnRetryAndCancel) button = MessageBoxButtons.RetryCancel; if (btn == rbnYesNo) button = MessageBoxButtons.YesNo; if (btn == rbnYesNoAndCancel) button = MessageBoxButtons.YesNoCancel; } private void rbnError_CheckedChanged(object sender, EventArgs e) { RadioButton btn = (RadioButton)sender; if (btn == rbnError) icon = MessageBoxIcon.Error; if (btn == rbnInformation) icon = MessageBoxIcon.Information; if (btn == rbnNone) icon = MessageBoxIcon.None; if (btn == rbnQuestion) icon = MessageBoxIcon.Question; if (btn == rbnWarming) icon = MessageBoxIcon.Warning; } private void rbnLeft_CheckedChanged(object sender, EventArgs e) { RadioButton btn = (RadioButton)sender; if (btn == rbnLeft) tabControl1.Alignment = TabAlignment.Left; if (btn == rbnBottom) tabControl1.Alignment = TabAlignment.Bottom; if (btn == rbnRight) tabControl1.Alignment = TabAlignment.Right; if (btn == rbnTop) tabControl1.Alignment = TabAlignment.Top; } } }

Page 54: Lab Report

54

Execution Result

Page 55: Lab Report

55

Exercise 16

Problem

Mục đích: Xây dựng hệ thống menu : MainMenu và ContextMenu(menu cảm ngữ cảnh) (Page

498)

Code Listing

Page 56: Lab Report

56

Execution Result

Exercise 18

Problem

Mục đích: Xây dựng ứng dụng đa form, Sử dụng form MDI

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai18 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int i = 1; private void newToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f = new Form2(); f.Name = "Form" + i.ToString(); f.Text = "Form" + i.ToString(); f.MdiParent = this; f.Show(); } private void customizeToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileHorizontal); } private void optionsToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); } private void toolStripMenuItem1_Click(object sender, EventArgs e) {

Page 57: Lab Report

57

this.LayoutMdi(MdiLayout.Cascade); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { saveFileDialog1.ShowDialog(); } } }

Page 58: Lab Report

58

Execution Result

Page 59: Lab Report

59

Page 60: Lab Report

60

Exercise 19

Problem

Mục đích: Xây dựng ứng dụng kết hợp đối tượng statusBar

Code Listing

Execution Result

Exercise 20

Problem

Mục đích: Xây dựng ứng dụng liên kết nhiềm form Yêu cầu Step by step Creating an MDI Applicati on (Page 512)

Page 61: Lab Report

61

Code Listing

Execution Result

Exercise 21

Problem

Mục đích: Tạo một ứng dụng liên kết các ứng dụng đ tạo ra từ các project trước. Tạo form khởi động (form Flash)

Tạo form About

Code Listing

Execution Result

Exercise 22

Problem

Mục đích: Tạo thanh công cụ (toolBar) trên ứng dụng

Code Listing

Execution Result

Page 62: Lab Report

62

Module 3

Exercise 1

Problem

Thiết kế giao diện như hình vẽ

Yêu cầu: Khi form thực thi o Tất cả text boxes và labels đều rỗng. o Đặt focus vào text box Tên Hàng và xác lập việc di chuyển Tab hợp lý ( menu View\Tab Order) o Form hiễn thị giữa màn hình. (StartPosition =CenterScreen ) o Thiết lập nút Thanh Tóan nhận sự kiện phím Enter (AcceptButton), nút Tiếp nhận sự kiện phím ESC (CancelButton) o Thực hiện canh lề phải cho các đối tượng chứa dữ liệu là số ( Text Align=Right) Xử lư nút lệnh “Thanh Tóan”.

o Các text box Số lượng, Đơn Giá buộc phải có giá trị và chỉ cho phép nhập số. Thêm vào đó:

Số lượng >=0. (int) Đ n giá >=0 (decimal)

Cung cấp các Message Box khác nhau để thông báo r ràng cho việc dữ liệu bị nhập sai. o Tính toán cho mặt hàng hiện tại xuất kết quả ra label Thành Tiền. Nút Tiếp: Thành tiền = số lượng * đơn giá; o Xóa nội dung các textbox và label o Đặt focus vào textbox đầu tiên Nút Kết Thúc: o Phát sinh Messagebox hỏi lại việc đóng form có chính xác không, nếu người sử dụng xác

nhận bằng nút Yes th cho thóat chư ng t nh

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnThanhToan_Click(object sender, EventArgs e) {

Page 63: Lab Report

63

int intSoLuong; decimal decDonGia, decThanhTien; if (txtTenHang.Text == "")// || txtSoLuong.Text == "" || txtDonGia.Text == "") { MessageBox.Show("Chua Nhap Ten Hang"); txtTenHang.Focus(); } else if(txtSoLuong.Text =="") { MessageBox.Show("Chua Nhap So Luong"); txtSoLuong.Focus(); } else if(txtDonGia.Text =="") { MessageBox.Show("Chua Nhap Don Gia"); txtDonGia.Focus(); } else { intSoLuong = int.Parse(txtSoLuong.Text); decDonGia = decimal.Parse(txtDonGia.Text); if (intSoLuong <= 0) { MessageBox.Show("So Luong Phai > 0"); txtSoLuong.SelectAll(); txtSoLuong.Focus(); } else if (decDonGia <= 0) { MessageBox.Show("Don Gia Phai > 0"); txtDonGia.SelectAll(); txtDonGia.Focus(); } else { decThanhTien = intSoLuong * decDonGia; txtThanhTien.Text = decThanhTien.ToString(); } } } private void btnTiep_Click(object sender, EventArgs e) { txtDonGia.Clear(); txtSoLuong.Clear(); txtTenHang.Clear(); txtThanhTien.Clear(); txtTenHang.Focus(); } private void btnKetThuc_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close();

Page 64: Lab Report

64

} } private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z')) { e.Handled = true; } } } }

Execution Result

Exercise 2

Problem

Thiết kế và viết chư ng trình tính diện tích của hình chữ nhật. Phải kiểm tra dữ liệu nhập

vào từ bàn phím một cách chặt chẽ.

Code Listing

using System; using System.Collections.Generic;

Page 65: Lab Report

65

using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtChieuDai_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z')) { e.Handled = true; } } private void btnThoat_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } private void btnTinh_Click(object sender, EventArgs e) { if (txtChieuDai.Text == "") { MessageBox.Show("Chua nhap chieu dai"); txtChieuDai.Focus(); } else if (txtChieuRong.Text == "") { MessageBox.Show("chua nhap chieu rong"); txtChieuRong.Focus(); } else { float fChieuRong = float.Parse(txtChieuRong.Text); float fChieuDai = float.Parse(txtChieuDai.Text); if (fChieuDai <= 0) { MessageBox.Show("Chieu dai phai > 0"); txtChieuDai.SelectAll(); txtChieuDai.Focus(); } else if (fChieuRong <= 0) { MessageBox.Show("Chieu Rong Phai > 0");

Page 66: Lab Report

66

txtChieuRong.SelectAll(); txtChieuRong.Focus(); } else { float fDienTich = fChieuDai * fChieuRong; txtDienTich.Text = fDienTich.ToString(); txtChieuDai.Focus(); } } } } }

Execution Result

Exercise 3

Problem

Thiết kế và viết chư ng t nh tính chu vi của hình tròn có bán kính R nhập vào từ bàn phím

(lưu ý kiểm tra dữ liệu chặt chẽ)(Chuvi=2*pi*R). Dùng hàm Math.PI

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai03 {

Page 67: Lab Report

67

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtBanKinh_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z')) { e.Handled = true; } } private void btnThoat_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } private void btnTinh_Click(object sender, EventArgs e) { double fBanKinh; if (txtBanKinh.Text == "") { MessageBox.Show("Chua Nhap Ban Kinh"); txtBanKinh.Focus(); } else { fBanKinh = double.Parse(txtBanKinh.Text); if (fBanKinh <= 0) { MessageBox.Show("Ban Kinh Phai > 0"); txtBanKinh.SelectAll(); txtBanKinh.Focus(); } else { double fChuVi = 2 * Math.PI * fBanKinh; txtChuVi.Text = fChuVi.ToString(); txtBanKinh.SelectAll(); } } } } }

Execution Result

Page 68: Lab Report

68

Exercise 4

Problem

Thiết kế và xử lư chư ng t nh giải phư ng t nh bậc 1, 2.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai04 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtC_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z')) { e.Handled = true; } } private void btnThoat_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) txtC.Enabled = false;

Page 69: Lab Report

69

else txtC.Enabled = true; } public bool KiemTraTextbox(TextBox t) { if (t.Text == "") { MessageBox.Show("chua nhap a,b,c"); t.Focus(); return false; } return true; } //ax + b =0 public void GiaiPTbac1(double a, double b) { if (a == 0) { if (b == 0) { txtX.Text = "phuong trinh vo nghiem"; } else { txtX.Text = "phuong trinh vo so nghiem"; } } else { double x = -b / a; txtX.Text = "x = "+x.ToString(); } } //ax*x+bx+c=0 public void GiaiPTbac2(double a, double b, double c) { if (a == 0) { GiaiPTbac1(b, c); } else { double delta = b * b - 4 * a * c; if (delta < 0) { txtX.Text = "phuong trinh vo nghiem"; } else if (delta == 0) { double x = -b / (2 * a); txtX.Text = "x = " + x.ToString(); } else { double x1 = (-b - Math.Sqrt(delta))/(2*a); double x2 = (-b + Math.Sqrt(delta)) / (2 * a);

Page 70: Lab Report

70

txtX.Text = "x1 = " + x1.ToString() + " ,x2= " + x2.ToString(); } } } private void btnGiai_Click(object sender, EventArgs e) { double a, b, c; if (checkBox1.Checked == false) { if (KiemTraTextbox(txtA) == true) { if (KiemTraTextbox(txtB) == true) { if (KiemTraTextbox(txtC) == true) { a = double.Parse(txtA.Text); b = double.Parse(txtB.Text); c = double.Parse(txtC.Text); GiaiPTbac2(a, b, c); } } } } else { if (KiemTraTextbox(txtA) == true) { if (KiemTraTextbox(txtB) == true) { a = double.Parse(txtA.Text); b = double.Parse(txtB.Text); GiaiPTbac1(a, b); } } } } } }

Page 71: Lab Report

71

Execution Result

Exercise 5

Problem

Mục đích: Sử dụng hằng số, biến toàn cục (modul e variable)

Yêu cầu: 1) Sinh viên tự thiết kế các đối tượng trên form cho phù hợp 2) Thiết đặt TabIndex cho hợp lý. 3) Nhấn nút Tính hoặc Enter thì tính và hiện thông tin lên các ô tư ng ứng. 4) Nhấn vào Exit hoặc Esc thì thoát chư n g trình. Ghi chú: sử dụng biến toàn cục và hằng số cho hợp lý

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai05 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)

Page 72: Lab Report

72

{ if((e.KeyChar >= 'A' && e.KeyChar <= 'Z')||(e.KeyChar >= 'a' &&e.KeyChar<='z')) { e.Handled=true; } } int intTongSoLuong = 0, intTongTien = 0, intSoLanBan=0; float fTrungBinh; private void btnTinh_Click(object sender, EventArgs e) { int intSoLuong,intDonGia,intThanhTien; if (txtSoLuong.Text == "") { MessageBox.Show("chua nhap so luong"); txtSoLuong.Focus(); } else if (txtDonGia.Text == "") { MessageBox.Show("chua nhap don gia"); txtDonGia.Focus(); } else { intSoLuong = int.Parse(txtSoLuong.Text); intDonGia = int.Parse(txtDonGia.Text); if (intSoLuong <= 0) { MessageBox.Show("so luong fai > 0"); txtSoLuong.SelectAll(); txtSoLuong.Focus(); } else if (intDonGia <= 0) { MessageBox.Show("don gia fai > 0"); txtDonGia.SelectAll(); txtDonGia.Focus(); } else { //tinh thanh tien intThanhTien = intDonGia * intSoLuong; txtThanhTien.Text = intThanhTien.ToString(); //tinh tong so luong intTongSoLuong += intSoLuong; txtTongSoLuong.Text = intTongSoLuong.ToString(); //tinh tong tien intTongTien += intThanhTien; txtTongTien.Text = intTongTien.ToString(); //tinh trung binh tren moi lan ban intSoLanBan++; fTrungBinh = (float)intTongTien / intSoLanBan; txtTrungBinh.Text = fTrungBinh.ToString(); } }

Page 73: Lab Report

73

} private void btnBanMoi_Click(object sender, EventArgs e) { intTongSoLuong = 0; intTongTien = 0; intSoLanBan = 0; Refresh(); } public void Refresh() { txtDonGia.Clear(); txtSoLuong.Clear(); txtThanhTien.Clear(); txtTongSoLuong.Clear(); txtTongTien.Clear(); txtTrungBinh.Clear(); } private void btnThoat_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } } }

Page 74: Lab Report

74

Execution Result

Page 75: Lab Report

75

Exercise 6

Problem

Mục đích: Sử dụng hằng số, biến toàn cục (module variable)

1. Khi chư ng trình vừa thực thi: - Form được hiển thị giữa màn h nh. Canh lề phải cho các control chứa dữ liệu là số, thiết

lập hệ thống Tab hợp lý. - Thiết lập nút “Tính TT” nhận phím Enter thay cho sự kiện Click. 2. Yêu cầu cài đặt Tên KHách hàng không được phép rỗng. Số lượng sách là số nguyên dư ng Mỗi quyển sách đều được bán với giá là 20000. - Nút “Tính TT”: tính thành tiền và xuất kết quả lên lable Thành tiền Thành tiền = Số lượng * Đ n Giá ; Lưu ý: Nếu Khách hàng là SV thì được giảm 5% thành tiền.

- Nút “Tiếp”: - Xóa nội dung các control trong gourpbox “Hóa Đ n” - Đặt focus cho Textbox “Tên Khách Hàng” - Nút “Thống Kê”: Tính và hiển thị kết quả trên các lable trong groupbox “Thống kê” - Nút “Kết Thúc”: Phát sinh messageBox hỏi lại người dùng có thật sự muốn đóng ứng

dụng hay không?

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai06 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int dongia = 20000; private void Form1_Load(object sender, EventArgs e) { } private void txtSLSach_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z'))

Page 76: Lab Report

76

{ e.Handled = true; } } private void btnKetThuc_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } private void btnTiep_Click(object sender, EventArgs e) { txtTenKH.Clear(); txtSLSach.Clear(); txtThanhTien.Clear(); lblTongDoanhThu.Text = ""; lblTongSoKH.Text = ""; lblTongSoKHLaSV.Text = ""; fTongDoanhThu = 0; intTongKH = 0; intTongSV = 0; checkBox1.Checked = false; } float fTongDoanhThu = 0; int intTongKH = 0,intTongSV = 0; private void btnTinhTT_Click(object sender, EventArgs e) { int intSoLuong; float fThanhTien,fHeSo; if (txtTenKH.Text == "") { MessageBox.Show("Ten khach hang ko dc rong"); txtTenKH.Focus(); } else { intSoLuong = int.Parse(txtSLSach.Text); if (intSoLuong <= 0) { MessageBox.Show("So Luong fai > 0"); txtSLSach.SelectAll(); txtSLSach.Focus(); } else { if (checkBox1.Checked == true) { fHeSo = 0.95f; intTongKH++; intTongSV++; } else { intTongKH++; fHeSo = 1; } //tinh thanh tien

Page 77: Lab Report

77

fThanhTien = intSoLuong * dongia * fHeSo; txtThanhTien.Text = fThanhTien.ToString(); fTongDoanhThu += fThanhTien; } } } private void btnThongKe_Click(object sender, EventArgs e) { //tinh tong so khach hang lblTongSoKH.Text = intTongKH.ToString(); //tinh tong so khach hang la sv lblTongSoKHLaSV.Text = intTongSV.ToString(); //tinh tong doanh thu lblTongDoanhThu.Text = fTongDoanhThu.ToString(); } } }

Page 78: Lab Report

78

Execution Result

Page 79: Lab Report

79

Exercise 7

Problem

Viết chư ng t nh nhập vào n m dư ng lịch và xuất n m âm lịch tư ng ứng. Biết rằng : N m Âm = Can + Chi; Can = N m dư ng % 10 :

Chi = dư ng lịch % 12 :

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

Page 80: Lab Report

80

namespace Bai07 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtNamDuongLich_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z')) { e.Handled = true; } } private void btnDoi_Click(object sender, EventArgs e) { int namDuongLich,intCan,intChi; string namAmLich = ""; string[] strCan = { "Canh", "Tân", "Nhâm", "Quý", "Giáp", "Ất", "Bính", "Đinh", "Mậu", "Kỷ" }; string[] strChi = { "Thân", "Dậu", "Tuất", "Hợi", "Tý", "Sửu", "Dần", "Mẹo", "Thìn", "Tỵ", "Ngọ", "Mùi" }; if (txtNamDuongLich.Text != "") { namDuongLich = int.Parse(txtNamDuongLich.Text); if (namDuongLich <= 0) { MessageBox.Show("Nam Duong Lich Phai > 0"); txtNamDuongLich.SelectAll(); txtNamDuongLich.Focus(); } else { intCan = namDuongLich % 10; intChi = namDuongLich % 12; namAmLich = strCan[intCan].ToString() + " " + strChi[intChi].ToString(); txtNamAmLich.Text = namAmLich.ToString(); txtNamDuongLich.SelectAll(); } } else { MessageBox.Show("chua nhap nam duong lich"); txtNamDuongLich.Focus(); } } } }

Page 81: Lab Report

81

Execution Result

Exercise 8

Problem

Chư ng trình quay số may mắn

Thiết kế giao diện như hình - Groupbox chứa 3 label. - 2 Label dùng để hiễn thị kết quả (canh lề pải) - 3 button như hình vẽ. Yêu cầu Khi chương trình vừa bắt đầu - 3 label sẽ hiễn thị 3 số 7. - Máy tính có 100 đồng - Người ch i có 100 đồng Khi Click nút quay số Luật ch i: Mỗi lần quay số, người ch i phải trả trước 30 đồng, tư ng ứng là máy sẽ được cộng thêm 30 đồng. Và nếu người ch i có số tiền ít h n 30 đồng th sẽ không được phép ch i tiếp Nếu mỗi lần quay được số 7, người ch i sẽ được thưởng như sau - Nếu ô đầu tiên là số 7 (random từ 0 đến 8), được thưởng 100 đồng + 50% số tiền của máy. - Nếu ô số 2 là số 7(random từ 0 đến 9), người ch i được thưởng 30 đồng + 50% tiền của máy. - Nếu ô số 3 là số 7(random từ 0 đến 10), người ch i được thưởng 10 đồng; (số tiền người ch i sẽ được cộng dồn nếu cả 3 ô cùng là số 7, tiền máy sẽ giảm đi phần

tr m tư ng ứng)

Nút game mới Cho phép 1 lược ch i mới hòan tòan (set lại tiền ngư i ch i và tiền máy là 100)

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel;

Page 82: Lab Report

82

using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai08 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int tienNguoiChoi, tienMay = 100, soThu1, soThu2, soThu3, tienThuong; private void btnQuaySo_Click(object sender, EventArgs e) { tienNguoiChoi = int.Parse(lblTienNguoiChoi.Text); tienNguoiChoi -= 30; lblTienNguoiChoi.Text = tienNguoiChoi.ToString(); timer1.Interval = 100; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { btnQuaySo.Enabled = false; if (timer1.Interval < 200) { Random r = new Random(); soThu1 = r.Next(1, 9); soThu2 = r.Next(1, 8); soThu3 = r.Next(1, 7); lblNo1.Text = soThu1.ToString(); lblNo2.Text = soThu2.ToString(); lblNo3.Text = soThu3.ToString(); timer1.Interval += 5; } else { timer1.Stop(); tienThuong = 0; if (soThu1 == 7) { tienThuong += 100 + tienMay / 2; tienMay /= 2; lblTienMay.Text = tienMay.ToString(); } if (soThu2 == 7) { tienThuong += 30 + tienMay / 2; tienMay /= 2;

Page 83: Lab Report

83

lblTienMay.Text = tienMay.ToString(); } if (soThu3 == 7) tienThuong += 10; tienNguoiChoi += tienThuong; lblTienNguoiChoi.Text = tienNguoiChoi.ToString(); MessageBox.Show("Thuong " + tienThuong + " dong!"); if (tienNguoiChoi >= 30) btnQuaySo.Enabled = true; else { btnGameMoi.Enabled = true; } } } private void btnGameMoi_Click(object sender, EventArgs e) { tienMay = 100; tienNguoiChoi = 100; lblTienMay.Text = tienMay.ToString(); lblTienNguoiChoi.Text = tienNguoiChoi.ToString(); btnQuaySo.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { btnGameMoi.Enabled = false; } private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } } }

Execution Result

Page 84: Lab Report

84

Page 85: Lab Report

85

Exercise 9

Problem

Viết chư n g trình cho phép phát sinh 1 số nguyên thuộ c [0,9] , sau đó cho người dùng đoán số và chư ng trình sẽ hiện kết quả xem người dùng có đoán đúng số phát sinh đó hay không. HD: Dùng lớp Random để phát sinh số ngẫu nhiên.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai09 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int soDung,soDoan; private Random r; private void Form1_Load(object sender, EventArgs e) { r = new Random();

Page 86: Lab Report

86

soDung = r.Next(0, 9); } private void btnDoan_Click(object sender, EventArgs e) { soDoan = int.Parse(txtNhap.Text); if(soDoan < soDung) { MessageBox.Show("can doan so lon hon"); } else { if(soDoan > soDung) { MessageBox.Show("can doan so nho hon"); } else { MessageBox.Show("chuc mung ban da doan dung,moi doan tiep"); soDung = r.Next(0, 9); } } txtNhap.SelectAll(); txtNhap.Focus(); } private void btnThoat_Click(object sender, EventArgs e) { Close(); } } }

Execution Result

Page 87: Lab Report

87

Exercise 10

Problem

Thiết kế giao diện trình bày s đồ các chỗ ngồi của một rạp chiếu phim giúp người sử dụng

chọn vị trí muốn mua. Trên s đồ này cũng thể hiện những vị trí đ bán vé và những vị trí

chưa bán vé bằng cách thể hiện màu khác nhau ( ghế chưa bán vé màu trắng, ghế đ bán

vé màu vàng, ghế đang chọn màu xanh).

Khi khởi động, ứng dụng phải nằm giữa màn hình. Không cho phép người dùng thay đổi kích thước form. Khi người sử dụng nhấp chuột tại một vị trí trên s đồ thì: o Nếu đây là vị trí chưa bán vé thì đổi màu của vị trí này sang màu xanh để cho biết đây là vị trí đang chọn. o Nếu đây là vị trí đang chọn (có màu xanh) thì đổi màu của vị trí này trở về màu trắng o Nếu đây là một vị trí đ bán vé thì xuất hiện một thông báo cho người sử dụng biết thông qua messageBox. Sau khi đ chọn các vị trí người sử sụng có thể nhấn vào nút CHỌN hoặc HỦY BỎ.

ếu click vào nút CHỌN (hoặc nhấn phím Enter) thì: Đổi màu các vị trí đ chọn (màu xanh) trên s đồ sang màu vàng (cho biết vị trí đ bán vé) và xuất lên label thành tiền phải trả cho số vé đ mua ( giả sử giá vé là 100đ/ghế)

ếu click vào nút HỦY BỎ (hoặc nhấn phín ESC) thì: Đổi màu các vị trí đ chọn (màu xanh) trên s đồ sang màu trắng trở lại và xuất lên label thành tiền giá trị 0

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai10 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

Page 88: Lab Report

88

private void btnKetThuc_Click(object sender, EventArgs e) { if (MessageBox.Show("thoat chuong trinh y/n?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } List<Label> DanhSachChon = new List<Label>(); int intThanhTien = 0; private void label2_Click(object sender, EventArgs e) { Label lbl = (Label)sender; //neu label khac mau vang if (lbl.BackColor != Color.Yellow) { //neu label la mau trang if (lbl.BackColor == Color.White) { //chuyen label mau trang thanh mau xanh lbl.BackColor = Color.Blue; DanhSachChon.Add(lbl); } else//nguoc lai label mau xanh { //chuyen sang mau trang lbl.BackColor = Color.White; DanhSachChon.Remove(lbl); } } else//label mau vang { //thang bao co nguoi chon roi MessageBox.Show("Bàn này có người chọn rồi"); } } private void btnChon_Click(object sender, EventArgs e) { foreach (Label l in DanhSachChon) { l.BackColor = Color.Yellow; intThanhTien += 100; } lblThanhTien.Text = intThanhTien.ToString(); intThanhTien = 0; DanhSachChon = new List<Label>(); } private void btnHuy_Click(object sender, EventArgs e) { foreach (Label l in DanhSachChon) { l.BackColor = Color.White; } lblThanhTien.Text = ""; DanhSachChon = new List<Label>(); }

Page 89: Lab Report

89

} }

Execution Result

Page 90: Lab Report

90

Exercise 11

Problem

H y tạo một Project dùng để quản lư giờ thuê máy của một phòng máy có giao diện và yêu cầu như sau: o Khi form vừa load lên màn hình thì các CheckBox Bắt đầu có Enabled=True các CheckBox Kết thúc có Enabled=False Các Label có Caption= rổng

Page 91: Lab Report

91

o Khi người sử dụng Click vào ChechBox Bắt Đầu của một máy và nếu thuộc tính value của nó =1 thì: Hiện Giờ hiện tại lên Label bắt đầu của máy tư ng ứng Cho thuộc tính Enabled của CheckBox Bắt đằu =False Cho thuộc tính Enabled của CheckBox Kết thúc =True o Khi người sử dụng Click vào ChechBox Kết thúc của một máy và nếu thuộc tính

value của nó =1 thì: Hiện Giờ hiện tại lên Label Kết thúc của máy tư ng ứng Dựa vào Giờ và Phút (không cần quan tâm đến số giây) trên các label Bắt đầu và Kết thúc tính ra số tiến phải trả, rồi dùng Msgbox để thông báo cho người sử dụng như h nh bên (tiền thuê máy là 3000đ/giờ) Xóa nội dung trong các Label Bắt đầu và Kết thúc Cho thuộc tính Enabled của CheckBox Bắt đằu =True Cho thuộc tính Enabled của CheckBox Kết t húc =False Cho thuộc tính Value của CheckBox Bắt đằu =0 Cho thuộc tính Value của CheckBox Kết thúc =0

Khi người sử dụng Click vào nút Dừng chương trình thì kiểm tra nếu có máy đang

thuê phải nhắc nhở người sử dụng bằng MsgBox, khi nào người sử dụng thật sự

muốn kết thúc thì mới dừng chư ng trình.

Code Listing

Execution Result

Exercise 12

Problem

H y tạo một Project dùng để quản lý giờ thuê máy của một phòng máy có giao diện và yêu cầu như sau: o Khi form vừa load lên màn hình thì Các image trong khung máy 1 và máy 2 có picture là Máy tính

Các Label có Text = rổng

o Khi người sử dụng click vào image của máy 1 hoặc máy 2 thì: Nếu là máy chưa thuê:

Đổi picture của image thành h nh Đồng hồ Hiện giờ hiện tại vào label bắt đầu của máy (label phía trên)

Page 92: Lab Report

92

Nếu là máy đang thuê: Hiện giờ hiện tại vào label Kết thúc của máy (label phía dưới) Dựa vào Giờ và Phút (không cần quan tâm đến số giây) trên các label Bắt đầu và Kết thúc tính ra số tiến phải trả, rồi dùng Msgbox để thông báo (tiền thuê máy là 3000đ/giờ)

Đổi picture của image thành hình Máy tính Xóa nội dung trong các Label Bắt đầu và Kết thúc Khi người sử dụng Click

vào nút Kết thúc th kiểm tra nếu có máy đang thuê phải nhắc nhở người sử dụng bằng MsgBox, khi nào người sử dụng thật sự muốn kết thúc thì mới dừng chư ng trình.

Code Listing

Execution Result

Module 4

Exercise 1

Problem

Mục đích: Sử dụng lớp Exception đề bẩy lỗi chư ng trình. Sử dụng MessageBox để hiển thị thông báo lỗi Yêu cầu: Viết chư ng trình tính số nhiên liệu tiêu thụ trung bình theo giao diện sau

Phải bắt lỗi chặc chẽ các trường hợp sau: - Số dặm đường, số gallon tiêu thụ là số dư ng. - Lỗi tràn số - Lỗi chia cho số 0 - …

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

Page 93: Lab Report

93

using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int miles, galon; private void btnCalculate_Click(object sender, EventArgs e) { miles = int.Parse(txtMilesDriven.Text); galon = int.Parse(txtGallonsOfGasUsed.Text); lblEfficiency.Text = ((float)miles / galon).ToString(); } private void txtMilesDriven_Validating(object sender, CancelEventArgs e) { //kiem tra nhap du lieu vao textbox TextBox txt = (TextBox)sender; try { //1. neu textbox rỗng if (txt.Text == "") { //thong bao chua nhap du lieu errorProvider1.SetError(txt, "chua nhap du lieu"); //giu con tro e.Cancel = true; } else//2. da nhap du lieu { try { //2.2 tao bien tam x va kiem tra int x = int.Parse(txt.Text); //3. so nho hon 0 if (x < 0) { throw new ArithmeticException(); } else//4. du lieu nhap > 0 { errorProvider1.SetError(txt, ""); } } //tràn số catch (OverflowException) { errorProvider1.SetError(txt, "so lon wa"); e.Cancel = true; } }

Page 94: Lab Report

94

} //2.1 du lieu ko phai la số catch (FormatException) { //2.1.1 thong bao errorProvider1.SetError(txt, "nhap so nguyen"); //2.1.2 giu con tro e.Cancel = true; } //3.1 bat su kien so nho hon 0 catch (ArithmeticException) { //3.1.1 thong bao errorProvider1.SetError(txt, "so phai >0"); //3.1.2 giu con tro e.Cancel = true; } //5. cac truong hop loi khac catch (Exception ex) { errorProvider1.SetError(txt, ex.Message.ToString()); e.Cancel = true; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("thoat y/n?", "thong bao 22222", MessageBoxButtons.YesNo) == DialogResult.Yes) { e.Cancel = false; } else { e.Cancel = true; } } private void button1_Click(object sender, EventArgs e) { this.Close(); } } }

Page 95: Lab Report

95

Execution Result

Page 96: Lab Report

96

Page 97: Lab Report

97

Exercise 2

Problem

Mục đích: Sử dụng lớp Exception đề bẩy lỗi chư ng trình. Sử dụng MessageBox để hiển thị thông báo lỗi Yêu cầu: Viết lại bài 1, nhưng sử dụng đối tượng ErrorProvider để hiển thị các thông báo lỗi ngay

trên control

Code Listing

Execution Result

Exercise 3

Problem

Mục đích: Sử dụng lớp Exception đề bẩy lỗi chư ng t nh. Sử dụng finally Block

Lưu ý: Chư ng t nh phải thức hiện việc đóng file bất kẻ có xảy ra lỗi hay không.

Code Listing

Page 98: Lab Report

98

Execution Result

Exercise 4

Problem

Mục đích: Sử dụng Component ErrorProvider kết hợp kỷ thuật Validation trên các control Yêu cầu: Viết lại bài tập một nhưng sử dụng chặn lỗi người dùng bằng kỹ thuật Validation.

Code Listing

Execution Result

Exercise 5

Problem

Mục đích: Lập trình với lớp Exception thuộc không gian System.IO.IOException Sử dụng các component OpenFileDialog, SaveFileDialog, ColorDialog, and FontDialog.

Code Listing

Execution Result

Page 99: Lab Report

99

Exercise 6

Problem

Mục đích: Sử dụng kỹ thuật Validating User Input. Yêu cầu: Thiết kế form login như h nh bên dưới. Xuất thông báo login hợp lệ nếu nội dung của 2 textbox “UserName” và “Password” là khác rỗng và không có bất kỳ khỏang trắng nào bên trong. Ngược lại xuất thông báo lỗi cụ thể thông qua ErrorProvider. Cho phép người dùng đóng Form bằng nút “Cancle” ngay cả khi xảy ra lỗi trên bất kỳ

trường dữ liệu nào.

Code Listing

Execution Result

Module 5

Exercise 1

Problem

Mục đích: Xây dựng mảng một chiều trên ConsoleApp Yêu cầu: Nhập một mảng 1 chiều có N phần tử (N<20) (các giá trị được phát sinh ngẫu nhiên trong [ 100, 100]) a. Tìm số lớn nhất, nhỏ nhất trong mảng. Xuất ra vị trí các giá trị này. b. Đếm xem trong mảng có bao nhiêu số dư ng, bao nhiêu số âm, bao nhiêu số 0. c. Nhập vào số nguyên x, kiểm tra xem x có trong mảng hay không. d. Tìm số dư ng nhỏ nhất trong mảng, số âm lớn nhất trong mảng e. In d y giảm dần.

Code Listing

Page 100: Lab Report

100

Execution Result

Exercise 2

Problem

Mục đích: Xây dựng mảng một chiều trên ConsoleApp Yêu cầu: Nhập một mảng 2 chiều có N x N phần tử (N<20) (các giá trị được phát sinh ngẫu nhiên trong [-100,100]) a. Tìm số lớn nhất, nhỏ nhất trong mảng. Xuất ra vị trí các giá trị này. b. Đếm xem trong mảng có bao nhiêu số dư ng, bao nhiêu số âm, bao nhiêu số 0. c. Đếm số dư ng, số âm, số 0 ở dưới đường chéo chính, chéo phụ.

d. Tính tổng các phần tử trong đường chéo chính, chéo phụ.

e. Tính tổng các phần tử nằm bên dưới đường chéo chính, chéo phụ. f. Tìm số lớn nhất trong từng dụng của mảng.

Code Listing

Execution Result

Exercise 3

Problem

Mục đích: Sử dụng mảng một chiều Yêu cầu: Thiết kế Form như hình sau:

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

Page 101: Lab Report

101

using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai03 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //khai bao private const int Max_Size = 10; private int[] intArray; //form load private void Form1_Load(object sender, EventArgs e) { intArray = new int[Max_Size]; } public void TaoMangNgauNhien() { Random r = new Random(); for(int i = 0; i< intArray.Length;i++) { intArray[i] = r.Next(1, 101); } } public void XuatMang(int[] a) { for (int i = 0; i < a.Length; i++) { txtMangGoc.Text += " " + a[i].ToString(); } } public int TongMang(int[] a) { int kq = 0; for (int i = 0; i < a.Length; i++) { kq += a[i]; } return kq; } public int FindMin(int[] a) { int min = a[0]; for (int i = 1; i < a.Length; i++) { if(a[i]<min) {

Page 102: Lab Report

102

min = a[i]; } } return min; } public void TangMoiPhanTuMangLen2(int[] a) { txtKetQua.Clear(); for (int i = 0; i < a.Length; i++) { a[i] += 2; txtKetQua.Text += " " + a[i].ToString(); } } public int SoPhanTuLe(int[] a) { int dem = 0; for (int i = 0; i < a.Length; i++) { if(a[i]%2==1) { dem++; } } return dem; } public int TongPhanTuLe(int[] a) { int tongle = 0; for (int i = 0; i < a.Length; i++) { if (a[i] % 2 == 1) { tongle+= a[i]; } } return tongle; } public void SapMangTang(int[] a) { for (int i = 0; i < a.Length-1; i++) { for(int j = i+1;j<a.Length;j++) { if(a[i]>a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } public void SapMangGiam(int[] a) {

Page 103: Lab Report

103

for (int i = 0; i < a.Length - 1; i++) { for (int j = i + 1; j < a.Length; j++) { if (a[i] < a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } private void btnXuatMangNgauNhien_Click(object sender, EventArgs e) { TaoMangNgauNhien(); XuatMang(intArray); } private void btnTimPTNhoNhat_Click(object sender, EventArgs e) { txtKetQua.Text = FindMin(intArray).ToString(); } private void btnTinhTongGiaTriMang_Click(object sender, EventArgs e) { txtKetQua.Text = TongMang(intArray).ToString(); } private void btnTangMoiPTLen2_Click(object sender, EventArgs e) { TangMoiPhanTuMangLen2(intArray); } private void btnDemSoPTLe_Click(object sender, EventArgs e) { txtKetQua.Text = SoPhanTuLe(intArray).ToString(); } private void btnTongGiaTriPTLe_Click(object sender, EventArgs e) { txtKetQua.Text = TongPhanTuLe(intArray).ToString(); } private void btnSapMangTang_Click(object sender, EventArgs e) { SapMangTang(intArray); HienThiMang(intArray); } public void HienThiMang(int[] a) { txtKetQua.Clear(); for (int i = 0; i < a.Length; i++) { txtKetQua.Text += " " + a[i].ToString(); } }

Page 104: Lab Report

104

private void btnSapMangGiam_Click(object sender, EventArgs e) { SapMangGiam(intArray); HienThiMang(intArray); } } }

Execution Result

Page 105: Lab Report

105

Page 106: Lab Report

106

Page 107: Lab Report

107

Exercise 4

Problem

Mục đích: Sử dụng mảng hai chiều trên ConsoleApp Yêu cầu:

Page 108: Lab Report

108

Nhập một mảng 2 chiều có N x N phần tử (N<20) (các giá trị được phát sinh ngẫu nhiên trong [-100,100]). Tìm số dư ng nhỏ nhất trong mảng. Ví dụ: -9 8 3 -3 5 0 1 -30 7 Số dư ng nhỏ nhất trong mảng là 1

Code Listing

Execution Result

Exercise 5

Problem

Mục đích: Sử dụng Arraylist, Yêu cầu:

Xây dựng từ điễn đ n giản

- Khi nhập vào combobox 1 từ cần tra thì chư ng trình sẽ dò tìm đến chữ nào khớp với ký tự gần nhất - Khi nhấn nút Enter hoặc Doubleclick vào từ cần tra thì nghĩa tư ng ứng của từ sẽ hiển thị vào textbox bên phải tư ng ứng. - Danh sách các từ lưu sẵ

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace Bai06 {

Page 109: Lab Report

109

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ArrayList arr; private void Form1_Load(object sender, EventArgs e) { arr = new ArrayList(); ThemDuLieuVaoArray(); HienThiArrLenListview(); txtSearch.Focus(); } void ThemDuLieuVaoArray() { arr.Add("student"); arr.Add("Sinh Viên,Học Sinh"); arr.Add("teacher"); arr.Add("Giáo Viên,Giảng Viên"); arr.Add("worker"); arr.Add("Công Nhân"); arr.Add("mouse"); arr.Add("Con Chuột"); arr.Add("stupid"); arr.Add("Ngu Ngốc"); } void HienThiArrLenListview() { for (int i = 0; i < arr.Count; i = i + 2) { listBox1.Items.Add(arr[i].ToString()); } } private void btnThoat_Click(object sender, EventArgs e) { this.Close(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { txtTiengAnh.Text = listBox1.SelectedItem.ToString(); for (int i = 0; i < arr.Count; i = i + 2) { if (arr[i].Equals(listBox1.SelectedItems[0].ToString())) { richTextBox1.Text = arr[i + 1].ToString(); }

Page 110: Lab Report

110

} } private void txtSearch_TextChanged(object sender, EventArgs e) { for (int i = 0; i < arr.Count; i = i + 2) { int x = txtSearch.Text.Length; if (arr[i].ToString().Substring(0, x).Equals(txtSearch.Text)) { int y = arr[i].ToString().Length; txtTiengAnh.Text = arr[i].ToString(); richTextBox1.Text = arr[i + 1].ToString(); //txtTiengAnh.Select(x, y - x); break; } } } } }

Page 111: Lab Report

111

Execution Result

Page 112: Lab Report

112

Module 6

Exercise 1

Problem

Yêu cầu: Thiết kế class Hóa Đ n đáp ứng yêu cầu xử lý của giao diện trên.

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai01 { class HoaDon { private String tenhang; private double soluong; private double dongia;

Page 113: Lab Report

113

public static double sohoadon = 0; public static double doanhthu = 0; public string Tenhang { get { return tenhang; } set { tenhang = value; } } public double Soluong { get { return soluong; } set { soluong = value; } } public double Dongia { get { return dongia; } set { dongia = value; } } public static double Sohoadon { get { return sohoadon; } } public static double Doanhthu { get { return doanhthu; } } public HoaDon() { sohoadon = 0; doanhthu = 0; } public HoaDon(String tenhang,double soluong,double dongia) { this.tenhang = tenhang; this.soluong = soluong; this.dongia = dongia; sohoadon++; } public double thanhTien() { double thanhTien = this.soluong*this.dongia; doanhthu += thanhTien; return thanhTien; } public double getSoHoaDon() { return sohoadon; } public double getDoanhThu() { return doanhthu;

Page 114: Lab Report

114

} } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private double soLuong, donGia; private HoaDon h; bool kiemTraTenHang() { if (string.IsNullOrEmpty(txtTenHang.Text)) { MessageBox.Show("chưa nhập tên hàng"); txtTenHang.Focus(); return false; } return true; } bool kiemTraSoLuong() { try { if (string.IsNullOrEmpty(txtSoLuong.Text)) { MessageBox.Show("chưa nhập số lượng"); txtSoLuong.Focus(); return false; } else { soLuong = double.Parse(txtSoLuong.Text); if (soLuong <= 0) throw new Exception(); } } catch (Exception) { MessageBox.Show("Số Lượng Không Hợp Lệ"); txtSoLuong.SelectAll(); txtSoLuong.Focus(); return false; } return true;

Page 115: Lab Report

115

} bool kiemTraDonGia() { try { if (string.IsNullOrEmpty(txtDonGia.Text)) { MessageBox.Show("chưa nhập Đơn Giá"); txtDonGia.Focus(); return false; } else { donGia = double.Parse(txtDonGia.Text); if (donGia <= 0) throw new Exception(); } } catch (Exception) { MessageBox.Show("Đơn Giá Không Hợp Lệ"); txtDonGia.SelectAll(); txtDonGia.Focus(); return false; } return true; } private void btnTinh_Click(object sender, EventArgs e) { if(kiemTraTenHang()) { if(kiemTraSoLuong()) { if(kiemTraDonGia()) { h = new HoaDon(txtTenHang.Text, soLuong, donGia); txtThanhTien.Text = h.thanhTien().ToString(); } } } } private void btnTiep_Click(object sender, EventArgs e) { txtTenHang.Clear(); txtSoLuong.Clear(); txtDonGia.Clear(); txtThanhTien.Clear(); txtTenHang.Focus(); } private void btnThongKe_Click(object sender, EventArgs e) { txtSoHoaDon.Text = h.getSoHoaDon().ToString(); txtTongDoanhThu.Text = h.getDoanhThu().ToString(); } private void btnKetThuc_Click(object sender, EventArgs e) {

Page 116: Lab Report

116

this.Close(); } } }

Page 117: Lab Report

117

Execution Result

Page 118: Lab Report

118

Exercise 2

Problem

Yêu cầu: 1. Thiết kế Form như hình sau:

2. Thiết kế class Hóa Đ n đáp ứng yêu cầu xử lý của giao diện trên.

Code Listing

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai02 { class HoaDon { //properties private String hoten; private String diachi; private String dienthoai; private double giatien; private double thue; private bool muaxeden; private static double sokhachhang=0; private static double sokhachmuaxeden=0; private static double doanhthu=0; //get , set public string Hoten { get { return hoten; } set { hoten = value; } } public string Diachi { get { return diachi; } set { diachi = value; } } public string Dienthoai { get { return dienthoai; } set { dienthoai = value; } } public static double Sokhachhang { get { return sokhachhang; } } public static double Sokhachmuaxeden { get { return sokhachmuaxeden; } }

Page 119: Lab Report

119

public static double Doanhthu { get { return doanhthu; } } public double Giatien { get { return giatien; } set { giatien = value; } } public double Thue { get { return giatien*0.1; } set { thue = value; } } //constructors public HoaDon() { sokhachhang = 0; sokhachmuaxeden = 0; doanhthu = 0; } public HoaDon(string hoten, string diachi, string dienthoai, double giatien, bool muaxeden) { this.hoten = hoten; this.dienthoai = dienthoai; this.diachi = diachi; this.giatien = giatien; this.muaxeden = muaxeden; sokhachhang++; if (muaxeden) sokhachmuaxeden++; } public double phuThu() { if(muaxeden) { return giatien*0.05; } return 0; } public double TinhThanhTien() { double thanhTien = this.phuThu() + this.Giatien + this.Thue; doanhthu += thanhTien; return thanhTien; } public double getTongKH() { return sokhachhang; } public double getKhMuaXeDen() {

Page 120: Lab Report

120

return sokhachmuaxeden; } public double TongDoanhThu() { return doanhthu; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private HoaDon hd; private void btnTinh_Click(object sender, EventArgs e) { bool muaxeden = rbnMauDen.Checked; hd = new HoaDon(txtHoTen.Text,txtDiaChi.Text,txtDienThoai.Text,double.Parse(txtGiaTien.Text),muaxeden); txtPhuThu.Text = hd.phuThu().ToString(); txtThue.Text = hd.Thue.ToString(); txtThanhTien.Text = hd.TinhThanhTien().ToString(); } private void btnTiep_Click(object sender, EventArgs e) { txtHoTen.Clear(); txtDiaChi.Clear(); txtDienThoai.Clear(); txtGiaTien.Clear(); txtPhuThu.Clear(); txtThue.Clear(); txtThanhTien.Clear(); } private void btnThongKe_Click(object sender, EventArgs e) { txtTongKH.Text = hd.getTongKH().ToString(); txtTongKHMuaXeDen.Text = hd.getKhMuaXeDen().ToString(); txtTongDoanhThu.Text = hd.TongDoanhThu().ToString(); } private void btnKetThuc_Click(object sender, EventArgs e) { this.Close();

Page 121: Lab Report

121

} } }

Page 122: Lab Report

122

Execution Result

Page 123: Lab Report

123

Module 7

Exercise 1

Problem

Viết chư ng trình có giao diện và thực hiện các yêu cầu sau: Khi form được load lên thì chỉ có nút nhập liệu , nút kết thúc là sử dụng được, nút nhập dữ liệu được focus. Khi nút nhập dữ liệu được ấn thì các nút lệnh khác được sáng lên xoá nội dung của

Textbox nhập liệu và Setfocus cho Textbox này ( sử dụng phư ng thức Setfocus).

Khi một trong các nút ( ngoại trừ nút Nhập Dữ Liệu và nút Kết Thúc) được chọn thì

kết quả sẽ hiện thị lên Textbox và các nút lệnh sẽ bị mờ (ngay cả nút vừa nhấn).

Page 124: Lab Report

124

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] arr = new string[100]; private string str; private void btnNhapDuLieu_Click(object sender, EventArgs e) { str = txt1.Text; btnDemSoKTHoa.Enabled = true; btnInChuHoa.Enabled = true; btnInSoTuTrenMoiDong.Enabled = true; btnInChuThuong.Enabled = true; btnDemSoTu.Enabled = true; btnDemSoKTThuong.Enabled = true; btnInNguyenAmPhuAm.Enabled = true; arr = str.Split(' '); } private void Form1_Load(object sender, EventArgs e) { btnNhapDuLieu.Focus(); } private void btnDemSoKTHoa_Click(object sender, EventArgs e) { int soKiTuHoa = 0; foreach (char ch in str) { if (Char.IsUpper(ch)) soKiTuHoa++; } txt2.Text = "số kí tự hoa = " + soKiTuHoa.ToString(); } private void btnInChuHoa_Click(object sender, EventArgs e) { txt2.Text = txt1.Text.ToUpper(); } private void btnInSoTuTrenMoiDong_Click(object sender, EventArgs e) { txt2.Clear();

Page 125: Lab Report

125

foreach (char c in txt1.Text) { if (c.ToString() == " ") txt2.Text += "\n"; else txt2.Text += c.ToString(); } } private void btnInChuThuong_Click(object sender, EventArgs e) { txt2.Text = txt1.Text.ToLower(); } private void btnDemSoTu_Click(object sender, EventArgs e) { txt2.Text = "có " + arr.Length + " từ"; } private void btnDemSoKTThuong_Click(object sender, EventArgs e) { int soKiTuThuong = 0; foreach (char ch in txt1.Text) { if (char.IsLower(ch)) { soKiTuThuong++; } } txt2.Text = "số kí tự thường = " + soKiTuThuong.ToString(); } private string[] CacPhuAm = {"b","c", "d", "đ", "g", "h", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "x"}; private void btnInNguyenAmPhuAm_Click(object sender, EventArgs e) { txt2.Clear(); HashSet<char> nguyenAm = new HashSet<char>(); HashSet<char> phuAm = new HashSet<char>(); foreach (char ch in txt1.Text) { bool flagPhuAm = false; foreach(string s in CacPhuAm) { if(ch.ToString().ToLower() == s) { flagPhuAm = true; } } if (flagPhuAm) phuAm.Add(ch); else nguyenAm.Add(ch); } txt2.Text = "các nguyên âm : "; foreach (char na in nguyenAm) {

Page 126: Lab Report

126

txt2.Text += na.ToString() + " "; } txt2.Text += "\n các phụ âm : "; foreach (char pa in phuAm) { txt2.Text += pa.ToString() + " "; } } private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } } }

Execution Result

Page 127: Lab Report

127

Page 128: Lab Report

128

Page 129: Lab Report

129

Page 130: Lab Report

130

Exercise 2

Problem

Tạo màn hình đếm số từ như sau Dựa vào chuỗi nhập , đếm số từ đ nhập theo quy định các từ ng n cách nhau bằng ký tự trắng (space).

Code Listing

Execution Result

Exercise 3

Problem

Tạo một Project như yêu cầu sau: Khi chọn nút Search thì chư ng trình sẽ thông báo cho người sử dụng biết nội dung nhập trong “Search string” có xuất hiện trong “Main string” hay không Khi chọn nút

Replace thì chư ng trình sẽ thay thế trong Main String chuỗi giá trị Search String

bằng chuỗi Replace String.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai03 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnReplace_Click(object sender, EventArgs e)

Page 131: Lab Report

131

{ rtxContent.SelectAll(); rtxContent.SelectionBackColor = Color.White; rtxContent.Text = rtxContent.Text.Replace(txtSearch.Text, txtReplace.Text); } private void btnSearch_Click(object sender, EventArgs e) { rtxContent.SelectAll(); rtxContent.SelectionBackColor = Color.White; int i = 0; foreach (char ch in rtxContent.Text) { if(ch.ToString().ToLower() == txtSearch.Text.ToLower()) { rtxContent.Select(i, 1); rtxContent.SelectionBackColor = Color.Yellow; } i++; } } } }

Execution Result

Page 132: Lab Report

132

Exercise 4

Problem

Viết chư ng trình thực hiện việc thực hành đánh máy chữ, giao diện như hình bên. Yêu cầu : Khi nhấn 1 kí tự hoặc 1 số nào thì kí tự hoặc số đó chuyển sang màu đỏ, khi thả phím ra thì màu chữ trở lại màu đen. Đồng thời kư tự vừa được nhấn sẽ hiện bên dưới Text box.

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai04 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string input = ""; private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { //MessageBox.Show("code:" + e.KeyCode.ToString() + "\ndata:" + e.KeyData.ToString() + "\nvalue:" + // e.KeyValue.ToString());

Page 133: Lab Report

133

input = e.KeyData.ToString(); SuKienKeyDown(); } private void richTextBox1_KeyUp(object sender, KeyEventArgs e) { input = e.KeyData.ToString(); SuKienKeyUp(); } private void SuKienKeyDown() { foreach (Control ctrlChild in this.Controls) { if (ctrlChild.GetType() == typeof(Label)) { if (ctrlChild.Text.ToUpper() == input.ToUpper()) { ctrlChild.BackColor = Color.OrangeRed; } } } } private void SuKienKeyUp() { foreach (Control ctrlChild in this.Controls) { if (ctrlChild.GetType() == typeof(Label)) { if (ctrlChild.Text.ToUpper() == input.ToUpper()) { ctrlChild.BackColor = Color.White; } } } } } }

Page 134: Lab Report

134

Execution Result

Exercise 5

Problem

Thiết kế giao diện và viết code để thực hiện những yêu cầu sau:

Cho phép nhập một chuỗi Họ Tên

Xuất Họ, Chữ lót, Tên của chuỗi họ tên vào các label tư ng ứng khi click chuột

vào một nút lệnh

Tạo điều kiện để người sử dụng thuận lợi khi nhập họ tên mới.

Có nút lệnh để đóng chư ng trình và phải hỏi người sử dụng trước khi thật sự

kết thúc chư ng trình

Code Listing

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Bai05

Page 135: Lab Report

135

{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnThoat_Click(object sender, EventArgs e) { if(MessageBox.Show("Thoát chương trình ?","thông báo",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK) { this.Close(); } } private void btnXuat_Click(object sender, EventArgs e) { if(string.IsNullOrEmpty(txtHoTen.Text)) { MessageBox.Show("chưa nhập họ tên"); txtHoTen.Focus(); } else { string input = txtHoTen.Text.Trim(); string[] arr = input.Split(' '); if (arr.Length >= 2) { string ho = arr[0]; string ten = arr[arr.Length - 1]; lblHo.Text = ho; lblTen.Text = ten; string chuLot = ""; if (arr.Length > 2) { for (int i = 1; i < arr.Length - 1; i++) { chuLot += arr[i] + " "; } } lblChuLot.Text = chuLot; } else { if (arr.Length <= 1) { MessageBox.Show("họ tên chưa đầy đủ"); txtHoTen.Focus(); } } } } }

Page 136: Lab Report

136

}

Execution Result