56
1. MODULE 1 1.1 Bài 1: Code: namespace Bai_1 { class Program { static void Main(string[] args) { System.Console.WriteLine("Welcome to"); System.Console.WriteLine("C# Programming!"); System.Console.WriteLine("Le Trong Nghia"); System.Console.ReadLine(); } } }

lab.docx

Embed Size (px)

Citation preview

Page 1: lab.docx

1. MODULE 11.1 Bài 1:

Code:namespace Bai_1{ class Program { static void Main(string[] args) { System.Console.WriteLine("Welcome to"); System.Console.WriteLine("C# Programming!"); System.Console.WriteLine("Le Trong Nghia"); System.Console.ReadLine(); } }}

Page 2: lab.docx

1.2 Bài 2:

Code: namespace Bai_2{ class Program { static void Main(string[] args) { string ten="Le Trong Nghia"; System.Console.WriteLine("Hello " + ten); System.Console.ReadLine();

} }}

Page 3: lab.docx

1.3 Bài 3:

Codenamespace Bai_3{ class Program { static void Main(string[] args) { //gia0i phương trình bậc nhấ7t Console.Write("Nhap vao he so a :"); int a = int.Parse(Console.ReadLine()); Console.Write("Nhap vao he so b :"); int b = int.Parse(Console.ReadLine()); if (a == 0) if (b == 0) Console.WriteLine("Phuong trinh co vo so nghiem"); else Console.WriteLine("Phuong trinh vo nghiem "); else { int ketqua = -b / a; Console.WriteLine("Phuong trinh co nghiem la x = {0}", ketqua.ToString()); } Console.ReadKey();

} } }

Page 4: lab.docx

1.4 Bài 4:

Code namespace Bai_4{ class Program { static void Main(string[] args) {

//gia0i phương trình bậc 2 double a, b, c, x1, x2, dt; Console.Write("\n Nhap cac he so cua phuong trinh:\n"); Console.Write(" a = "); a = Convert.ToInt32(Console.ReadLine()); Console.Write(" b = "); b = Convert.ToInt32(Console.ReadLine()); Console.Write(" c = "); c = Convert.ToInt32(Console.ReadLine());

if (a == 0) { if (b == 0) { if (c == 0) { Console.Write(" >> Phuong trinh co vo so nghiem."); } else Console.Write(" >> Phuong trinh vo nghiem."); } else {

Page 5: lab.docx

x1 = -c / b; Console.Write(" >> Phuong trinh co mot nghiem: X1 = "); Console.Write(x1); } } else { dt = b * b - 4 * a * c; if (dt < 0) { Console.Write(" >> Phuong trinh vo nghiem."); } else { if (dt == 0) { x1 = -b / (2 * a); } else { Console.WriteLine(" >> Phuong trinh co hai nghiemphan biet:\n"); x1 = (-Math.Sqrt(dt) - b) / (2 * a); x2 = (Math.Sqrt(dt) - b) / (2 * a); Console.Write("\tX1 = "); Console.Write(x1); Console.WriteLine(); Console.Write("\tX2 = "); Console.Write(x2);

} }

} Console.ReadLine(); } }}

1.5 Bài 5:

Page 6: lab.docx

1.6 Bài 6:

Code

namespace Bai_6{ class Program { public void Cong() { int so1, so2; Console.WriteLine("Nhap so thu nhat: ");

Page 7: lab.docx

so1 = int.Parse(Console.ReadLine());

Console.WriteLine("Nhap so thu hai: "); so2 = int.Parse(Console.ReadLine());

Console.WriteLine("Ket qua = " + (so1 + so2)); } public void Tru() { int so1, so2;

Console.WriteLine("Nhap so thu nhat: "); so1 = int.Parse(Console.ReadLine());

Console.WriteLine("Nhap so thu hai: "); so2 = int.Parse(Console.ReadLine());

Console.WriteLine("Ket qua = " + (so1 - so2)); } public void Nhan() { int so1, so2;

Console.WriteLine("Nhap so thu nhat: "); so1 = int.Parse(Console.ReadLine());

Console.WriteLine("Nhap so thu hai: "); so2 = int.Parse(Console.ReadLine());

Console.WriteLine("Ket qua = " + (so1 * so2)); } public void Chia() { int so1, so2;

Console.WriteLine("Nhap so thu nhat: "); so1 = int.Parse(Console.ReadLine());

Console.WriteLine("Nhap so thu hai: "); so2 = int.Parse(Console.ReadLine());

Console.WriteLine("Ket qua = " + (so1 / so2)); }

static void Main(string[] args) { Program prg = new Program(); int choice = 0;

do { Console.WriteLine("-------Menu-------"); Console.WriteLine("1.Tong a + b"); Console.WriteLine("2.Hieu a - b "); Console.WriteLine("3.Hieu a * b "); Console.WriteLine("4.Hieu a / b "); Console.WriteLine("5.Thoat"); choice = int.Parse(Console.ReadLine());

Page 8: lab.docx

switch (choice) { case 1: prg.Cong(); break; case 2: prg.Tru(); break; case 3: prg.Nhan(); break; case 4: prg.Chia(); break; case 5: break;

default: Console.WriteLine("mời bạn chọn"); break; }

} while (choice != 5); } }}

