Public Partial Class Private String: Using Using Using Using Using Namespace
Public Partial Class Private String: Using Using Using Using Using Namespace
CNhanVien Form
using System; public partial class Form1 : Form
using {
System.Collections.Generic;
using System.Linq;
private Dictionary<string, CNhanVien> dsNhanVien;
using System.Text;
using System.Threading.Tasks; //Show danh sách
private void HienThiDSNV()
namespace OnThi_Bai1 {
{
[Serializable]
dataGridView1.DataSource =
class CNhanVien dsNhanVien.Values.ToList();
{ }
private string
m_manv, m_tennv; //Tìm nhân viên
private bool m_gt;
private CNhanVien TimNV(string manv)
private DateTime
m_ngaysinh; {
private double try { return dsNhanVien[manv]; }
m_giolam; catch { return null; }
private double }
m_tangca; public Form1()
{
public string Manv { InitializeComponent();
get => m_manv; set => m_manv }
= value; } //Load form
public string Tennv { private void Form1_Load(object sender, EventArgs
get => m_tennv; set =>
m_tennv = value; } e)
public bool Gt { get {
=> m_gt; set => m_gt = value; dsNhanVien = new Dictionary<string,
} CNhanVien>();
public DateTime loadFile();
Ngaysinh { get => m_ngaysinh;
set => m_ngaysinh = value; }
HienThiDSNV();
public double Giolam }
{ get => m_giolam; set =>
m_giolam = value; } //Them
public double Tangca private void btnThem_Click(object sender,
{ get => m_tangca; set =>
m_tangca = value; }
EventArgs e)
{
public double LuongNV CNhanVien nv = new CNhanVien();
{ get { return m_giolam * nv.Manv = txtMaNV.Text;
250000 + m_tangca * nv.Tennv = txtTenNV.Text;
25000; } }
public CNhanVien()
nv.Ngaysinh = dateTimePicker1.Value;
{ nv.Giolam =
m_manv = ""; Convert.ToDouble(txtSoNgayLam.Text.ToString());
m_tennv = ""; nv.Tangca =
m_ngaysinh = Convert.ToDouble(txtSoGioTangCa.Text.ToString());
DateTime.Today;
nv.Gt = radioButton1.Checked;
m_gt = true;
m_giolam = 0; if (TimNV(nv.Manv) == null)
m_tangca = 0; {
} dsNhanVien.Add(nv.Manv,nv);
HienThiDSNV();
public }
CNhanVien(string manv, string
tennv, bool gt, DateTime else
ngaysinh, double giolam, {
double tangca) MessageBox.Show("Ma NV " + nv.Manv + " da
{ ton tai. Khong the them!");
this.m_manv = }
manv;
this.m_tennv =
tennv; ResetControlValues(this);
this.m_gt = gt; }
this.m_ngaysinh =
ngaysinh;
this.m_giolam =
giolam;
//SUA
this.m_tangca = private void btnSua_Click(object sender,
tangca; EventArgs e)
} {
string ma = txtMaNV.Text;
}
}
CNhanVien nv = TimNV(ma);
//Cach 2
CNhanVien nv2 = nv;
if (nv2 != null)
{
nv2.Manv = txtMaNV.Text;
nv2.Tennv = txtTenNV.Text;
nv2.Ngaysinh = dateTimePicker1.Value;
nv2.Giolam =
Convert.ToDouble(txtSoNgayLam.Text.ToString());
nv2.Tangca =
Convert.ToDouble(txtSoGioTangCa.Text.ToString());
nv2.Gt = radioButton1.Checked;
MessageBox.Show("Sua nv thanh cong");
HienThiDSNV();
}
ResetControlValues(this);
}
}
//Xoa
private void btnXoa_Click(object sender,
EventArgs e)
{
string ma = txtMaNV.Text;
if (TimNV(ma) != null)
{
dsNhanVien.Remove(ma);
MessageBox.Show("Xóa thành công nhân
viên: " +ma +" !");
HienThiDSNV();
}
else
{
MessageBox.Show("Nhân viên " + ma + "
KHONG TON TAI !");
}
}
//Loadfile
void loadFile()
{
BinaryFormatter binaryFormatter = new
BinaryFormatter();
string strFileLocation = "dsPT.bin";
if (File.Exists(strFileLocation))
{
using (FileStream readerFileStream = new
FileStream(strFileLocation, FileMode.Open,
System.IO.FileAccess.Read))
{
dsNhanVien = (Dictionary<string,
CNhanVien>)binaryFormatter.Deserialize(readerFileStream);
}
}
}
//LUU
private void btnLuu_Click(object sender,
EventArgs e)
{
using (Stream file = File.Open("dsPT.bin",
FileMode.Create))
{
BinaryFormatter bf = new
BinaryFormatter();
bf.Serialize(file, dsNhanVien);
MessageBox.Show("Luu thành công!");
}
}
(mycontrols as TextBox).Text =
string.Empty;
}
radioButton1.Checked = false;
radioButton2.Checked = false;
}
}
}
Bài 2 :
CnhaSach form
namespace OnThi_bai2 namespace OnThi_bai2
{ {
[Serializable] public partial class Form1 : Form
class CNhaSach {
{ private Dictionary<string, CNhaSach> dsHangHoa;
private string m_ma,
m_ten, m_nhasx, m_loai; private void hienthiDSHH()
private DateTime {
m_ngaynhap; dataGridView1.DataSource =
private int m_sl; dsHangHoa.Values.ToList();
private double
m_dongia, m_thanhtien; txtslcl.Text = dsHangHoa.Count + "" ;
}
public string Ma { private CNhaSach timHH(string ma)
get => m_ma; set => m_ma = {
value; } try { return dsHangHoa[ma]; }
public string Ten { catch { return null; }
get => m_ten; set => m_ten = }
value; } public Form1()
public string Nhasx {
{ get => m_nhasx; set => InitializeComponent();
m_nhasx = value; } }
public string Loai {
get => m_loai; set => m_loai private void Form1_Load(object sender, EventArgs
= value; } e)
public DateTime {
Ngaynhap { get =>
m_ngaynhap; set => dsHangHoa = new Dictionary<string,
m_ngaynhap = value; } CNhaSach>();
public int Sl { get loadFile();
=> m_sl; set => m_sl = hienthiDSHH();
value; } }
public double Dongia
{ get => m_dongia; set => private void btnthem_Click(object sender,
m_dongia = value; } EventArgs e)
public double {
Thanhtien { get { return CNhaSach n = new CNhaSach();
m_thanhtien = m_sl *
m_dongia; } }
n.Ma = txtma.Text;
public CNhaSach() n.Ten = txtten.Text;
{ n.Nhasx = txtncc.Text;
m_ma = ""; n.Loai = cboLoai.Text;
m_ten = ""; n.Ngaynhap = dateTimePicker1.Value;
m_nhasx = ""; n.Sl =
m_loai = ""; (int)Convert.ToUInt32(txtsl.Text.ToString());
m_ngaynhap = n.Dongia =
DateTime.Now; (double)Convert.ToDouble(txtdongia.Text.ToString());
m_sl = 0; if (timHH(n.Ma) == null)
m_dongia = 0; {
} dsHangHoa.Add(n.Ma, n);
hienthiDSHH();
public }
CNhaSach(string ma, string else
ten, string nhasx, string {
loai, MessageBox.Show("Ma phieu thue " + n.Ma
DateTime + " da ton tai!");
ngaynhap, int sl, double }
dongia, double thanhtien) }
{
this.m_ma = ma; private void dataGridView1_RowEnter(object
this.m_ten = sender, DataGridViewCellEventArgs e)
ten; {
this.m_nhasx = txtma.Text =
nhasx; dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()
this.m_loai = ;
loai; txtten.Text =
this.m_ngaynhap dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()
= ngaynhap; ;
this.m_sl = sl; txtncc.Text =
this.m_dongia = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()
dongia; ;
this.m_thanhtien cboLoai.SelectedItem =
= thanhtien; dataGridView1.Rows[e.RowIndex].Cells[3].Value;
} dateTimePicker1.Value = (DateTime)
} dataGridView1.Rows[e.RowIndex].Cells[4].Value;
} txtsl.Text =
dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()
;
txtdongia.Text =
dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString()
;
txtthanhtien.Text =
dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString()
;
}
private void btnxoa_Click(object sender,
EventArgs e)
{
string ma = txtma.Text;
if (timHH(ma) != null)
{
dsHangHoa.Remove(ma);
MessageBox.Show("Xóa thành công nhân
viên: " + ma + " !");
hienthiDSHH();
}
else
{
MessageBox.Show("Nhân viên " + ma + "
KHONG TON TAI !");
}
}
//Cach 2
CNhaSach nv2 = nv;
if (nv2 != null)
{
nv2.Ma = txtma.Text;
nv2.Ten = txtten.Text;
nv2.Ngaynhap = dateTimePicker1.Value;
nv2.Nhasx = txtncc.Text;
nv2.Loai = cboLoai.Text;
nv2.Sl =
Convert.ToInt32(txtsl.Text.ToString());
nv2.Dongia =
Convert.ToDouble(txtdongia.Text.ToString());
ChangHoa.cs From1.cs
using System; using System;
using using System.Collections.Generic;
System.Collections.Generic; using System.ComponentModel;
using System.Linq; using System.Data;
using System.Text; using System.Drawing;
using System.Threading.Tasks; using System.IO;
using System.Linq;
namespace bai_4 using System.Runtime.Serialization.Formatters.Binary;
{ using System.Text;
[Serializable] using System.Threading.Tasks;
class CHangHoa using System.Windows.Forms;
{
private string m_ma, namespace bai_4
m_ten, m_loai, m_ghichu; {
private int m_slnhap, public partial class Form1 : Form
m_slxuat, m_tonkho; {
private DateTime
m_ngaynhap, m_ngayxuat; private Dictionary<string, CHangHoa> dsHH;
private void hienthiDSHangHoa()
public string Ma { get {
=> m_ma; set => m_ma = value; } dgvCuaHang.DataSource =
public string Ten { get dsHH.Values.ToList();
=> m_ten; set => m_ten = value;
} }
public string Loai { private CHangHoa timHH(string ma)
get => m_loai; set => m_loai = {
value; } try
public int Slnhap { get {
=> m_slnhap; set => m_slnhap = return dsHH[ma];
value; } }
public int Slxuat { get catch
=> m_slxuat; set => m_slxuat = {
value; } return null;
public int Tonkho { get }
=> m_tonkho = m_slnhap - }
m_slxuat; } public Form1()
public DateTime {
Ngaynhap { get => m_ngaynhap; InitializeComponent();
set => m_ngaynhap = value; } }
public DateTime
Ngayxuat { get => m_ngayxuat; private void Form1_Load(object sender,
set => m_ngayxuat = value; } EventArgs e)
public string Ghichu {
{ dsHH = new Dictionary<string,
get CHangHoa>();
{ loadFile();
if (Tonkho < hienthiDSHangHoa();
10) return "cần nhập thêm"; }
else return "";
} private void btnThem_Click(object sender,
} EventArgs e)
public CHangHoa() {
{ CHangHoa hs = new CHangHoa();
hs.Ma = txtMa.Text;
} hs.Ten = txtten.Text;
public CHangHoa(string hs.Ngaynhap = dateTimePicker1.Value;
ma, string ten, string laoi, hs.Ngayxuat = dateTimePicker2.Value;
int slnhap, int hs.Slnhap =
slxuat, DateTime ngaynhap, (int)Convert.ToInt32(txtnhap.Text.ToString());
DateTime ngayxuat) hs.Slxuat =
{ (int)Convert.ToInt32(txtxuat.Text.ToString());
m_ma = ma; if (rbA.Checked == true)
m_ten = ten; hs.Loai = "Sơn nước";
m_loai = Loai; else if (rbB.Checked == true)
m_slnhap = slnhap; hs.Loai = "Sơn dầu";
m_slxuat = Slxuat; else if (rbC.Checked == true)
m_ngaynhap = hs.Loai = "Sơn chống rủ";
ngaynhap; else hs.Loai = "Sơn PU";
m_ngayxuat = if (timHH(hs.Ma) == null)
ngayxuat; {
} dsHH.Add(hs.Ma, hs);
} hienthiDSHangHoa();
} }
else
{
MessageBox.Show("Ma HS " + hs.Ma +
"da ton tai. khong the them");
}
}
string ma = txtMa.Text;
CHangHoa hh = timHH(ma);
CHangHoa hh2 = hh;
if (hh2 != null)
{
hh2.Ten = txtten.Text;
hh2.Slnhap =
Convert.ToInt32(txtnhap.Text.ToString());
hh2.Slxuat =
Convert.ToInt32(txtxuat.Text.ToString());
hh2.Ngaynhap = dateTimePicker1.Value;
hh2.Ngayxuat = dateTimePicker2.Value;
if (rbA.Checked)
{
rbA.Checked = true;
}
else if (rbB.Checked)
{
rbB.Checked = true;
}
else if (rbC.Checked)
{
rbC.Checked = true;
}
else
rbD.Checked = true;
MessageBox.Show("Sửa thành công");
}
}
if (File.Exists(strFileLocation))
{
using (FileStream readerFileStream =
new FileStream(strFileLocation, FileMode.Open,
System.IO.FileAccess.Read))
{
dsHH = (Dictionary<string,
CHangHoa>)binFormatter.Deserialize(readerFileStream);
}
}
}
}
}
Lưu ý: tạo thư mục HoTen_MaSoSV_CK trong ổ đĩa E để lưu bài. Khi nộp nén
toàn bộ Thư mục đã tạo. Không nộp riêng lẻ file.cs.
Ví dụ: TranVanA_DH001_CK