95
TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP THÀNH PHỐ HỒ CHÍ MINH KHOA CÔNG NGHỆ THÔNG TIN MÔN CÔNG NGHỆ DOTNET BÀI TẬP THỰC HÀNH GIẢNG VIÊN: PHẠM QUẢNG TRI TRỊNH NGỌC HƯNG 07712961 HUỲNH HỒNG PHÚC 07711661

Bao cao bai tap mon cong nghe dot net (C# 1) [email protected]

Embed Size (px)

DESCRIPTION

TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP THÀNH PHỐ HỒ CHÍ MINHKHOA CÔNG NGHỆ THÔNG TINMÔN CÔNG NGHỆ DOTNETBÀI TẬP THỰC HÀNHGIẢNG VIÊN: PHẠM QUẢNG TRI TRỊNH NGỌC HƯNG 07712961 HUỲNH HỒNG PHÚC 07711661TP.Hồ Chí Minh tháng 3 năm 2012Chương 2 Bài 1: Tạo giao diện như hình: nhập vào 2 số và 4 Radio, click vào Radio Cộng sẽ thực hiện phép Cộng 2 số và xuất kết quả vào Label Kết quả. Thực hiện tương tự cho các Radio còn lại. (kiểm tra dữ liệu nhập phải là số) Code:using using using using using using usi

Citation preview

Page 1: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

TR NG Đ I H C CÔNG NGHI P THÀNH PH HƯỜ Ạ Ọ Ệ Ố Ồ CHÍ MINH

KHOA CÔNG NGH THÔNG TINỆ

MÔN CÔNG NGH DOTNETỆ

BÀI T P TH C HÀNHẬ Ự

GI NG VIÊN: PH M QU NG TRIẢ Ạ Ả

TR NH NG C H NG 07712961Ị Ọ Ư

HUỲNH H NG PHÚC 07711661Ồ

Page 2: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

TP.H Chí Minh tháng 3 năm 2012ồ

Chương 2

Bài 1: Tạo giao diện như hình: nhập vào 2 số và 4 Radio, click vào Radio Cộng sẽ thực hiện phép Cộng 2 số và xuất kết quả vào Label Kết quả. Thực hiện tương tự cho các Radio còn lại. (kiểm tra dữ liệu nhập phải là số)

Code:

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 S2Basic_Controls{ public partial class Bai1_frmPhepTinh : Form { public Bai1_frmPhepTinh() { InitializeComponent(); }

private void rdbCong_CheckedChanged(object sender, EventArgs e) { flooat a, b, kq; if (rdbCong.Checked == true) { try { a = flooat.Parse(txtSoA.Text); b = flooat.Parse(txtSoB.Text); kq = a + b; txtKetQuaa.Text = kq.ToString(); } catch { txtKetQuaa.Text = "gia tri khong hop le"; txtSoA.Focus(); } } else txtKetQuaa.Text = ""; }

private void Bai1_PhepTinh_Load(object sender, EventArgs e) { txtSoA.Text = ""; txtSoB.Text = ""; txtKetQuaa.Text = "";

Page 3: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

rdbCong.Checked = rdbTru.Checked = rdbNhan.Checked = rdbChia.Checked = false; }

private void rdbCong_Leave(object sender, EventArgs e) { //rdbCong.Checked = false; }

private void rdbTru_CheckedChanged(object sender, EventArgs e) { flooat a, b, kq; if (rdbTru.Checked == true) { try { a = flooat.Parse(txtSoA.Text); b = flooat.Parse(txtSoB.Text); kq = a - b; txtKetQuaa.Text = kq.ToString(); } catch { txtKetQuaa.Text = "gia tri khong hop le"; txtSoA.Focus(); } } else txtKetQuaa.Text = ""; }

private void rdbNhan_CheckedChanged(object sender, EventArgs e) { flooat a, b, kq; if (rdbNhan.Checked == true) { try { a = flooat.Parse(txtSoA.Text); b = flooat.Parse(txtSoB.Text); kq = a * b; txtKetQuaa.Text = kq.ToString(); } catch { txtKetQuaa.Text = "gia tri khong hop le"; txtSoA.Focus(); } } else txtKetQuaa.Text = ""; }

private void rdbChia_CheckedChanged(object sender, EventArgs e) { flooat a, b, kq; if (rdbChia.Checked == true) { try { a = flooat.Parse(txtSoA.Text); b = flooat.Parse(txtSoB.Text); kq = a / b; txtKetQuaa.Text = kq.ToString(); }

Page 4: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

catch (DivideByZeroException) { txtKetQuaa.Text = "Loi chia cho 0"; //MessageBox.Show("Loi " + e.ToString()); txtSoB.Focus(); } catch { txtKetQuaa.Text = "gia tri khong hop le"; txtSoA.Focus(); } } else txtKetQuaa.Text = ""; } }}

Execution Result:

Bài 2: Viết chương trình dùng để định dạng LABEl TEXT theo các Checkbox và Radio button tương ứng (Font Style, Color).

Code:

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 S2Basic_Controls{ public partial class Bai2_frmDinhDangChu : Form { public Bai2_frmDinhDangChu() { InitializeComponent();

Page 5: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

}

private void chbRegular_CheckedChanged(object sender, EventArgs e) { //CheckBox chb = sender as CheckBox; if(chbRegular.Checked == true) lbText.Font = new Font(lbText.Font, lbText.Font.Style ^ FontStyle.Regular); }

private void chbBold_CheckedChanged(object sender, EventArgs e) { FontStyle re = lbText.Font.Style; if (chbBold.Checked == true) lbText.Font = new Font(lbText.Font, lbText.Font.Style ^ FontStyle.Bold); else lbText.Font = new Font(lbText.Font, FontStyle.Regular); }

private void chbItalic_CheckedChanged(object sender, EventArgs e) { if (chbItalic.Checked == true) lbText.Font = new Font(lbText.Font, lbText.Font.Style^FontStyle.Italic); else lbText.Font = new Font(lbText.Font, FontStyle.Regular); }

private void chbBandI_CheckedChanged(object sender, EventArgs e) { if (chbBandI.Checked == true) lbText.Font = new Font(lbText.Font, lbText.Font.Style^FontStyle.Bold | lbText.Font.Style^FontStyle.Italic); else lbText.Font = new Font(lbText.Font, FontStyle.Regular); }

private void rdbRed_CheckedChanged(object sender, EventArgs e) { if (rdbRed.Checked == true) lbText.ForeColor = Color.Red; }

private void rdbGreen_CheckedChanged(object sender, EventArgs e) { if (rdbGreen.Checked == true) lbText.ForeColor = Color.Green; }

private void rdbBlue_CheckedChanged(object sender, EventArgs e) { if (rdbBlue.Checked == true) lbText.ForeColor = Color.Blue; }

private void rdbAuto_CheckedChanged(object sender, EventArgs e) { Random rand = new Random(); int red = rand.Next(100); int gre = rand.Next(200); int blu = rand.Next(300); lbText.ForeColor = Color.FromArgb(red, gre, blu); }

private void Bai2_frmDinhDangChu_Load(object sender, EventArgs e) {

Page 6: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

} }}

Execution Result:

Bài 3: Viết chương trình dùng để định dạng TEXTBOX theo các font chữ tương ứng với từng Radio button.

Code:

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 S2Basic_Controls{ public partial class Bai3_frmDinhDangFontChu : Form { public Bai3_frmDinhDangFontChu() { InitializeComponent(); }

private void rdbTimesNewRoman_CheckedChanged(object sender, EventArgs e) { //txtMau.Font = new Font(rdbTimesNewRoman.Font.Name, 30f); //string s = rdbTimesNewRoman.Font.Name; txtMau.Font = new Font("Times New Roman", 30f);

Page 7: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

//MessageBox.Show(s); }

private void rdbArial_CheckedChanged(object sender, EventArgs e) { txtMau.Font = new Font(rdbArial.Font.Name, 30f); }

private void rdbTahoma_CheckedChanged(object sender, EventArgs e) { txtMau.Font = new Font(rdbTahoma.Font.Name, 30f); }

private void rdbCourierNew_CheckedChanged(object sender, EventArgs e) { txtMau.Font = new Font(rdbCourierNew.Font.Name, 30f); } }}

Execution Result:

Bài 4: Viết chương trình Quản lý thu tiền cho quán Café Sinh Viên như sau:

Khi nhóm khách hàng vào quán gọi café sẽ được chọn 1 trong các loại café. Giá cho từng loại café tương ứng như sau:Café đen: 20.000đCafé đá: 25.000đCafé sữa: 25.000đCafé sữa đá: 30.000đCafé kem: 35.000đ

Page 8: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Khi khách hàng gọi tính tiền, thu ngân sẽ nhập thông tin tên khách hàng và tùy vào loại café thì sẽ có giá tương ứng ( lưu ý: nếu khách hàng là Sinh Viên – check vào checkbox Sinh Viên – thì sẽ được giảm giá 20%). Cuối cùng thu ngân sẽ tính tổng số người trong nhóm và thanh toán tiền.

Chương trình thực hiện:

- Form_Load: con trỏ văn bản đặt vào ô tên khách hàng, các button TinhTien, NhapLai, ThanhToan bị mờ (enabled=false). Thực hiện nhập tên khách hàng (tên khách hàng không được để trống). Check vào checkbox Sinh Viên nếu khách hàng là Sinh Viên, chọn loại café mà khách hàng đã gọi. Khi nhập đầy đủ thông tin thì btnTinhTien có tác dụng.

- btnTinhTien _Click: thực hiện tính tiền cho khách hàng vừa nhập và hiển thị lên messagebox, đồng thời lưu lại thông tin tổng số tiền và tổng số lượt khách. btnNhapLai, btnThanhToan sáng lên sẵn sàng cho việc nhập khách hàng mới hoặc thanh toán tiền.

- btnNhapLai_Click: khởi tạo lại trạng thái ban đầu của Form, btnNhapLai bị mờ.

- btnThanhToan_Click: Ghi lại thông tin tổng số khách và tổng tiền Thanh toán vào các label tương ứng, đồng thời khởi tạo lại giá trị tổng số khách hàng =0, tổng tiền thanh toán =0, sẵn sàng cho việc nhập nhóm khách hàng mới. btnThanhToan bị mờ.

- btnThoat_Click: hỏi người dùng có chắc chắn thoát khỏi chương trình hay không? Yes: thoát, No: không.

Code:

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 S2Basic_Controls{ public partial class Bai4_frmCoffeee : Form {

Page 9: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

private flooat tongTien = 0; private int tongNguoi = 0; private flooat isSV = 0.2f; private readonly int cafeDen = 200000; private readonly int cafeDa = 250000; private readonly int cafeSua = 300000; private readonly int cafeSuaDa = 350000; private readonly int cafeKem = 350000;

public Bai4_frmCoffeee() { InitializeComponent(); }

private void txtHoTen_Leave(object sender, EventArgs e) { //if(btnThooat.Click==null) if (txtHoTen.Text == "") { MessageBox.Show("Ho ten khong the rong"); txtHoTen.Focus(); } }

private void Bai4_frmCoffeee_Load(object sender, EventArgs e) { btnTinhTien.Enabled = false; btnNhapLai.Enabled = false; btnThoanhToan.Enabled = false; }

private void rdbDen_CheckedChanged(object sender, EventArgs e) { RadioButtoon rdb = sender as RadioButtoon; if (rdb.Checked == true) { btnTinhTien.Enabled = true; } }

private void btnTinhTien_Click(object sender, EventArgs e) { tongNguoi++; if (rdbDa.Checked == true) tongTien += cafeDa; else if (rdbDen.Checked == true) tongTien += cafeDen; else if (rdbSua.Checked == true) tongTien += cafeSua; else if (rdbSuaDa.Checked == true) tongTien += cafeSuaDa; else tongTien += cafeKem; if (chbIsSinhVien.Checked == true) tongTien = tongTien - tongTien * isSV; //cho phep nhap tiep hoac thanh toan btnNhapLai.Enabled = true; btnThoanhToan.Enabled = true;

}

private void btnNhapLai_Click(object sender, EventArgs e) { txtHoTen.Text = "";

Page 10: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

chbIsSinhVien.Checked = false; Bai4_frmCoffeee_Load(null, null); txtHoTen.Focus(); }

private void btnThoanhToan_Click(object sender, EventArgs e) { txtSoNguoi.Text = tongNguoi.ToString(); txtTongTien.Text = tongTien.ToString(); tongTien = tongNguoi = 0; btnNhapLai_Click(null,null); }

private void btnThooat_Click(object sender, EventArgs e) { DialogResult r = MessageBox.Show("ban co muon thoat", "o thoat", MessageBoxButtoons.YesNo); if (r == DialogResult.Yes) this.Close(); }

private void txtHoTen_TextChanged(object sender, EventArgs e) {

} }}

Execution Result:

Bài 5: Viết chương trình kiểm tra sức khỏe của 1 người bằng cách nhập vào các thông số: họ tên (không được rỗng), chiều cao (cm),

Page 11: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

cân nặng (kg) dữ liệu vày phải là số. Tính cân số cân chuẩn của người đó theo công thức sau (tùy theo Nam hay Nữ):

Công thức tính cân nặng chuẩn:Cân nặng lý tưởng (kg) = T – 100 – (T –150/ N)(Trong đó: T là chiều cao (cm), N = 4 với Nam và N = 2 với Nữ.)

- btnTinh: Thực hiện tính số cân chuẩn theo công thức, sau đó hiển thị kết quả vào label kết quả như hình.

- btnXoa: trả lại trạng ban đầu của Form, cho phép nhập người mới

- btnThoat: thoát khỏi chương trình.

Code:

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 S2Basic_Controls{ public partial class Bai5_frmKiemTraSucKhoe : Form { private int heso;

public Bai5_frmKiemTraSucKhoe() { InitializeComponent(); }

private void txtCanNang_TextChanged(object sender, EventArgs e) {

}

private void txtChiSo_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar)) e.Handled = true; }

private void txtHoTen_Leave(object sender, EventArgs e) { if (txtHoTen.Text == "") { MessageBox.Show("Ho ten khong duoc de trong"); txtHoTen.Focus(); } }

private void btnTinh_Click(object sender, EventArgs e)

Page 12: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ int cannang = int.Parse(txtCanNang.Text); //MessageBox.Show(cannang.ToString()); int chieucao = int.Parse(txtChieuCao.Text); //MessageBox.Show(chieucao.ToString()); if (rdbNam.Checked == true) heso = 4; else heso = 3;

int trungbinh = (int)Math.Abs(chieucao - 100 - (chieucao - 150) / heso); lbNx1.Text = "Can Nang Li Tuong Cua Ban " + txtHoTen.Text + " la: " + trungbinh.ToString(); //MessageBox.Show(trungbinh.ToString()); if (cannang > trungbinh) lbNx2.Text = "ban dang thua can, hay giam :D"; else if (cannang < trungbinh) lbNx2.Text = "Ban dang thieu can, hay tang :D"; else lbNx2.Text = "ban vua can, dat. :D"; }

private void Bai5_frmKiemTraSucKhoe_Load(object sender, EventArgs e) { rdbNu.Checked = true; }

private void btnXoa_Click(object sender, EventArgs e) { txtHoTen.Text = ""; txtCanNang.Text = ""; txtChieuCao.Text = ""; lbNx2.Text = ""; lbNx1.Text = ""; txtHoTen.Focus(); }

private void btnThooat_Click(object sender, EventArgs e) { DialogResult r = MessageBox.Show("Ban co muon thoat", "Hop thoat", MessageBoxButtoons.YesNo); if (r == DialogResult.Yes) this.Close(); }

}}

Execution Result:

Page 13: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 6: Viết chương trình Quản lýThanh toán tiền phòng cho Khách sạn Thanh Thanh như sau:

Khi khách trả phòng, nhân viên kế toán sẽ thực hiện kiểm tra tên, địa chỉ, số ngày ở, loại phòng, tiện nghi và dịch vụ của khách. Sau đây là bảng giá:

o Loại phòng:Phòng đơn: 300.000đ/1 ngàyPhòng đôi: 350.000đ/1 ngàyPhòng ba: 400.000đ/1 ngày

o Tiện nghi: mỗi loại cộng thêm 10.000đo Dịch vụ:

Karaoke: 50.000đĂn sáng: 15.000đ/1 ngày

Cuối ngày Nhân viên kế toán sẽ thống kê tổng số lượt người trả phòng, tổng số tiền thu được trong ngày.

Chương trình thực hiện:

- Form_Load: con trỏ văn bản đặt vào ô tên khách hàng, các button TongKet, NhapMoi, ThanhToan bị mờ (enabled=false). Thực hiện nhập tên (tên khách không được để trống), địa chỉ, số ngày ở (number), check vào

Page 14: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

loại phòng, tiện nghi, dịch vụ mà khách đã thuê. Khi nhập đầy đủ thông tin thì btnThanhToan có tác dụng.

- btnThanhToan: thực hiện tính tiền cho khách vừa nhập và hiển thị lên label Thành Tiền, đồng thời lưu lại thông tin tổng số tiền và tổng số lượt khách. btnNhapMoi, btnTongKet sáng lên sẵn sàng cho việc nhập khách mới.

- btnNhapMoi: khởi tạo lại trạng thái ban đầu của Form, btnNhapMoi bị mờ.

- btnTôngKet: Ghi lại thông tin tổng số khách và tổng tiền Thanh toán vào các label tương ứng, đồng thời khởi tạo lại giá trị tổng số khách hàng =0, tổng tiền thanh toán =0. btnTongKet bị mờ.

- btnThoat_Click: hỏi người dùng có chắc chắn thoát khỏi chương trình hay không? Yes: thoát, No: không.

Code:

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 S2Basic_Controls{ public partial class Bai6_frmKhachSanThoanhThoanh : Form { //fieelds can thiet trong app private int tongNguoi = 0; private long tongTien = 0; private int[] giaPhong = { 3000000, 3500000, 4000000 }; private int giaTienNghi = 100000; private int[] giaDV = { 500000, 1500000 };

public enum LoaiPhong { phongDon, phongDoi, phongBa }

public enum DichVu { karaoke, ansang }

public Bai6_frmKhachSanThoanhThoanh() { InitializeComponent(); }

Page 15: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

private void txtHoTen_Leave(object sender, EventArgs e) { if (txtHoTen.Text == "") { MessageBox.Show("ten khong duoc rong"); txtHoTen.Focus(); } }

private void Bai6_frmKhachSanThoanhThoanh_Load(object sender, EventArgs e) { txtHoTen.Focus(); //txtDiaChi.Text = ""; //txtDiaChi.Select(); btnNhapMoi.Enabled = false; btnThoanhToan.Enabled = false; btnTongKet.Enabled = false; rdbPhongDon.Checked = true; //chbTiVi.Checked = true; //chbKaraoke.Checked = true; }

private void txtSoNgayO_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true; }

private void txtSoNgayO_Leave(object sender, EventArgs e) { btnThoanhToan.Enabled = true; //btnTongKet.Enabled = true; }

private void btnThoanhToan_Click(object sender, EventArgs e) { //string s = ((int)LoaiPhong.phongBa).ToString(); //MessageBox.Show(s); int songayo = int.Parse(txtSoNgayO.Text); long thanhtien; int gphong, gtn = 0, gdv = 0; //lay gia phong if (rdbPhongDon.Checked == true) gphong = giaPhong[(int)LoaiPhong.phongDon]; else if (rdbPhongDoi.Checked == true) gphong = giaPhong[(int)LoaiPhong.phongDoi]; else gphong = giaPhong[(int)LoaiPhong.phongBa];

//tien tien nghi if (chbTiVi.Checked == true||chbInternet.Checked==true||chbMayNcNong.Checked==true) gtn += giaTienNghi;

//lay tien dich vu if(chbKaraoke.Checked==true) gdv+=giaDV[(int)DichVu.karaoke]; if(chbAnSang.Checked==true) gdv+=giaDV[(int)DichVu.ansang]; thanhtien = (gphong + gtn + gdv) * songayo; //xuat thanh tien ra txtThoanhTien.Text = thanhtien.ToString() + " vnd";

//hien nhap moi btnNhapMoi.Enabled = true;

Page 16: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

btnTongKet.Enabled = true;

//cap nhat so nguoi o va tong tien tongNguoi++; tongTien += thanhtien; }

private void btnNhapMoi_Click(object sender, EventArgs e) { txtHoTen.Text = ""; txtDiaChi.Text = ""; txtSoNgayO.Text = ""; txtThoanhTien.Text = ""; lbSoLuotNguoi.Text = ""; lbTongSoTien.Text = ""; Bai6_frmKhachSanThoanhThoanh_Load(null, null); }

private void btnTongKet_Click(object sender, EventArgs e) { lbTongSoTien.Text = tongTien.ToString() + " vnd"; lbSoLuotNguoi.Text = tongNguoi.ToString() + " vnd";

//reset lai tong tien va tong nguoi tongNguoi = 0; tongTien = 0; //buttoon tong ket bi mo btnThoanhToan.Enabled = false; btnTongKet.Enabled = false; }

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

}}

Execution Result:

Chương 3:

Page 17: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 1:Viết chương trình thực hiện như sau ( xem hình bên dưới)

- Form_load: nhập số vào Textbox (nhớ kiểm tra dữ liệu

nhập)

- btnCapNhat: thêm số vừa nhập vào Combobox, đồng thời

xóa textbox nhập, đặt con trỏ lại textbox nhập.

- Khi người dùng chọn 1 số trong Combobox thì danh sách các

ước số của số này sẽ hiển thị vào Listbox bên phải tương

ứng.

- Khi nhấn Các button Tính: “Tổng các ước số”, “Số lượng các

ước số chẵn”, “Số lượng các ước số nguyên tố” thì sẽ hiển

thị thông tin tương ứng vào messagebox dựa vào các ước số

trên listbox.

btnThoat: thoát khỏi ứng dụng.

Code:

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 SW3Basic_Controls2{ public partial class Bai1_frmCombobox : Form { public Bai1_frmCombobox() { InitializeComponent(); }

private void txtNhap_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar)) e.Handled = true; }

private void btnNhap_Click(object sender, EventArgs e) { if(txtNhap.Text!="") cbbNhap.Items.Add(txtNhap.Text); txtNhap.Text = "";

Page 18: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

txtNhap.Focus(); }

private void Bai1_frmCombobox_Load(object sender, EventArgs e) { txtNhap.Text = ""; txtNhap.Focus(); }

private void cbbNhap_SelectedIndexChanged(object sender, EventArgs e) { int so = int.Parse((string)cbbNhap.SelectedItem); string[] items = TimUocSo(so); //foreach(string s in items) // MessageBox.Show(s); lsbUocSo.Items.Clear(); lsbUocSo.Items.AddRange(items); } private string[] TimUocSo(int value) { //dung tim uoc so cho gia tri truyen vao.

int item = 1, i = 0; string[] items=new string[100]; while (item < value) { if (value % item == 0) { items[i] = item.ToString(); i++; } item++; } items[i] = value.ToString(); string[] us = new string[i+1]; for (int j = 0; j <= i;j++ ) { us[j] = items[j]; } return us; }

private void btnTongUocSo_Click(object sender, EventArgs e) { long tong = 0; for (int i = 0; i < lsbUocSo.Items.Count; i++) { lsbUocSo.SetSelected(i, true); tong += int.Parse((string)lsbUocSo.SelectedItem); } //for (int i = 0; i < obj.Length - 1; i++) // tong += int.Parse(obj[i].ToString()); MessageBox.Show("TOng uoc so la: " + tong.ToString()); }

private void btnSoLuongUSChan_Click(object sender, EventArgs e) { int count = 0; for (int i = 0; i < lsbUocSo.Items.Count; i++) { lsbUocSo.SetSelected(i, true); if (int.Parse((string)lsbUocSo.SelectedItem) % 2 == 0)

Page 19: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

count++; } MessageBox.Show("So uoc so chan la: " + count.ToString()); }

private void btnSoLuongSoNTo_Click(object sender, EventArgs e) { int count = 0; for (int i = 0; i < lsbUocSo.Items.Count; i++) { lsbUocSo.SetSelected(i, true); if (LaSoNTo(int.Parse((string)lsbUocSo.SelectedItem))) { count++; MessageBox.Show((string)lsbUocSo.SelectedItem); } } MessageBox.Show("So uoc so la so nguyen to la: " + count.ToString()); } private bool LaSoNTo(int value) { int term = 2; while(term<value) { if (value % term == 0) return false; term++; } if (term == value) return true; //neu la so 1 return true; }

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

Execution Result:

Page 20: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 2: Viết chương trình thêm các phần tử vào listbox (listbox được

chọn nhiều phần tử) các số tự nhiên N được nhập từ textbox

Viết code cho các nút lệnh thực hiện các công việc sau:

- Tính tổng các phẩn tử trong Listbox, hiển thị lên

Messagebox.

- Xóa phần tử đầu và cuối của listbox.

- Xóa các phần tử đang chọn trong listbox.

- Tăng giá trị mỗi phần tử lên 2.

- Thay mỗi giá trị của mỗi phần tử bằng bình pương của

chính nó

- Thực hiện chọn các phần tử trong listbox là số chẵn.

- Thực hiện chọn các phần tử trong listbox là số lẻ.

Code:

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

Page 21: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

using System.Text;using System.Windows.Forms;

namespace SW3Basic_Controls2{ public partial class Bai2_frmListBox : Form { public Bai2_frmListBox() { InitializeComponent(); }

private void txtNhap_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true; }

private void btnNhap_Click(object sender, EventArgs e) { if (txtNhap.Text != "") lsbDuLieu.Items.Add(txtNhap.Text); txtNhap.Text = ""; //txtNhap.Select(); txtNhap.Focus(); }

private void Bai2_frmListBox_Load(object sender, EventArgs e) { txtNhap.Focus(); }

private void btnTongPhanTu_Click(object sender, EventArgs e) { //ICollection<object> ico = (ICollection<object>)lsbDuLieu.SelectedItems; //Array arr = ico as Array; //int tong = 0; //foreach (object o in arr) //{ // tong += int.Parse((string)o); //} //MessageBox.Show(tong.ToString());

int tong = 0; //clear cac item da duoc chon lsbDuLieu.SelectedItems.Clear(); for (int i = 0; i < lsbDuLieu.Items.Count; i++) { lsbDuLieu.SetSelected(i, true); tong += int.Parse((string)lsbDuLieu.SelectedItem); } //clear cac item da select de tinh lsbDuLieu.SelectedItems.Clear(); //show tong gia tri MessageBox.Show("tong phan tu trong listbox la: " + tong.ToString()); }

private void btnXoaDauVaCuoi_Click(object sender, EventArgs e) { //xoa phan tu cuoi if(lsbDuLieu.Items.Count>=1) lsbDuLieu.Items.RemoveAt(lsbDuLieu.Items.Count - 1);

Page 22: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

//xoa phan tu dau if(lsbDuLieu.Items.Count>=1) lsbDuLieu.Items.RemoveAt(0);

}

private void btnXoaDangChon_Click(object sender, EventArgs e) { //for (int i = 0; i < lsbDuLieu.Items.Count; i++) //{ // if(lsbDuLieu.s) //} int se = lsbDuLieu.SelectedIndex; //int [] see = new int(lsbDuLieu.SelectedIndices); while (se != -1) { MessageBox.Show(se.ToString()); lsbDuLieu.Items.RemoveAt(se); se = lsbDuLieu.SelectedIndex; } //MessageBox.Show(se.ToString()); }

private void btnTangLen2_Click(object sender, EventArgs e) { lsbDuLieu.SelectedIndices.Clear(); string[] items = new string[lsbDuLieu.Items.Count]; for (int i = 0; i < lsbDuLieu.Items.Count; i++) { lsbDuLieu.SetSelected(i, true); items[i] = (int.Parse((string)lsbDuLieu.SelectedItem) + 2).ToString(); lsbDuLieu.SetSelected(i, false); } lsbDuLieu.Items.Clear(); lsbDuLieu.Items.AddRange(items); }

private void btnBinhPhuong_Click(object sender, EventArgs e) { lsbDuLieu.SelectedIndices.Clear(); string[] items = new string[lsbDuLieu.Items.Count]; for (int i = 0; i < lsbDuLieu.Items.Count; i++) { lsbDuLieu.SetSelected(i, true); items[i] = Math.Pow((int.Parse((string)lsbDuLieu.SelectedItem)),2).ToString(); lsbDuLieu.SetSelected(i, false); } lsbDuLieu.Items.Clear(); lsbDuLieu.Items.AddRange(items); }

private void btnChonSoChan_Click(object sender, EventArgs e) { lsbDuLieu.SelectedItems.Clear(); int[] sel = new int[lsbDuLieu.Items.Count]; for (int i = 0; i < lsbDuLieu.Items.Count; i++) { lsbDuLieu.SetSelected(i,true); if (int.Parse((string)lsbDuLieu.SelectedItem) % 2 == 0) sel[i] = 1; lsbDuLieu.SetSelected(i, false); } for (int i = 0; i < lsbDuLieu.Items.Count; i++) if (sel[i] == 1)

Page 23: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

lsbDuLieu.SetSelected(i, true); }

private void btnChonSoLe_Click(object sender, EventArgs e) { lsbDuLieu.SelectedItems.Clear(); int[] sel = new int[lsbDuLieu.Items.Count]; for (int i = 0; i < lsbDuLieu.Items.Count; i++) { lsbDuLieu.SetSelected(i, true); if (int.Parse((string)lsbDuLieu.SelectedItem) % 2 != 0) sel[i] = 1; lsbDuLieu.SetSelected(i, false); } for (int i = 0; i < lsbDuLieu.Items.Count; i++) if (sel[i] == 1) lsbDuLieu.SetSelected(i, true); }

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

Execution Result:

Page 24: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 3: Viết chương trình nhập danh sách các Sinh Viên theo yêu cầu

sau: (xem hình bên dưới).

Các listbox được phép chọn nhiều mục (kết hợp giữa phím

Shift, Ctrl và chuột)

Khi người dùng nhập họ và tên của Sinh viên vào Textbox,

click nút Cập Nhật thì tên Sinh Viên đó sẽ được nhập vào

Listbox Trái.

, chuyển các tên đang chọn từ Lisxtbox Trái sang

Listbox Phải và ngược lại.

, chuyển hết toàn bộ các tên từ Listbox Trái sang

Listbox Phải và ngược lại.

btnXoa: cho phép xóa các tên đang chọn trong Listbox Trái.

Code:

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

Page 25: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

namespace SW3Basic_Controls2{ public partial class Bai3_frmListSV : Form { public Bai3_frmListSV() { InitializeComponent(); }

private void tbnNhap_Click(object sender, EventArgs e) { if (txtNhap.Text != "") lsbLopA.Items.Add(txtNhap.Text); txtNhap.Text = ""; txtNhap.Focus(); }

private void Bai3_frmListSV_Load(object sender, EventArgs e) { txtNhap.Focus(); }

private void btnChonSangB_Click(object sender, EventArgs e) {

//if(lsbLopA.SelectedIndex==-1)// MessageBox.Show("Chua chon du lieu");

while (lsbLopA.SelectedIndex!=-1) { lsbLopB.Items.Add(lsbLopA.SelectedItem); lsbLopA.Items.Remove(lsbLopA.SelectedItem); } }

private void btnTatCaSangB_Click(object sender, EventArgs e) {

//if(lsbLopA.SelectedIndex==-1)// MessageBox.Show("Chua chon du lieu");

while (lsbLopA.Items.Count > 0) { lsbLopA.SetSelected(0, true); lsbLopB.Items.Add(lsbLopA.SelectedItem); lsbLopA.Items.Remove(lsbLopA.SelectedItem); } }

private void btnChonSangA_Click(object sender, EventArgs e) {

//if(lsbLopB.SelectedIndex==-1)// MessageBox.Show("Chua chon du lieu");

while (lsbLopB.SelectedIndex != -1) { lsbLopA.Items.Add(lsbLopB.SelectedItem); lsbLopB.Items.Remove(lsbLopB.SelectedItem); } }

private void btnTatCaVeA_Click(object sender, EventArgs e) {

Page 26: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

//if(lsbLopB.SelectedIndex==-1)// MessageBox.Show("Chua chon du lieu");

while (lsbLopB.Items.Count > 0) { lsbLopB.SetSelected(0, true); lsbLopA.Items.Add(lsbLopB.SelectedItem); lsbLopB.Items.Remove(lsbLopB.SelectedItem); } } }}

Execution Result:

Bài 4: Viết chương trình từ điển Anh – Việt và Việt – Anh (dùng

TabControl)

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

namespace SW3Basic_Controls2{ public partial class Bai4_frmTuDien : Form { private Dictionary<string, string> anhViet = new Dictionary<string, string>(); private Dictionary<string, string> vietAnh = new Dictionary<string, string>(); public Bai4_frmTuDien() { InitializeComponent();

Page 27: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

initDicAnhViet(); }

private void initDicAnhViet() { anhViet.Add("hello", "xin chao"); anhViet.Add("computer", "may tinh"); anhViet.Add("win", "chien thang");

//tao tap hop luu tru tu khoa tieng anh Dictionary<string, string>.KeyCollection keyColl = anhViet.Keys;

//day tu khoa tieng anh vao combobox anhviet foreach (string s in keyColl) cmbAnhViet.Items.Add(s); } private void cmbAnhViet_SelectedIndexChanged(object sender, EventArgs e) { lsbAnhViet.Items.Clear(); if (cmbAnhViet.SelectedIndex != -1) lsbAnhViet.Items.Add(anhViet[(string)cmbAnhViet.SelectedItem]); }

}}

Execution Result:

Page 28: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Ch ng 4:ươ

Bài 2: Viết chương trình hiển thị vào PictureBox lá cờ tương ứng với

nước được chọn trên Radio Button tương ứng.

Page 29: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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 S4Basic_Controls3{ public partial class Bai1_frmQuaocGia : Form { private ImageList imagelist = new ImageList(); public Bai1_frmQuaocGia() { InitializeComponent(); initImageList(); }

private void initImageList() { //imagelist.ImageSize(400, 400); imagelist.ImageSize = new Size(256,256);

imagelist.Images.Add(Image.FromFile(@"C:\Users\Hungbu\Dropbox\hoctap\win 1\thucHanh\S4Basic_Controls3\Bai1_images\vietnam.jpg")); imagelist.Images.Add(Image.FromFile(@"C:\Users\Hungbu\Dropbox\hoctap\win 1\thucHanh\S4Basic_Controls3\Bai1_images\lao.jpg")); imagelist.Images.Add(Image.FromFile(@"C:\Users\Hungbu\Dropbox\hoctap\win 1\thucHanh\S4Basic_Controls3\Bai1_images\usa.jpg")); imagelist.Images.Add(Image.FromFile(@"C:\Users\Hungbu\Dropbox\hoctap\win 1\thucHanh\S4Basic_Controls3\Bai1_images\singapore.jpg")); }

private void radioButtoon1_CheckedChanged(object sender, EventArgs e) { //pictureBox1.ImageLocation = "//Bai1_Images/vietnam.jpg"; //Bitmap myImage = new Bitmap(@"D:\Execrises\c#\thucHanh\S4Basic_Controls3\Bai1_images\vietnam.jpg"); //pictureBox1.Image = (Image)myImage;

Page 30: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

//pictureBox1.Image = pictureBox1.InitialImage;

pictureBox1.Image = imagelist.Images[0]; }

private void radioButtoon3_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = imagelist.Images[2]; }

private void radioButtoon4_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = imagelist.Images[1]; }

private void radioButtoon5_CheckedChanged(object sender, EventArgs e) { pictureBox1.Image = imagelist.Images[3]; } }}

Execution Result:

Page 31: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 3:

Tạo giao diện như hình: 2 trackbar hoặc HscrollBar và

VscrollBar (minimunvalue=50, maximumvalue=chiều dài hoặc

chiều rộng của Form), 1 PictureBox (xuất hiện vị trí bắt đầu là

(50,50)).

Khi người dùng thay đổi giá trị trên thanh trượt của Trackbar

hoặc scrollbar thì hình sẽ di chuyển theo tọa độ tương ứng với

giá trị thay đổi trên Trackbar hoặc ScrollBar

Page 32: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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 S4Basic_Controls3{ public partial class Bai3_frmTrackBar : Form { public Bai3_frmTrackBar() { InitializeComponent(); }

private void trackBar2_Scroll(object sender, EventArgs e) { pictureBox1.Location = new Point(trackBar1.Value, trackBar2.Size.Height - trackBar2.Value+trackBar2.Minimum); }

private void trackBar1_Scroll(object sender, EventArgs e) { pictureBox1.Location = new Point(trackBar1.Value, trackBar2.Size.Height - trackBar2.Value+trackBar2.Minimum); }

private void Bai3_frmTrackBar_Load(object sender, EventArgs e) { pictureBox1.Location = new Point(60, 60); trackBar1.Maximum = this.Width - 100; trackBar2.Maximum = this.Height - 100; trackBar2.Value = trackBar2.Maximum; } }}

Execution Result:

Page 33: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 4: Xây dựng chương trình hiển thị lịch tờ như sau:

Khi mới mở Form sẽ hiển thị lịch theo ngày hiện hành. Khi thay

đổi ngày, tháng , năm trên form (nhớ kiểm tra ngày hợp lệ)

sau đó click nút Xem Lịch thì sẽ hiển thị đúng ngày tháng năm

đó vào groupbox bên phải (bao gồm cả thứ trong tuần).

Page 34: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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 S4Basic_Controls3{ public partial class Bai4_frmLich : Form { private bool isNamNhuan = false; //private int[] thang; private int[] soNgayTrongThoang; public Bai4_frmLich() { InitializeComponent(); initBien(); } private void initBien() { //thang = new int[12] {1,2,3,4,5,6,7,8,9,10,11,12}; soNgayTrongThoang = new int[12] {31,28,31,30,31,30,31,31,30,31,30,31 }; } private void numericUpDown3_ValueChanged(object sender, EventArgs e) {

}

private void numericUpDown1_ValueChanged(object sender, EventArgs e) { //kiem tra xem nam co nhuan hay khong //neu nhuan thi thang 2 co 29 ngay //khong nhuan thi thang 2 co 28 ngay int nam = (int)numericUpDown1.Value; if (nam % 4 == 0) isNamNhuan = true; if (nam % 100 == 0 && nam % 400 == 0) isNamNhuan = true;

if (isNamNhuan) soNgayTrongThoang[1] = 29; numericUpDown3.Maximum = soNgayTrongThoang[(int)numericUpDown2.Value - 1]; }

private void numericUpDown2_ValueChanged(object sender, EventArgs e) { if (isNamNhuan) soNgayTrongThoang[1] = 29; numericUpDown3.Maximum = soNgayTrongThoang[(int)numericUpDown2.Value - 1]; }

private void buttoon1_Click(object sender, EventArgs e) { int nam = (int)numericUpDown1.Value; int thang = (int)numericUpDown2.Value; int ngay = (int)numericUpDown3.Value; DateTime dt = new DateTime(nam, thang, ngay);

Page 35: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

label4.Text = "Thoang " + dt.Month.ToString(); label5.Text = "Ngay " + dt.Day.ToString(); label6.Text = dt.DayOfWeek.ToString(); } }}

Execution Result:

Bài 5:

Viết chương trình Quản lý Đăng ký Khách hàng VIP cho Siêu

thị CoopMark như sau:

Khi khách hàng đăng ký thông tin khách hàng VIP, nhân viên

quản lý khách hàng thực hiện nhập mã số KH, họ tên, ngày

sinh, địa chỉ, nghề nghiệp của khách.

Chương trình thực hiện:

Xây dựng class KhachHang (msKH, tenKH, Ngaysinh,

Diachi, NgheNghiep)

Page 36: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Xây dựng class DanhSachKhachHang để quản lý Thêm

Khách Hàng vào danh sách khách hàng thân thiết, xem

tổng số khách hàng trong danh sách.

- Form_Load: Danh Sách khách hàng chưa có thành viên

nào, con trỏ văn bản đặt vào textbox mã số khách hàng,

các button Dangky, NhapMoi, TongKet bị mờ

(enabled=false). Thực hiện nhập các thông tin tương

ứng. Khi nhập đầy đủ thông tin (kiểm tra dữ liệu hợp lệ)

thì btnDangky có tác dụng.

- btnDangKy: thực hiện thêm khách hàng vào Danh sách

khách hàng, đồng thời hiển thị thông tin khách hàng vừa

đăng ký thành công vào messagebox như hình bên dưới.

btnNhapMoi, btnTongKet .

- btnNhapMoi: khởi tạo lại trạng thái ban đầu của Form,

btnNhapMoi bị mờ.

- btnTôngKet: hiển thị Tổng khách hàng có trong Danh

sách khách hàng vào messagebox như hình bên dưới.

btnTongKet bị mờ.

- btnThoat_Click: hỏi người dùng có chắc chắn thoát khỏi

chương trình hay không? Yes: thoát, No: không.

Page 37: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Danh sách các giá trị trong DomainUpDown Nghề Nghiệp:

Page 38: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

Bai5_DSKhachHang.cs

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

namespace S4Basic_Controls3{ class Bai5_DSKhachHang { private ArrayList data;

public Bai5_DSKhachHang() { data = new ArrayList(); } public void ThoemKhachHang(Bai5_KhachHang kh) { try {

data.Add(kh); } catch {

throw new Exception(); } } public int XemTongSoKH() { return data.Count; } /* public override string ToString() { foreach (object obj in data) { Bai5_KhachHang kh = (Bai5_KhachHang)obj; return kh.ToString(); }

Page 39: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

return null; }*/ }}

Bai5_KhachHang.cs

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

namespace S4Basic_Controls3{ class Bai5_KhachHang { private int maKH; private string tenKH; private DateTime ngaySinh; private string diaChi; private string ngheNghiep;

public Bai5_KhachHang() { } public Bai5_KhachHang(int maKH, string tenKH, DateTime ngaySinh, string diaChi, string ngheNghiep) { this.maKH = maKH; this.tenKH = tenKH; this.ngaySinh = ngaySinh; this.diaChi = diaChi; this.ngheNghiep = ngheNghiep; } /* public override string ToString() { return "ma khach hang: " + maKH.ToString() + "\nten khach hang: " + tenKH.ToString() + "\ngay sinh: " + ngaySinh.ToString() + "\nDia chi: " + diaChi.ToString() + "\nnghe nghiep: " + ngheNghiep.ToString(); } */ //properties }}

Page 40: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Execution Result:

Chương 5:

Bài 1: Tạo giao diện như hình:

Khi Form Load thì có dòng chữ “TTĐT CNTT TPHCM Xin chào các bạn” chạy ngang màn hình (dùng timer)

Code:

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

Page 41: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

using System.Text;using System.Windows.Forms;

namespace SW5_AdvancedControl{ public partial class Bai1_frmChuChay : Form { public Bai1_frmChuChay() { InitializeComponent(); }

private void Bai1_frmChuChay_Load(object sender, EventArgs e) { timChuChay.Start(); //Dispose(); }

private void timChuChay_Tick(object sender, EventArgs e) { if (labelX1.Location.X >= (this.Right - this.Left))) labelX1.Location = new Point(this.Left)-labelX1.Size.Width, labelX1.Location.Y); else labelX1.Location = new Point(labelX1.Location.X + 1, labelX1.Location.Y); } }}

Execution Result:

Bài 2: Tạo Giao diem Form Splash như hình: chứa thanh progress bar, và một nút OK.

Khi khởi động chương trình thì Form Splash xuất hiện, và form này dừng 1 khoảng thời gian là 15s.

Page 42: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

- Thanh progress bar sẽ thể hiện tiến trình load form này, sau 15s form splash sẽ tự động tắt và khởi động Form trong bài 1

- Trong khi Form Splash đang hoạt động, nếu người dùng nhấn vào nút OK thì Form này ngưng hoạt động, đồng thời Form bài 1 được gọi hoạt động.

Code:

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 SW5_AdvancedControl{

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

private void progressBarX1_Click(object sender, EventArgs e) { }

private void timer1_Tick(object sender, EventArgs e) { if (progressBarX1.Value == progressBarX1.Maximum)

Page 43: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ timer1.Stop(); Bai1_frmChuChay frm = new Bai1_frmChuChay(); frm.Show(); this.Close(); } else { progressBarX1.PerformStep(); } }

private void Bai2_frmLoadSlash_Load(object sender, EventArgs e) { progressBarX1.Minimum = 0; progressBarX1.Maximum = 15; progressBarX1.Step = 1; timer1.Interval = 1000; timer1.Start(); }

private void buttoonX1_Click(object sender, EventArgs e) {

timer1.Stop(); Bai1_frmChuChay frm = new Bai1_frmChuChay(); frm.ShowDialog(); this.Close(); } }}

Chương 6:

Bài 1:

Page 44: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Viết chương trình nhập danh danh bạ với yêu cầu giao

diện như hình dưới.

Khi chương trình vừa hiển thị, treeview chứa tất cả các

chữ cái từ A->Z.

Nhằm mục đích tiện lợi cho người sử dụng khi tìm tên, khi

người sử dụng nhập tên của một người nàp đó, chương

trình sẽ đưa tên người này vào treeview ở vị trí treenode

có tương ứng với chữ các đầu của tên (xem hình).

Code:

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

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

private void Form1_Load(object sender, EventArgs e) { //

Page 45: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

txtTen.Focus(); }

private void btnNhap_Click(object sender, EventArgs e) { if (txtHo.Text != "" & txtTen.Text != "") { string hoten = txtTen.Text + ", " + txtHo.Text; foreach(TreeNode value in trvHoTen.Nodes) if (string.Compare(value.Text.ToUpper(), txtTen.Text[0].ToString().ToUpper()) == 0) { value.Nodes.Add(hoten); value.Expand(); txtTen.Text = txtHo.Text = ""; txtTen.Focus(); return; } } } }}

Execution Result:

Bài 2:

Viết chương trình nhập dữ liệu vào Listview như hình bên dưới.

Page 46: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Người sử dụng nhập thông tin: (lastname, firstname,

phone) và sử dụng nút Add Name để nhập vào listview.

Các contact có biểu tượng (icon) hiển thị như hình.

Người sử dụng có thể thay đổi chế độ view của listview

bằng menu View.

Menu FormatListview hiểu thị hộp thọai chọn màu dùng

để thay đổi dạng grid của Listview.

Gợi ý:

o Sử dụng ListviewItem để thêm một dòng mới cho Listview

o Sử dụng ImageList để chứa thư viện icon cho Listview.

Kết nối Listview với ImageList.

Code:

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

namespace WindowsApplication1{ public partial class Bai2_Detail : Form {

Page 47: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

public Bai2_Detail() { InitializeComponent(); }

private void Bai2_Detail_Load(object sender, EventArgs e) { InitLsvDetail(); txtLastName.Select(); } private void InitLsvDetail() { ImageList imgList = new ImageList(); imgList.ColorDepth = ColorDepth.Depth4Bit; imgList.ImageSize = new Size(16, 16); imgList.Images.Add(Image.FromFile(@"E:\IMAGES\thongoc.jpg"));

ImageList imgListLarge = new ImageList(); imgListLarge.TransparentColor = Color.White; imgListLarge.ColorDepth = ColorDepth.Depth24Bit; imgListLarge.ImageSize = new Size(64,64); imgListLarge.Images.Add(Image.FromFile(@"E:\IMAGES\thongoc.jpg"));

ColumnHeader clhFirstName = new ColumnHeader(); ColumnHeader clhLastName = new ColumnHeader(); ColumnHeader clhPhone = new ColumnHeader();

clhFirstName.Text = "FirstName"; clhLastName.Text = "LastName"; clhPhone.Text = "PhoneNumber";

clhFirstName.Width = lsvDetail.Width / 3-1; clhLastName.Width = lsvDetail.Width / 3-1; clhPhone.Width = lsvDetail.Width / 3-1;

lsvDetail.Columns.Add(clhLastName); lsvDetail.Columns.Add(clhFirstName); lsvDetail.Columns.Add(clhPhone);

lsvDetail.View = View.Details; lsvDetail.SmallImageList = imgList; lsvDetail.LargeImageList = imgListLarge; lsvDetail.AllowColumnReorder = true; }

private void lagerIconsToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.LargeIcon; }

private void btnNhap_Click(object sender, EventArgs e) { ListViewItem item = new ListViewItem(); item.ImageIndex = 0; item.Text = txtLastName.Text; item.SubItems.Add(txtFirstName.Text); //item.SubItems.Add(txtLastName.Text); item.SubItems.Add(txtPhone.Text);

lsvDetail.Items.Add(item);

txtFirstName.Text = txtLastName.Text =

Page 48: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

txtPhone.Text = ""; txtLastName.Focus(); }

private void detailToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.Details; }

private void listToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.List; }

private void titleToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.Tile; }

private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.SmallIcon; }

private void formatListVToolStripMenuItem_Click(object sender, EventArgs e) {

}

private void colorForLisviewGrapToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); //colorDialog.ShowDialog(); colorDialog.AllowFullOpen = false; //colorDialog.Color= }

//property //private ImageList imgList; }}

Execution Result:

Page 49: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 3: Thiết kế chương trình quản lý SV, cho phép nhập thông tin SV

vào các textbox

Yêu cầu:

Thêm vào form ilsNho là Imagelist có kìch thước mặc định

16 x 16 phục vụ cho ListView, ilsLon là Imagelist có kìch

thước mặc 48 x 48 phục vụ cho ListView

Page 50: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Nhấn nút Cập nhật Item thì đưa thông tin sinh viên vào

Listview theo nguyên tắc dựa theo hình.

Nhấn nút Xóa item là xóa item đang chọn trên Listview

( có thể chọn nhiều), nhưng phải cài bẫy lỗi xác nhận lại

tình trạng xóa. Xác nhận đã chọn Item nào chưa, xác

nhận có chắc xóa không.

Nhấn nút thêm mới thì xóa thông tin sinh viên cho phép

nhập thông tin Sinh Viên mới.

Nếu chọn một Sinh Viên nào trong Listview thì hiện lại

thông tin Sinh Viên đó lên các Texbox tương ứng .

Code

using System;using System.Collections.Generic;using System.Text;using System.Runtime.Serialization;

namespace WindowsApplication1

Page 51: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ [Serializable] class Bai3_ThoongTInSV { public string maSV; public string hoTen; public string diaChi; public string ngaySinh; public string lop; public Bai3_ThoongTInSV() { }

} }

:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formattoers.Binary;using System.Collections;

namespace WindowsApplication1{ //[Serializable] public partial class Bai3_ThoongTinSinhVien : Form { private List<ListViewItem> arrSV; public Bai3_ThoongTinSinhVien() { arrSV = new List<ListViewItem>(); InitializeComponent(); InitListView(); } private void InitListView() { lsvDetail.Columns.Add("Ma SV", lsvDetail.Width/5, HorizontalAlignment.Center); lsvDetail.Columns.Add("Ho Ten SV", lsvDetail.Width / 5, HorizontalAlignment.Center); lsvDetail.Columns.Add("Dia Chi", lsvDetail.Width / 5, HorizontalAlignment.Center); lsvDetail.Columns.Add("Ngay Sinh", lsvDetail.Width / 5, HorizontalAlignment.Center); lsvDetail.Columns.Add("Lop", lsvDetail.Width / 5, HorizontalAlignment.Center); lsvDetail.View = View.Details; lsvDetail.GridLines = true; } private void Bai3_ThoongTinSinhVien_Load(object sender, EventArgs e) {

}

private void btnThoemMoi_Click(object sender, EventArgs e) { ListViewItem item = new ListViewItem(); item.Text = txtMaSV.Text;

Page 52: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

item.SubItems.Add(txtHoTen.Text); item.SubItems.Add(txtDiaChi.Text); item.SubItems.Add(dtpNgaySinh.Text); item.SubItems.Add(cbbLop.Text); lsvDetail.Items.Add(item); arrSV.Add(item); }

private void btnLuuFile_Click(object sender, EventArgs e) { FileStream fiele = new FileStream("luu.txt", FileMode.Create); BinaryFormattoer bforn = new BinaryFormattoer(); bforn.Serialize(fiele, arrSV); fiele.Close(); }

private void buttoon1_Click(object sender, EventArgs e) { FileStream fiele = new FileStream("luu.txt", FileMode.Open); BinaryFormattoer bform = new BinaryFormattoer(); arrSV.Clear(); arrSV=(List<ListViewItem>)bform.Deserialize(fiele); lsvDetail.Clear(); fiele.Close(); foreach (ListViewItem item in arrSV) lsvDetail.Items.Add(item); lsvDetail.Update();

} }}

Execution Result:

Page 53: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 5:

Tạo 1 chương trình dùng để duyệt và hiển thị tất cả các ảnh

trong 1 thư mục ở dạng thumnails view.

Khi chương trình khởi động sẽ nạp cây thư mục lên

Treeview

Khi người dùng chọn 1 thư mục trên Treeview, chương

trình sẽ liệt kê tất cả các hình ảnh lên 1 FlowlayoutPanel.

Khi người dùng chọn 1 hình từ FlowlayoutPanel, hình ảnh

đó sẽ hiện thị lên 1 PictureBox ở phía trên

Gợi ý:

Dùng mảng picturebox động để chứa danh sách các hình

ảnh

Dùng FlowLayoutPanel để chứa mảng picturebox

Page 54: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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

namespace WindowsApplication1{ public partial class Bai6_frmPlayPicture : Form { public Bai6_frmPlayPicture() { InitializeComponent(); InitExploreTreeview(); } private void InitExploreTreeview() { explorerTreeView.ShowRootLines = true; explorerTreeView.ShowLines = true; TreeNode root; string[] driver = Directory.GetLogicalDrives(); foreach (string dvr in driver) { root = new TreeNode(dvr);

Page 55: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

root.Nodes.Add(new TreeNode("")); explorerTreeView.Nodes.Add(root);

}

} private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {

}

private void Bai6_frmPlayPicture_Load(object sender, EventArgs e) { showPicPictureBox.SizeMode = PictureBoxSizeMode.Zoom; contentPicFlowLayoutPanel.BorderStyle = BorderStyle.FixedSingle; }

private void explorerTreeView_Aft)erSelect(object sender, TreeViewEventArgs e) { DirectoryInfo dir = new DirectoryInfo(e.Node.FullPath); contentPicFlowLayoutPanel.Controls.Clear(); PictureBox[] pic; showPicPictureBox.Image = null; if (dir.Exists) { pic = new PictureBox[dir.GetFiles().GetLength(0)]; int i = 0; foreach (FileInfo fiele in dir.GetFiles()) { if (fiele.Name.EndsWith("jpg") || fiele.Name.EndsWith("gif") || fiele.Name.EndsWith("bmp")) { pic[i] = new PictureBox(); pic[i].Name = "pic" + fiele.Name; pic[i].SizeMode = PictureBoxSizeMode.Zoom; pic[i].Size = new Size(100, 100); pic[i].BorderStyle = BorderStyle.FixedSingle; pic[i].Image = Image.FromFile(e.Node.FullPath+"//"+fiele.Name); pic[i].Click+=new EventHandler(Bai6_frmPlayPicture_Click); contentPicFlowLayoutPanel.Controls.Add(pic[i]); i++; contentPicFlowLayoutPanel.Update(); } } //MessageBox.Show(i.ToString()+", "+contentPicFlowLayoutPanel.Controls.Count); } } private void Bai6_frmPlayPicture_Click(object sender, EventArgs e) { PictureBox pic = (PictureBox)sender; showPicPictureBox.Image = pic.Image; }

private void explorerTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) { try { if (e.Node.Nodes[0].Text == "") { e.Node.Nodes.Clear(); FillNode(e.Node); } } catch (Exception ex)

Page 56: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ MessageBox.Show(ex.Message); } } private void FillNode(TreeNode rootNode) { DirectoryInfo dir = new DirectoryInfo(rootNode.FullPath); foreach (DirectoryInfo chirldName in dir.GetDirectories()) { TreeNode chirldNode = new TreeNode(chirldName.Name); chirldNode.Nodes.Add(new TreeNode("")); rootNode.Nodes.Add(chirldNode); } } }}

Execution Result:

Chương 7

Bài 1:

Page 57: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Thiết kế và lập trình cho chương trình Calculator đơn giản

trong windows. (chú ý: dùng sự kiện các phím khi nhập biểu

thức tính)

Code:

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 S7_Mouse_keyboard_handling{ public partial class Bai1_Calculator : Form { private char phepToan; private flooat soA, soB, ketQuaa; public Bai1_Calculator() { InitializeComponent(); }

private void Bai1_Calculator_KeyPress(object sender, KeyPressEventArgs e) { char c = e.KeyChar; //if(hienThoiText.Text) //MessageBox.Show(e.KeyChar.ToString()); if (c == '+' || c == '-' || c == '*' || c == '/') { soA = flooat.Parse(hienThoiText.Text); phepToan=c; hienThoiText.Text = ""; } if (c == '=') { //MessageBox.Show(e.KeyChar.ToString()); if(hienThoiText.Text.Length==0)

Page 58: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

hienThoiText.Text="0"; soB = flooat.Parse(hienThoiText.Text); hienThoiText.Text = ""; try { ketQuaa = Tinh(soA, soB, phepToan); hienThoiText.Text = ketQuaa.ToString(); } catch (Exception ex) { hienThoiText.Text = ex.Message; } } if (hienThoiText.Text == "0") hienThoiText.Text = ""; if(char.IsDigit(e.KeyChar)||e.KeyChar=='.') hienThoiText.Text += c; } private flooat Tinh(flooat soA, flooat soB, char phepToan) { if (phepToan == '+') return (soB + soA); if (phepToan == '-') return (soA - soB); if (phepToan == '*') return (soA * soB); if (phepToan == '/') { try { return (soA / soB); } catch (DivideByZeroException) { throw new Exception("loi chi chia cho 0.."); } } return 0; }

private void Bai1_Calculator_Load(object sender, EventArgs e) { //hienThoiText.Text = "0"; //hienThoiText.ReadOnly = true; this.Focus(); }

private void Bai1_Calculator_KeyDown(object sender, KeyEventArgs e) {

} }}

Execution Result:

Page 59: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 2: 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ặt 1 số nào thì kí tự hoặt 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:

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

Page 60: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

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

namespace S7_Mouse_keyboard_handling{ public partial class Bai2_frmTyper : Form { private ArrayList arrKey; public Bai2_frmTyper() { arrKey = new ArrayList(); InitializeComponent(); InitLabelKey(); }

private void Bai2_frmTyper_Load(object sender, EventArgs e) {

} private void InitLabelKey() { int indexOfKey = 65; int keyWidth = keyPanel.Width / 10; int keyHeight = keyPanel.Height / 3;

for (int i = 0; i < 26; i++) { Label key = new Label(); key.AutoSize = false; key.BorderStyle = BorderStyle.Fixed3D; key.Font = new Font(new FontFamily("Arial"), 15f, FontStyle.Bold); key.TextAlign = ContentAlignment.MiddleCenter; key.Parent = this.keyPanel; key.Text = char.ConvertFromUtf32(indexOfKey).ToUpper(); //key.Tag if (i < 10) key.Location = new Point((i%10) * keyWidth, 1); else if (i>=10&&i<20) key.Location = new Point((i%10) * keyWidth, 1 + keyHeight); else key.Location = new Point((i % 10) * keyWidth, 1 + 2*keyHeight); key.Width = keyWidth; key.Height = keyHeight; keyPanel.Controls.Add(key); arrKey.Add(key); indexOfKey++; } }

private void noiDungRichTextBox_KeyPress(object sender, KeyPressEventArgs e) { //for (int i = 0; i < 26; i++) //{ // if (Char.Parse(((Label)arrKey[i]).Text) == e.KeyChar) // { // ((Label)arrKey[i]).ForeColor = Color.Red;

// keyPanel.Update(); // return; // }

Page 61: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

//} foreach (Control ctr in keyPanel.Controls) { if (String.Compare(ctr.Text.ToUpper(),e.KeyChar.ToString().ToUpper())==0) { ctr.ForeColor = Color.Red; keyPanel.Update(); return; } } //keyPanel.Refresh(); }

private void noiDungRichTextBox_KeyUp(object sender, KeyEventArgs e) { foreach (Control ctr in keyPanel.Controls) { ctr.ForeColor = this.ForeColor; } keyPanel.Update(); } }}

Execution Result:

Chương 9:

Bài 1: Tạo giao diện Form như hình:

Page 62: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Viết code cho các menu tương ứng:

Form treeview:

- Tạo giao diện như hình, cho phép người dùng nhập vào

treeview các node có định dạng như hình.

- Tạo contextmenustrip áp dụng cho treeview, để thao tác

trực tiếp ngay trên treeview như: đổi tên, xóa node, xóa tất

cả các node trên treeview.

- Viết các code thích hợp cho các menu của contextmenustrip

Page 63: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Form Listview: ( bài tập 3, chương 6)

- Tạo giao diện như hình, cho phép người dùng nhập vào

ListView các phần tử có định dạng như hình.

- Tạo contextmenustrip áp dụng cho Listview, để thao tác

trực tiếp ngay trên ListView như: Large Icon, Small, List, Tile,

Detail, Update (cập nhật lại thông tin của phần tử)

- Viết các code thích hợp cho các menu của contextmenustrip

Page 64: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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 S9_Menu_Toolbar_Status{ public partial class Bai1_MDIForm : Form { //private Container container; public Bai1_MDIForm() { InitializeComponent(); }

private void treeViewToolStripButtoon_Click(object sender, EventArgs e) { Bai1_TreeViewChirld treeFrm = new Bai1_TreeViewChirld(); treeFrm.MdiParent = this; treeFrm.Text = "Tree View Chirld.."; //container.Add(treeFrm); //this.MdiParent.comp treeFrm.Show(); }

Page 65: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {

}

private void Bai1_MDIForm_Load(object sender, EventArgs e) { this.IsMdiContainer = true; this.WindowState = FormWindowState.Maximized; }

private void listViewToolStripButtoon_Click(object sender, EventArgs e) { WindowsApplication1.Bai3_ThoongTinSinhVien thongTinSV = new WindowsApplication1.Bai3_ThoongTinSinhVien(); thongTinSV.MdiParent = this; thongTinSV.Text = "List View Chirld.."; //container.Add(thongTinSV); thongTinSV.Show(); }

private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.Cascade); }

private void tiToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); }

private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.ArrangeIcons); }

private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) { //this.MdiParent.Container.Dispose(); //this.MdiParent.Close(); foreach (Form frmChirld in this.MdiChildren) { frmChirld.Close(); } }

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

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

namespace WindowsApplication1{ public partial class Bai3_ThoongTinSinhVien : Form

Page 66: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ public Bai3_ThoongTinSinhVien() { InitializeComponent(); }

private void Bai3_ThoongTinSinhVien_Load(object sender, EventArgs e) { txtMaSV.Select(); }

private void largeIconToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.LargeIcon; }

private void detailToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.Details; }

private void smallToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.SmallIcon; }

private void listToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.List; }