2. MODULE 2:2.1 Bài 1:

Page 9: lab.docx

Code:

namespace Bai_1{ public partial class frmModule2_Bai1 : Form { public frmModule2_Bai1() { InitializeComponent(); }//hàm xư0 lý nút thoát private void btnthoat_Click(object sender, EventArgs e) { this.Close(); }//hàm xư0 lý nút xóa private void btnclear_Click(object sender, EventArgs e) { txtten.Clear(); txtngaysinh.Clear(); txtten.Focus(); }//hàm xư0 lý thoát form private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r; r = MessageBox.Show("Do you want exit?", "Ex1", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.No)

Page 10: lab.docx

e.Cancel = true; }//hàm xư0 lý nút hiể0n thị private void btnshow_Click(object sender, EventArgs e) { int age; string s; s = "My name is:" + txtten.Text + "\n"; age = DateTime.Now.Year - Convert.ToInt32(txtngaysinh.Text); s = s + "age:" + age.ToString(); MessageBox.Show(s); }//hàm kiể0m tra nhập ngày sinh private void txtngaysinh_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else this.errorProvider1.Clear(); }//hàm kiể0m tra nhập tển private void txtten_Leave(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Trim().Length == 0) this.errorProvider1.SetError(ctr, "Ban phai nhap ten"); else this.errorProvider1.Clear(); }

}}

2.2 Bài 2:

Page 11: lab.docx

Code:

namespace Bai_2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }//hàm xư0 lý thoát form private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r; r=MessageBox.Show("Do you want exit","Ex1", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if(r==DialogResult.No) e.Cancel=true; }//hàm xư0 lý nút chuyể0n vn to usd private void btnvntousd_Click(object sender, EventArgs e) { double a = double.Parse(txttien.Text); double kq; kq = Math.Round( a/ 17861); this.lblketqua.Text += kq; }//hàm xư0 lý nút chuyể0n vn to eur private void btnvntoeur_Click(object sender, EventArgs e) { double b = double.Parse(txttien.Text); double kq; kq = Math.Round(b / 27043); this.lblketqua.Text += kq; }//hàm xư0 lý nút chuyể0n usd to vn private void btnUSDtoVN_Click(object sender, EventArgs e) { double c = double.Parse(txttien.Text); double kq; kq = Math.Round(c * 17861); this.lblketqua.Text += kq; }//hàm xư0 lý nút chuyể0n eur to vn private void btneurtovn_Click(object sender, EventArgs e) { double d = double.Parse(txttien.Text); double kq; kq = Math.Round(d * 27043); this.lblketqua.Text += kq; }//hàm xư0 lý nhập tiểdn private void txttien_TextChanged(object sender, EventArgs e) {

Page 12: lab.docx

Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else this.errorProvider1.Clear(); }//hàm xư0 lý kể7t text qua0 private void lblketqua_Click(object sender, EventArgs e) { lblketqua.Text = ""; } }}

2.3 Bài 3:

Code: namespace Bai_3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e) {

} //hàm xư0 lý thoát form private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r = MessageBox.Show("Ban chac chan thoat", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question,

Page 13: lab.docx

MessageBoxDefaultButton.Button1); if (r == DialogResult.No) e.Cancel = true; }

int flag = 0;//hàm xư0 lý text nhập a private void txtNhapA_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else { this.errorProvider1.Clear(); flag = 1; }

}//hàm xư0 lý text nhập b private void txtNhapB_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else { this.errorProvider1.Clear(); if (flag == 1) { btnTinh.Enabled = true; } } }//hàm xư0 lý nút xóa private void btnXoa_Click(object sender, EventArgs e) { txtNhapA.Clear(); txtNhapB.Clear(); txtKetqua.Clear(); btnTinh.Enabled = false; btnXoa.Enabled = false; }//hàm xư0 lý nút tính private void btnTinh_Click(object sender, EventArgs e) { double a, b; a = Convert.ToDouble(txtNhapA.Text); b = Convert.ToDouble(txtNhapB.Text); if (a == 0) { txtKetqua.Text = "Phương trình vô nghiệm"; btnXoa.Enabled = true;

Page 14: lab.docx

