0% found this document useful (0 votes)
28 views14 pages

Public Partial Class Private String: Using Using Using Using Using Namespace

The document discusses a class called CNhanVien that represents employees with properties like ID, name, gender, date of birth, working hours, overtime hours, and salary. It also includes a form that allows adding, editing, removing employees from a dictionary storage and loading/saving from a binary file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views14 pages

Public Partial Class Private String: Using Using Using Using Using Namespace

The document discusses a class called CNhanVien that represents employees with properties like ID, name, gender, date of birth, working hours, overtime hours, and salary. It also includes a form that allows adding, editing, removing employees from a dictionary storage and loading/saving from a binary file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

BÀI 1:

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);
}

private void dataGridView1_CellFormatting(object


sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name
== "gioiTinh")
//if (e.ColumnIndex == 3)
{
if (e.Value is bool)
{
bool value = (bool)e.Value;
e.Value = (value) ? "Nam" : "Nu";
//e.FormattingApplied = true;
}
}

private void dataGridView1_RowEnter(object


sender, DataGridViewCellEventArgs e)
{
txtMaNV.Text =
dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
txtTenNV.Text =
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
bool gt =
Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[2]
.Value.ToString());
if (gt)
{
radioButton1.Checked=true;
}
else
{
radioButton2.Checked = true;
}
dateTimePicker1.Value =(DateTime)
dataGridView1.Rows[e.RowIndex].Cells[3].Value;
txtSoNgayLam.Text =
dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
txtSoGioTangCa.Text =
dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
txtLuong.Text =
dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();

}
//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!");
}
}

//reset giá trị sau khi nhập


public void ResetControlValues(Control Parent)
{

foreach (Control mycontrols in


Parent.Controls)
if (mycontrols is TextBox)
{

(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 !");

}
}

private void btnsua_Click(object sender,


EventArgs e)
{
string ma = txtma.Text;
CNhaSach nv = timHH(ma);

//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());

MessageBox.Show("Sua nv thanh cong");


hienthiDSHH();
}
}
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))
{
dsHangHoa = (Dictionary<string,
CNhaSach>)binaryFormatter.Deserialize(readerFileStream);
}
}
}
private void btnluu_Click(object sender,
EventArgs e)
{
using (Stream file = File.Open("dsPT.bin",
FileMode.Create))
{
BinaryFormatter bf = new
BinaryFormatter();
bf.Serialize(file, dsHangHoa);
MessageBox.Show("Luu thành công!");
}
}
}
}
Bài 4 :

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");
}
}

private void dgvCuaHang_RowEnter(object


sender, DataGridViewCellEventArgs e)
{
txtMa.Text =
dgvCuaHang.Rows[e.RowIndex].Cells[0].Value.ToString()
;
txtten.Text =
dgvCuaHang.Rows[e.RowIndex].Cells[1].Value.ToString()
;
txtnhap.Text =
dgvCuaHang.Rows[e.RowIndex].Cells[3].Value.ToString()
;
txtton.Text =
dgvCuaHang.Rows[e.RowIndex].Cells[5].Value.ToString()
;
dateTimePicker1.Value =
(DateTime)dgvCuaHang.Rows[e.RowIndex].Cells[6].Value;
dateTimePicker2.Value =
(DateTime)dgvCuaHang.Rows[e.RowIndex].Cells[7].Value;
string loai =
dgvCuaHang.Rows[e.RowIndex].Cells[2].Value.ToString()
;
if (loai == "Sơn nước")
{
rbA.Checked = true;
}
else if (loai == "Sơn dầu")
{
rbB.Checked = true;
}
else if (loai == "Sơn chống rủ")
{
rbC.Checked = true;
}
else
rbD.Checked = true;

private void btnSua_Click(object sender,


EventArgs e)
{

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");
}
}

private void btnXoa_Click(object sender,


EventArgs e)
{
string ma = txtMa.Text;
if (timHH(ma) != null)
{
dsHH.Remove(ma);
hienthiDSHangHoa();
}
else
MessageBox.Show("Không tìm thấy mã
phiếu cần xóa !!!");

private void btnLuu_Click(object sender,


EventArgs e)
{
using (Stream file =
File.Open("dsHH.bin", FileMode.Create))
{
BinaryFormatter bf = new
BinaryFormatter();
bf.Serialize(file, dsHH);
}
}
private void loadFile()
{
BinaryFormatter binFormatter = new
BinaryFormatter();
string strFileLocation = "dsHH.bin";

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

You might also like