private void titleToolStripMenuItem_Click(object sender, EventArgs e) { lsvDetail.View = View.Tile; }

private void updateToolStripMenuItem_Click(object sender, EventArgs e) { } }}

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 S9_Menu_Toolbar_Status{ public partial class Bai1_TreeViewChirld : Form { public Bai1_TreeViewChirld() { InitializeComponent(); }

private void nhapButtoon_Click(object sender, EventArgs e) { if (maSoText.Text.Length != 0)

Page 67: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ TreeNode node = new TreeNode(maSoText.Text); if (!treeView1.Nodes.Contains(node)) { TreeNode parentNode = treeView1.Nodes.Add(maSoText.Text); int parentIndex = treeView1.Nodes.IndexOf(parentNode); treeView1.Nodes[parentIndex].Tag = "parent"; treeView1.Nodes[parentIndex].Nodes.Add(hoTenText.Text); treeView1.Nodes[parentIndex].Nodes.Add(diaChiText.Text); treeView1.Nodes[parentIndex].Nodes.Add(lopText.Text); treeView1.Nodes[parentIndex].Expand(); } else { MessageBox.Show("trung ma so.."); //break; } } else MessageBox.Show("ma so khong duoc rong..");

maSoText.Text = hoTenText.Text = diaChiText.Text = lopText.Text = ""; maSoText.Focus(); }

private void removeToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode node = treeView1.SelectedNode; if (node.Tag.ToString() != "parent") node = node.Parent; treeView1.Nodes.Remove(node); }

private void renameNodeToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.LabelEdit = true; TreeNode node = treeView1.SelectedNode; node.BeginEdit(); //node.EndEdit( }

private void removeAllNodeToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); } }}