btnTinh.Enabled = false; } else { if (b == 0) { txtKetqua.Text = "Phương trình có vô sô7 nghiệm"; btnXoa.Enabled = true; btnTinh.Enabled = false; } else { txtKetqua.Text = "Phương trình có nghiệm: " + Math.Round(b / a, 2); btnXoa.Enabled = true; btnTinh.Enabled = false; } } } //hàm xư0 lý nút thoát private void btnThoat_Click(object sender, EventArgs e) { this.Close(); }

}}

2.4 Bài 4:

Code:

namespace Bai_4{

Page 15: lab.docx

public partial class Form1 : Form { public Form1() { InitializeComponent(); } //hàm xư0 lý nút cộng private void btnCong_CheckedChanged(object sender, EventArgs e) { lblKetQua.Text = Convert.ToString(Convert.ToInt32(txtNhapSo1.Text) + Convert.ToInt32(txtNhapSo2.Text)); }

//hàm kiể0m tra text nhập sô7 1 private void txtNhapSo1_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else { this.errorProvider1.Clear(); } } //hàm kiể0m tra text nhập sô7 2 private void txtNhapSo2_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "Khong phai so"); else { this.errorProvider1.Clear(); } } //hàm xư0 lý nút trừ private void btnTru_CheckedChanged(object sender, EventArgs e) { lblKetQua.Text = Convert.ToString(Convert.ToInt32(txtNhapSo1.Text) - Convert.ToInt32(txtNhapSo2.Text)); } //hàm xư0 lý nút nhấn private void btnNhan_CheckedChanged(object sender, EventArgs e) { lblKetQua.Text = Convert.ToString(Convert.ToInt32(txtNhapSo1.Text) * Convert.ToInt32(txtNhapSo2.Text)); } //hàm xư0 lý nút chia

Page 16: lab.docx

private void btnChia_CheckedChanged(object sender, EventArgs e) { lblKetQua.Text = Convert.ToString(Convert.ToInt32(txtNhapSo1.Text) / Convert.ToInt32(txtNhapSo2.Text)); } }

2.5 Bài 5:

Code:

namespace Bai_5

Page 17: lab.docx

{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

//hàm xư0 lý nút thoát

private void btnthoat_Click(object sender, EventArgs e) { this.Close(); } //hàm xư0 lý thoát form private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r; r = MessageBox.Show("bạn có muô7n thoát", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.No) e.Cancel = true; } //hàm gán text nhập tển vào label lập trình private void txtNhapTen_TextChanged(object sender, EventArgs e) { lblLapTrinh.Text = txtNhapTen.Text; } //hàm xư0 lý radiobutton red private void radred_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Red; txtNhapTen.ForeColor = Color.Red; } //hàm xư0 lý radiobutton green private void radgreen_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Green; txtNhapTen.ForeColor = Color.Green; } //hàm xư0 lý radiobutton blu private void radblue_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Blue; txtNhapTen.ForeColor = Color.Blue; } //hàm xư0 lý radiobutton black private void radblack_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.ForeColor = Color.Black; txtNhapTen.ForeColor = Color.Black; } //hàm xư0 lý checkbox bold private void checkBold_CheckedChanged(object sender, EventArgs e) {

Page 18: lab.docx

lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size, lblLapTrinh.Font.Style ^ FontStyle.Bold); } //hàm xư0 lý checkbox italic private void chkItalic_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size, lblLapTrinh.Font.Style ^ FontStyle.Italic); } //hàm xư0 lý checkbox gạch chấn private void chkGachChan_CheckedChanged(object sender, EventArgs e) { lblLapTrinh.Font = new Font(lblLapTrinh.Font.Name, lblLapTrinh.Font.Size, lblLapTrinh.Font.Style ^ FontStyle.Underline); } }}

2.6 Bài 6:

Page 19: lab.docx

Code:

namespace Bai_6{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //hàm xư0 lý nút thoát private void btnThoat_Click(object sender, EventArgs e) { this.Close(); } //hàm xư0 lý thoát form private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult r; r = MessageBox.Show("Do you want exit", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.No) e.Cancel = true; }

//hàm xư0 lý radio button time new roman private void radTimeNewRoman_CheckedChanged(object sender, EventArgs e) { FontFamily f = new FontFamily("Times New Roman"); txtFont.Font = new Font(f, txtFont.Font.Size); } //hàm xư0 lý radio button arial private void radArial_CheckedChanged(object sender, EventArgs e) { FontFamily f = new FontFamily("Arial"); txtFont.Font = new Font(f, txtFont.Font.Size); } //hàm xư0 lý radio button tahoma private void radTahoma_CheckedChanged(object sender, EventArgs e) { FontFamily f = new FontFamily("Tahoma"); txtFont.Font = new Font(f, txtFont.Font.Size); } //hàm xư0 lý radio button courier new private void radCourierNew_CheckedChanged(object sender, EventArgs e) { FontFamily f = new FontFamily("Courier New"); txtFont.Font = new Font(f, txtFont.Font.Size); }

Page 20: lab.docx

}}

3. CHƯƠNG 3:3.1 Bài 1:

Code:

namespace la_co{ public partial class frmModule3_bai1 : Form { public frmModule3_bai1() { InitializeComponent(); }

private void radbtnVietNam_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("vietnam.jpg"); }

private void radbtnUsd_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("usa.jpg"); }

private void radbtnItalian_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("y.jpg"); }

private void radbtnPhilippine_CheckedChanged(object sender, EventArgs e)

Page 21: lab.docx

{ pictureBox1.Image = Image.FromFile("philip.jpg"); }

}}

3.2 Bài 2:

Page 22: lab.docx

Code:

namespace uocso{ public partial class frmModule3_bai2 : Form { public frmModule3_bai2() { InitializeComponent(); } /// <summary> /// hàm cho phép kiể0m tra nhập vào là chữ hay sô7 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtNhapSo_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "bạn pha0i nhập sô7"); else this.errorProvider1.Clear(); } /// <summary> /// hàm xư0 lý nút cập nhật /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCapNhat_Click(object sender, EventArgs e) { cbo1.Text = txtNhapSo.Text; cbo1.Items.Add(txtNhapSo.Text); txtNhapSo.Text = ""; txtNhapSo.Focus(); } /// <summary> /// hàm xư0 lý nút thoát /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnThoat_Click(object sender, EventArgs e) { this.Close(); } /// <summary> /// hàm hiể0n thị ước sô7 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbo1_SelectedIndexChanged(object sender, EventArgs e) { lstDanhSachUocSo.Items.Clear(); double n = double.Parse(cbo1.Text.ToString()); for (int i = 0; i <= n; i++) {

Page 23: lab.docx

if(n%i==0) lstDanhSachUocSo.Items.Add(i); } } /// <summary> /// hàm tính tô0ng /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTongUocSo_Click(object sender, EventArgs e) { double tong = 0; double n = double.Parse(cbo1.Text.ToString()); for (int i = 0; i <= n; i++) { if (n % i == 0) tong = tong + i; } MessageBox.Show("tô0ng các ước sô7" + tong); } /// <summary> /// sô7 lượng các ước sô7 chẵrn /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSoLuongUocSoChan_Click(object sender, EventArgs e) { double tong = 0; double n = double.Parse(cbo1.Text.ToString()); for (int i = 0; i <= n; i++) { if (n % i == 0 && i%2==0) { tong = tong + 1; } } MessageBox.Show("sô7 lượng các ước sô7 chẵrn " +tong); } /// <summary> /// hàm tính sô7 lượng các ước sô7 nguyển tô7 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSoLuongUocSoNguyenTo_Click(object sender, EventArgs e) { double tong = 0; double n = double.Parse(cbo1.Text.ToString()); for (int i = 1; i <= n; i++) { if (n % i == 0) { if (i <= 2) tong++; else { int a = 0; for (int j = 2; j < i; j++) {

Page 24: lab.docx

if (i % j != 0) a = 1; } if (a == 1) tong++; } } } MessageBox.Show("tô0ng sô7 lượng các ước sô7 nguyển tô7 "+tong);

}

}}

3.3 Bài 3:

Page 25: lab.docx
Page 26: lab.docx
Page 27: lab.docx
Page 28: lab.docx

Code:

namespace xulylistbox{ public partial class frmModule3_bai3 : Form { public frmModule3_bai3() { InitializeComponent(); } /// <summary> /// hàm kiể0m tra text nhập vào /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtNhap_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Length > 0 && !char.IsDigit(ctr.Text[ctr.Text.Length - 1])) this.errorProvider1.SetError(ctr, "bạn pha0i nhập sô7"); else this.errorProvider1.Clear();

} /// <summary> /// hàm xư0 lý nút nhập /// </summary>

Page 29: lab.docx

/// <param name="sender"></param> /// <param name="e"></param> private void btnNhap_Click(object sender, EventArgs e) { lstHienThi.Items.Add(txtNhap.Text.ToString()); txtNhap.Clear(); txtNhap.Focus();

} /// <summary> /// hàm xư0 lý tô0ng các phấdn tư0 trong list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTong_Click(object sender, EventArgs e) { double n, tong=0,a; n = lstHienThi.Items.Count; for (int i = 0; i < n; i++) { a = Convert.ToDouble(lstHienThi.Items[i]); tong = tong + a; } MessageBox.Show("tong cac phan tu trong list: "+tong); } /// <summary> /// hàm cho phép xóa phấdn tư0 đấdu và cuô7i /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnXoaDauCuoi_Click(object sender, EventArgs e) { int n = Convert.ToInt32(lstHienThi.Items.Count); if (n < 2) { MessageBox.Show("không thể0 xóa");

} else { lstHienThi.Items.RemoveAt(n-1); lstHienThi.Items.RemoveAt(0); lstHienThi.Update(); } } /// <summary> /// hàm cho phép xóa phấdn tư0 đang chọn /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnXoaChon_Click(object sender, EventArgs e) { lstHienThi.Items.Remove(lstHienThi.SelectedItem); } /// <summary> /// hàm cho phép tẵng môri phấdn tư0 lển 2 /// </summary> /// <param name="sender"></param>