Execution Result:

Page 68: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 2:

Page 69: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Tạo 1 trình duyệt đơn giản như sau:

ToolBar: Các nút GoBack, Go Forward, Refresh, Stop và

Home , URL bar, Go button. (dùng ToolStrip để tạo hint cho

các nút trên Toolbar)

Control Webbrowser: để hiển thị nội dung của trang web

Status bar: chèn thêm 1 progressbar để theo dõi tiến trình

download trang web.

Execution Result:

Page 70: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Chương 10:

Bài 1:

1. Xây dựng UserControl Login giao diện thiết kế như hình

- Validation các textBox not blank.

- Xử lý mã cứng user name và password

- User control ném ra 2 event : LoginSuccess và LoginFail.

- Đưa Control vào ToolBox với icon tùy chọn.

Page 71: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

2. Xây dựng chương trình sử dụng UserControl với giao diện

như sau :

Code:

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

namespace S10_CommonDialog_CustomControl{ public partial class Bai1_LoginUserControl : UserControl { //event handler public EventHandler LoginSuccess; public EventHandler LoginFail;

public Bai1_LoginUserControl() { InitializeComponent(); }

//properties public string UserNameLabel { get { return lbUsername.Text; } set { lbUsername.Text = value; } } public string PasswordLabel { get { return lbPassword.Text; } set { lbPassword.Text = value; } } public string LoginButtoon {

Page 72: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

get { return btnLogin.Text; } set { btnLogin.Text = value; } } [Browsable(false)] //khong cho phep nhap text trong luc thiet ke public string UserName { set { txtUsername.Text = value; } } [Browsable(false)] public string PassWord { set { txtPassword.Text = value; } } //login buttoon click handler private void btnLogin_Click(object sender, EventArgs e) { if (txtUsername.Text.Length == 0){ MessageBox.Show("enter user name.."); return; } if (txtPassword.Text.Length == 0) { MessageBox.Show("enter pass word.."); return; } if (AllVali(txtUsername.Text, txtPassword.Text)) { if (LoginSuccess != null) LoginSuccess(this, EventArgs.Empty); } else if (LoginFail != null) LoginFail(this, EventArgs.Empty); } private bool AllVali(string uName, string pWord) { return (uName == "admin" && pWord == "ngochung"); } }}

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 S10_CommonDialog_CustomControl{ public partial class Bai1_UserCOntrol : Form { private string useName = "admin"; private string pass = "ngochung"; public Bai1_UserCOntrol() { InitializeComponent(); }

private void btnLogin_Click(object sender, EventArgs e) {

Page 73: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

if (txtUsername.Text != "" && txtPassword.Text != "") { if (string.Compare(txtUsername.Text, useName) == 0 && string.Compare(txtPassword.Text, pass) == 0) { //nem ra event success //tao MessageBox.Show("ban dang nhap thanh congc"); } else { //nem ra event fail MessageBox.Show("dang nhap khong thanh congc"); } txtUsername.Text = txtPassword.Text = ""; txtUsername.Focus(); } else { MessageBox.Show("phai nhap username va password.."); txtUsername.Focus(); } }

private void Bai1_UserCOntrol_Load(object sender, EventArgs e) { txtUsername.Select(); } }}

Execution Result:

Page 74: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 2:

1. Xây dựng form OutFit Details giao diện thiết kế như hình

Page 75: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

- Nhấn Next hay Previous sẽ đi qua sản phẩm kế tiếp hay

trước đó.

- Validation để báo đã đi tới record cuối hay record đầu

( không di chuyển được nữa)

- Khi bấm Print sẽ in ra PrintPreviewDialog bằng các API GDI

+ ;

- Gợi ý:

o Khai báo một class chứa sản phẩm (Description,

MyPicture)

o Khai báo 1 ArrayList arr chứa các sản phẩm

o Nhập 5 Sản phẩm vào arr

Page 76: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Code:

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

namespace S10_CommonDialog_CustomControl{ class Bai3_OutFitClass { private string moTa; private Image hinhAnh;

public Bai3_OutFitClass(string mt, Image ha) { MoTa = mt; HinhAnh = ha; } public string MoTa { get { return moTa; } set { moTa = value; } } public Image HinhAnh { get { return hinhAnh; } set { hinhAnh = value; } } }}

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

Page 77: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

using System.Linq;using System.Text;using System.Windows.Forms;

namespace S10_CommonDialog_CustomControl{ public partial class Bai3_OutFitDetail : Form { private ArrayList arrOutFit; private int count = 0;

public Bai3_OutFitDetail() { InitializeComponent(); arrOutFit = new ArrayList(); InitArrOutFit(); ptbOutFit.SizeMode = PictureBoxSizeMode.Zoom; txtMoTa.Text = ((Bai3_OutFitClass)arrOutFit[count]).MoTa; ptbOutFit.Image = ((Bai3_OutFitClass)arrOutFit[count]).HinhAnh; } private void InitArrOutFit() { Bai3_OutFitClass outFit1 = new Bai3_OutFitClass("outfiet 1", Image.FromFile("images/1.jpg")); Bai3_OutFitClass outFit2 = new Bai3_OutFitClass("outfiet 2", Image.FromFile("images/2.jpg")); Bai3_OutFitClass outFit3 = new Bai3_OutFitClass("outfiet 3", Image.FromFile("images/3.jpg")); Bai3_OutFitClass outFit4 = new Bai3_OutFitClass("outfiet 4", Image.FromFile("images/4.jpg")); arrOutFit.Add(outFit1); arrOutFit.Add(outFit2); arrOutFit.Add(outFit3); arrOutFit.Add(outFit4); }

private void btnNext_Click(object sender, EventArgs e) { if (count < 0) count = 0; if (count < 3) { count++; txtMoTa.Text = ""; txtMoTa.Text = ((Bai3_OutFitClass)arrOutFit[count]).MoTa; ptbOutFit.Image = ((Bai3_OutFitClass)arrOutFit[count]).HinhAnh; } }

private void btnPre_Click(object sender, EventArgs e) { if (count > 3) count=3; if (count > 0) { count--; txtMoTa.Text = ""; txtMoTa.Text = ((Bai3_OutFitClass)arrOutFit[count]).MoTa; ptbOutFit.Image = ((Bai3_OutFitClass)arrOutFit[count]).HinhAnh; } }

private void btnPrint_Click(object sender, EventArgs e) { PrintPreviewDialog printPreview = new PrintPreviewDialog(); PrintDocument doc = new PrintDocument(); doc.PrintPage += new PrintPageEventHandler(PrintDoc); printPreview.Document = doc; doc.DocumentName = "Test GDI+";

Page 78: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

printPreview.ShowDialog(); } private void PrintDoc(object sender, PrintPageEventArgs e) { Image im = ptbOutFit.Image; string des = txtMoTa.Text; e.Graphics.DrawString(des, new Font("Arial",30f), new SolidBrush(Color.Red), new Point(20, 20)); e.Graphics.DrawImage(im, new Point(120, 120)); } }}

Execution Result:

Page 79: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

Bài 3: Xây dựng chương trình Wordpad đơn giản như hình bên dưới:

Sử dụng các Commond dialog: SaveFileDialog, OpenFileDialog,

ColorDialog, FontDialog

Code:

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

namespace S10_CommonDialog_CustomControl{ public partial class Bai4_Notepad : Form

Page 80: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

{ public Bai4_Notepad() { InitializeComponent(); }

private void fontToolStripButtoon_Click(object sender, EventArgs e) { FontDialog fontDia = new FontDialog(); fontDia.ShowApply = true; fontDia.Apply += new EventHandler(ApplyFont); fontDia.Font = noiDungRichText.Font; fontDia.Color = noiDungRichText.ForeColor; if (fontDia.ShowDialog() == DialogResult.OK) { ApplyFont(fontDia, EventArgs.Empty); } } private void ApplyFont(object sender, EventArgs e) { FontDialog fontDia = (FontDialog)sender; noiDungRichText.Font = fontDia.Font; noiDungRichText.ForeColor = fontDia.Color; }

private void colorToolStripButtoon_Click(object sender, EventArgs e) { ColorDialog colorDia = new ColorDialog(); colorDia.Color = noiDungRichText.ForeColor; if (colorDia.ShowDialog() == DialogResult.OK) { noiDungRichText.ForeColor = colorDia.Color; } }

private void printToolStripButtoon_Click(object sender, EventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage+= new PrintPageEventHandler(doc_PrintPage); PrintDialog prin = new PrintDialog(); if (prin.ShowDialog() == DialogResult.OK) { doc.Print(); } } private void doc_PrintPage(object sender, PrintPageEventArgs e) { string s = noiDungRichText.Text; e.Graphics.DrawString(s, new Font("Arial", 13f), new SolidBrush(Color.Red), new Point(20, 20)); }

private void openToolStripButtoon_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "Text fiele|*.txt"; if (openFile.ShowDialog() == DialogResult.OK) { Stream myStream = openFile.OpenFile(); StreamReader streamReader = new StreamReader(myStream); noiDungRichText.Text = streamReader.ReadToEnd(); streamReader.Close(); myStream.Close(); } }

Page 81: Bao cao bai tap mon cong nghe dot net (C# 1) hungbuit@gmail.com

private void newToolStripButtoon_Click(object sender, EventArgs e) { noiDungRichText.Text = ""; }

private void saveToolStripButtoon_Click(object sender, EventArgs e) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "Text fiele|*.txt"; if (saveFile.ShowDialog() == DialogResult.OK) { Stream myStream = saveFile.OpenFile(); StreamWriter myWriter = new StreamWriter(myStream); myWriter.Write(noiDungRichText.Text); myWriter.Close(); myStream.Close(); } } }}