Page 30: lab.docx

/// <param name="e"></param> private void btnTang_Click(object sender, EventArgs e) { int n, a; n = lstHienThi.Items.Count; for (int i = 0; i < n; i++) { a = Convert.ToInt32(lstHienThi.Items[i]); lstHienThi.Items[i] = a + 2; } } /// <summary> /// hàm cho phép tẵng lển bình phương /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnBinhPhuong_Click(object sender, EventArgs e) { int n,a; n = lstHienThi.Items.Count; for (int i = 0; i < n; i++) { a = Convert.ToInt32(lstHienThi.Items[i]); lstHienThi.Items[i] = a * a; } } /// <summary> /// hàm cho phép chọn sô7 chẵrn /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnChonChan_Click(object sender, EventArgs e) { int n,a; n = lstHienThi.Items.Count; for (int i = 0; i < n; i++) { a = Convert.ToInt32(lstHienThi.Items[i]); if (a % 2 == 0) lstHienThi.SelectedIndex=i;

} } /// <summary> /// hàm cho phép chọn sô7 le0 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnChonLe_Click(object sender, EventArgs e) { int n, a; n = lstHienThi.Items.Count; for (int i = 0; i < n; i++) { a = Convert.ToInt32(lstHienThi.Items[i]); if (a % 2 != 0) lstHienThi.SelectedIndex = i; }

Page 31: lab.docx

} /// <summary> /// hàm kể7t thúc /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } }}

3.4 Bài 4:

Page 32: lab.docx
Page 33: lab.docx

Code:

namespace danhsachsinhvien{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// hàm kiể0m tra nhập tển /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtHoTen_TextChanged(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Trim().Length == 0) this.errorProvider1.SetError(ctr, "bạn chưa nhập tển"); else this.errorProvider1.Clear(); } /// <summary> /// hàm xư0 lý nút cập nhật

Page 34: lab.docx

/// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCapNhat_Click(object sender, EventArgs e) { if (txtHoTen.Text == "") { MessageBox.Show("bạn chưa nhập họ tển"); } else { if (cboLop.Text == "A") { lstLopA.Text = txtHoTen.Text; lstLopA.Items.Add(txtHoTen.Text); } else { lstLopB.Text = txtHoTen.Text; lstLopB.Items.Add(txtHoTen.Text); } txtHoTen.Text = ""; txtHoTen.Focus(); } }

private void btnChuyenTenChonSangLopB_Click(object sender, EventArgs e) { for (int i = 0; i < lstLopA.SelectedItems.Count; i++) { lstLopB.Items.Add(lstLopA.SelectedItems[i]); } for (int j = lstLopA.SelectedItems.Count - 1; j >= 0; j--) { lstLopA.Items.Remove(lstLopA.SelectedItems[j]); } }

private void btnChuyenTenChonSangLopA_Click(object sender, EventArgs e) { for (int i = 0; i < lstLopB.SelectedItems.Count; i++) { lstLopA.Items.Add(lstLopB.SelectedItems[i]); } for (int j = lstLopB.SelectedItems.Count -1; j >= 0; j--) { lstLopB.Items.Remove(lstLopB.SelectedItems[j]); } }

private void btnChuyenHetSangLopB_Click(object sender, EventArgs e) { for (int i = 0; i < lstLopA.Items.Count; i++) { lstLopB.Items.Add(lstLopA.Items[i]); } for (int j = lstLopA.Items.Count - 1; j >= 0; j--) {

Page 35: lab.docx

lstLopA.Items.Remove(lstLopA.Items[j]); } }

private void btnChuyenHetSangLopA_Click(object sender, EventArgs e) { for (int i = 0; i < lstLopB.Items.Count; i++) { lstLopA.Items.Add(lstLopB.Items[i]); } for (int j = lstLopB.Items.Count - 1; j >= 0; j--) { lstLopB.Items.Remove(lstLopB.Items[j]); } }

private void btnXoa_Click(object sender, EventArgs e) { for (int i = 0; i < lstLopA.SelectedItems.Count; i++) { lstLopA.Items.Remove(lstLopA.SelectedItems[i]); } for (int j = 0; j < lstLopB.SelectedItems.Count; j++) { lstLopB.Items.Remove(lstLopB.SelectedItems[j]); } }

private void btnKetThuc_Click(object sender, EventArgs e) { this.Close(); } }}

Page 36: lab.docx

3.5 Bài 5:

Code: namespace Bai_5{ public partial class Form1 : Form { public Form1() { InitializeComponent();

Page 37: lab.docx

} //hàm xư0 lý thoát form private void btnThoat_Click(object sender, EventArgs e) { DialogResult r; r = MessageBox.Show("Bạn có muô7n thoát ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.Yes) this.Close(); }

//Hàm xư0 lý hu0y checklistbox void HuyCheckListBox(CheckedListBox lst) { while (lst.CheckedIndices.Count > 0) { // lst.CheckedIndices[0]==> lay ra vi tri trong list lst.SetItemChecked(lst.CheckedIndices[0], false); } } //hàm xư0 lý nút hu0y private void btnHuy_Click(object sender, EventArgs e) { HuyCheckListBox(chklstMonHoc); txtMSSV.Clear(); txtHoTen.Clear(); rad1.Checked = false; rad2.Checked = false; rad3.Checked = false; rad4.Checked = false; cboNienKhoa.Text = ""; cboLop.Text = ""; }

//Dang ky mon hoc string CacMonHocDangChon(CheckedListBox lst) { int count = 0; string str = ""; foreach (var item in lst.CheckedItems) { count++; str += count.ToString() + ". " + item.ToString() + "\n"; } return str; } //hàm xư0 lý nút đẵng ký môn học private void btnDangKy_Click(object sender, EventArgs e) { string Dangky=""; Dangky = "Sinh viển:"+txtHoTen.Text + " " + "\nLớp: " + cboLop.SelectedItem.ToString() + "\nNiển Khóa: " + cboNienKhoa.SelectedItem.ToString() + "\nĐã đẵng ký các môn học sau:\n" + CacMonHocDangChon(chklstMonHoc); MessageBox.Show(Dangky); }

Page 38: lab.docx

} }

4. MODULE 44.1 Bài 1:

Code:

namespace Bai_1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //hàm xư0 lý các trackbar void ProgramInit() { int nRed = trkRed.Value; int nGreen = trkGreen.Value; int nBlue = trkBlue.Value; lblRed.Text = "R = " + nRed; lblGreen.Text = "G = " + nGreen; lblBlue.Text = "B = " + nBlue; Color color = Color.FromArgb(nRed, nGreen, nBlue); pnlColor.BackColor = color; } private void Form1_Load(object sender, EventArgs e) { ProgramInit(); } }}

4.2 Bài 2:

Page 39: lab.docx

Code:

namespace Bai_2{ partial class FrmModule4_Bai2 { private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

//khơ0i tạo các biể7n private void InitializeComponent() { this.grpYear = new System.Windows.Forms.GroupBox(); this.lblToday = new System.Windows.Forms.Label(); this.lblDay = new System.Windows.Forms.Label(); this.lblMonth = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); this.btnShow = new System.Windows.Forms.Button(); this.nudDay = new System.Windows.Forms.NumericUpDown(); this.lblChonNgay = new System.Windows.Forms.Label(); this.nudMonth = new System.Windows.Forms.NumericUpDown(); this.lblChonThang = new System.Windows.Forms.Label();

Page 40: lab.docx

this.nudYear = new System.Windows.Forms.NumericUpDown(); this.lblChonNam = new System.Windows.Forms.Label(); this.grpYear.SuspendLayout(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.nudDay)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudMonth)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudYear)).BeginInit(); this.SuspendLayout(); // grpYear this.grpYear.Controls.Add(this.lblToday); this.grpYear.Controls.Add(this.lblDay); this.grpYear.Controls.Add(this.lblMonth); this.grpYear.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.grpYear.Location = new System.Drawing.Point(291, 29); this.grpYear.Name = "grpYear"; this.grpYear.Size = new System.Drawing.Size(221, 205); this.grpYear.TabIndex = 3; this.grpYear.TabStop = false; this.grpYear.Text = "Lịch Nẵm 1993"; // lblToday this.lblToday.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblToday.Location = new System.Drawing.Point(20, 168); this.lblToday.Name = "lblToday"; this.lblToday.Size = new System.Drawing.Size(180, 25); this.lblToday.TabIndex = 2; this.lblToday.Text = "Thứ tư"; this.lblToday.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // lblDay this.lblDay.Font = new System.Drawing.Font("Microsoft Sans Serif", 72F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblDay.Location = new System.Drawing.Point(20, 51); this.lblDay.Name = "lblDay"; this.lblDay.Size = new System.Drawing.Size(180, 103); this.lblDay.TabIndex = 1; this.lblDay.Text = "10"; this.lblDay.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // lblMonth this.lblMonth.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblMonth.Location = new System.Drawing.Point(20, 21);

Page 41: lab.docx

this.lblMonth.Name = "lblMonth"; this.lblMonth.Size = new System.Drawing.Size(180, 30); this.lblMonth.TabIndex = 0; this.lblMonth.Text = "Tháng mười"; this.lblMonth.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // panel1 this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.Controls.Add(this.btnShow); this.panel1.Controls.Add(this.nudDay); this.panel1.Controls.Add(this.lblChonNgay); this.panel1.Controls.Add(this.nudMonth); this.panel1.Controls.Add(this.lblChonThang); this.panel1.Controls.Add(this.nudYear); this.panel1.Controls.Add(this.lblChonNam); this.panel1.Location = new System.Drawing.Point(32, 29); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(239, 205); this.panel1.TabIndex = 2; // btnShow this.btnShow.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnShow.Location = new System.Drawing.Point(31, 151); this.btnShow.Name = "btnShow"; this.btnShow.Size = new System.Drawing.Size(173, 30); this.btnShow.TabIndex = 3; this.btnShow.Text = "&Xem Lịch"; this.btnShow.UseVisualStyleBackColor = true; this.btnShow.Click += new System.EventHandler(this.btnShow_Click); // nudDay this.nudDay.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.nudDay.Location = new System.Drawing.Point(115, 107); this.nudDay.Maximum = new decimal(new int[] { 31,0,0,0}); this.nudDay.Minimum = new decimal(new int[] { 1,0,0,0}); this.nudDay.Name = "nudDay"; this.nudDay.Size = new System.Drawing.Size(86, 22); this.nudDay.TabIndex = 2; this.nudDay.Value = new decimal(new int[] { 10,0,0,0}); // lblChonNgay this.lblChonNgay.AutoSize = true;

Page 42: lab.docx

this.lblChonNgay.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblChonNgay.Location = new System.Drawing.Point(28, 109); this.lblChonNgay.Name = "lblChonNgay"; this.lblChonNgay.Size = new System.Drawing.Size(81, 16); this.lblChonNgay.TabIndex = 3; this.lblChonNgay.Text = "Chọn Ngày: "; // nudMonth this.nudMonth.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.nudMonth.Location = new System.Drawing.Point(115, 63); this.nudMonth.Maximum = new decimal(new int[] { 12,0,0,0}); this.nudMonth.Minimum = new decimal(new int[] { 1,0,0,0}); this.nudMonth.Name = "nudMonth"; this.nudMonth.Size = new System.Drawing.Size(86, 22); this.nudMonth.TabIndex = 1; this.nudMonth.Value = new decimal(new int[] { 10,0,0,0}); this.nudMonth.ValueChanged += new System.EventHandler(this.nudMonth_ValueChanged); // lblChonThang this.lblChonThang.AutoSize = true; this.lblChonThang.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblChonThang.Location = new System.Drawing.Point(25, 65); this.lblChonThang.Name = "lblChonThang"; this.lblChonThang.Size = new System.Drawing.Size(84, 16); this.lblChonThang.TabIndex = 1; this.lblChonThang.Text = "Chọn Tháng:"; // nudYear this.nudYear.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.nudYear.Location = new System.Drawing.Point(115, 20); this.nudYear.Maximum = new decimal(new int[] { 2099,0,0,0}); this.nudYear.Minimum = new decimal(new int[] { 1900,0,0,0}); this.nudYear.Name = "nudYear"; this.nudYear.Size = new System.Drawing.Size(86, 22); this.nudYear.TabIndex = 0; this.nudYear.Value = new decimal(new int[] { 1993,0,0,0}); this.nudYear.ValueChanged += new System.EventHandler(this.nudYear_ValueChanged); // lblChonNam

Page 43: lab.docx

this.lblChonNam.AutoSize = true; this.lblChonNam.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblChonNam.Location = new System.Drawing.Point(35, 22); this.lblChonNam.Name = "lblChonNam"; this.lblChonNam.Size = new System.Drawing.Size(74, 16); this.lblChonNam.TabIndex = 0; this.lblChonNam.Text = "Chọn Nẵm:"; // FrmModule4_Bai2 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(545, 262); this.Controls.Add(this.grpYear); this.Controls.Add(this.panel1); this.Name = "FrmModule4_Bai2"; this.Text = "Module 4_Bai 2"; this.Load += new System.EventHandler(this.Form1_Load); this.grpYear.ResumeLayout(false); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.nudDay)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nudMonth)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nudYear)).EndInit(); this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.GroupBox grpYear; private System.Windows.Forms.Label lblToday; private System.Windows.Forms.Label lblDay; private System.Windows.Forms.Label lblMonth; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button btnShow; private System.Windows.Forms.NumericUpDown nudDay; private System.Windows.Forms.Label lblChonNgay; private System.Windows.Forms.NumericUpDown nudMonth; private System.Windows.Forms.Label lblChonThang; private System.Windows.Forms.NumericUpDown nudYear; private System.Windows.Forms.Label lblChonNam;

}}

4.3 Bài 3:

Page 44: lab.docx

Code:

namespace Bai_3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //hàm xư0 lý chữ chạy private void timer1_Tick(object sender, EventArgs e) { lblTextChay.Left += 50; } }}

4.4 Bài 4:

Page 45: lab.docx

Code:

namespace Bai_4{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int nIndex = 0;

// hàm duyệt a0nh private void timer1_Tick(object sender, EventArgs e) { picShow.Image = imageList1.Images[nIndex]; nIndex++; if (nIndex == imageList1.Images.Count) nIndex = 0; }

private void Form1_Load(object sender, EventArgs e) { timer1.Enabled = true; timer1.Start(); }

private void picShow_Click(object sender, EventArgs e) { if (timer1.Enabled == true) timer1.Enabled = false; else

Page 46: lab.docx

timer1.Enabled = true; } }}

4.5 Bài 5:

Page 47: lab.docx

Code:

namespace Bai_5{ public partial class frmModul6_Bai5 : Form { public frmModul6_Bai5() { InitializeComponent(); } // hàm xư0 lý combobox country private void cboCountry_SelectedIndexChanged(object sender, EventArgs e) { string showcity = ""; showcity = cboCountry.Text; listBox1.Items.Clear(); switch (showcity) { case "Viet Nam": listBox1.Items.Add("Ho Chi Minh"); listBox1.Items.Add("Nha Trang"); listBox1.Items.Add("Ha Noi"); break; case "ThaiLand": listBox1.Items.Add("Pattaya"); listBox1.Items.Add("ChiengMai"); listBox1.Items.Add("Bangkok"); break; default: MessageBox.Show("Selection City"); break; } }

//hàm xư0 lý nút thoát private void btnExit_Click(object sender, EventArgs e) { DialogResult r; r = MessageBox.Show("Do you want exit", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.Yes) this.Close(); }

//hàm xư0 lý nút gơ0i private void btnSubmit_Click(object sender, EventArgs e) { String Submit = ""; Submit = "Name: " + txtName.Text + "\n" + "Birthday: " + msktxtBirthday.Text + "\n" + "Address: " + txtAddress.Text + "\n"

Page 48: lab.docx

+ "City: " + listBox1.SelectedItem.ToString() + "\n" + "Country: " + cboCountry.SelectedItem.ToString() + "\n" + "Qualification: " + listBox2.SelectedItem.ToString() + "\n" + "Phone: " + msktxtPhone.Text + "\n" + "Email: " + txtEmail.Text + "\n" + "Date of Joinning: " + dateTimePicker1.Text + "\n"; MessageBox.Show(Submit); } //hàm kiể0m tra text name private void txtName_Leave(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Trim().Length == 0) this.errorProvider1.SetError(ctr, "Ban phai nhap ten"); else this.errorProvider1.Clear(); } //hàm kiể0m tra text address private void txtAddress_Leave(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Trim().Length == 0) this.errorProvider1.SetError(ctr, "Ban phai nhap ten"); else this.errorProvider1.Clear(); } //hàm kiể0m tra text mail private void txtEmail_Leave(object sender, EventArgs e) { Control ctr = (Control)sender; if (ctr.Text.Trim().Length == 0) this.errorProvider1.SetError(ctr, "Ban phai nhap ten"); else this.errorProvider1.Clear(); } }}

4.6 Bài 6:

Page 49: lab.docx

Code:

namespace Bai_6{ public partial class Form1 : Form { public Form1() { InitializeComponent(); progressBar1.Minimum = 0; //bẵ7t đấdu progressBar1.Maximum = 30;//kể7t thúc

Page 50: lab.docx

progressBar1.Step = 1;//kích thước } #region Timer private void timer1_Tick(object sender, EventArgs e) { if (progressBar1.Value < progressBar1.Maximum) { progressBar1.PerformStep();//chạy thểm 1 bước nữa trển thanh } else { this.DialogResult = DialogResult.OK; } } #endregion #region OK private void btnOK_Click(object sender, EventArgs e) { timer1.Stop(); timer1.Enabled = false; progressBar1.Value = progressBar1.Maximum; this.DialogResult = DialogResult.OK; } #endregion }}

4.7 Bài 7:

4.8 Bài 8: