Tuần 2 - Phần 2 - Câu 2
Thứ Bảy, 4 tháng 1, 2020
/
No Comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Book
{
private string m_Title;
private string m_Code;
private int m_Page;
public string Title { get => m_Title; set => m_Title = value; }
public string Code { get => m_Code; set => m_Code = value; }
public int Page { get => m_Page; set => m_Page = value; }
public Book()
{
this.Title = "";
this.Page = 0;
this.Code = "";
}
public Book(string code)
{
this.Code = code;
}
public Book(string title, string code)
{
this.Title = title;
this.Code = code;
}
public Book(string title, string code, int page)
{
this.Title = title;
this.Code = code;
this.Page = page;
}
public Book(Book bk)
{
this.Title = bk.Title;
this.Code = bk.Code;
this.Page = bk.Page;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Library
{
ArrayList m_BookShelf;
public Library()
{
m_BookShelf = new ArrayList();
}
public void AddBook(Book newBook)
{
m_BookShelf.Add(newBook);
}
public Book RemoveBook(string code)
{
int i = 0;
foreach (Book item in m_BookShelf)
{
if (item.Code == code)
m_BookShelf.RemoveAt(i);
i++;
}
return null;
}
public Book FindBook(string title)
{
foreach (Book item in m_BookShelf)
{
if (item.Title == title)
return item;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Program
{
static void Main(string[] args)
{
int lua_chon = 0;
Library lb = new Library();
do
{
Console.WriteLine("===== MENU =====");
Console.WriteLine("1. Nhap Sach");
Console.WriteLine("2. Tim Sach");
Console.WriteLine("3. Xoa Sach");
Console.WriteLine("0. Exit");
Console.Write("Ban muon chon?: ");
lua_chon = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
switch (lua_chon)
{
case 1:
Console.WriteLine("----- THEM SACH -----");
Console.Write("Nhap so luong sach: ");
int sl = int.Parse(Console.ReadLine());
for (int i = 0; i < sl; i++)
{
Book bk = new Book();
Console.Write("Nhap ten sach: ");
bk.Title = Console.ReadLine();
Console.Write("Nhap ma sach: ");
bk.Code = Console.ReadLine();
Console.Write("Nhap trang sach: ");
bk.Page = int.Parse(Console.ReadLine());
lb.AddBook(bk);
}
break;
case 2:
Console.WriteLine("----- TIM SACH -----");
Console.Write("Nhap ten sach can tim: ");
string ten = Console.ReadLine();
Book bk1 = new Book();
bk1 = lb.FindBook(ten);
if (bk1 != null)
{
Console.WriteLine("Da tim thay ["+ten+"]");
Console.WriteLine("Ten sach: "+bk1.Title);
Console.WriteLine("Ma sach: " + bk1.Code);
Console.WriteLine("Page sach: " + bk1.Page);
}
else
Console.WriteLine("Khong tim thay [" + ten + "] trong thu vien!");
break;
case 3:
Console.WriteLine("----- XOA SACH -----");
Console.Write("Nhap ma sach can xoa: ");
string ma_sach = Console.ReadLine();
//Book temp = new Book();
lb.RemoveBook(ma_sach);
break;
case 0:
Console.WriteLine("Ban da thoat chuong trinh!");
break;
default:
Console.WriteLine("Ban da chon sai! Hay chon lai!\n\n");
break;
}
} while (lua_chon != 0);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Cau2
{
public class Database
{
SqlConnection Connection;
SqlCommand cmd;
public Database()
{
string strConnect = "";
Connection = new SqlConnection(strConnect);
}
public DataTable ExecuteQuery(string query)
{
Connection.Open();
cmd = new SqlCommand(query, Connection);
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(data);
Connection.Close();
return data;
}
public int ExecuteNonQuery(string query)
{
int data = 0;
Connection.Open();
SqlCommand command = new SqlCommand(query, Connection);
data = command.ExecuteNonQuery();
Connection.Close();
return data;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Cau2
{
public class DocGia
{
public DataTable LoadData()
{
string query = "select *from DocGia";
DataTable result = new Database().ExecuteQuery(query);
return result;
}
public bool ThemDocGia(string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
{
string query = "insert into DocGia values(N'"+ten+"', '"+ngaysinh+"', N'"+dia_chi+"', '"+email+"', '"+ngay_lap+"', '"+ngay_het_han+"', "+tien_no+")";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
public bool XoaDocGia(int id)
{
string query = "delete from DocGia where MaDocGia = '"+id+"'";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
public bool SuaDocGia(string id, string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
{
string query = "Update DocGia set HoTenDocGia = N'"+ten+"', NgaySinh = '"+ngaysinh+"', DiaChi = N'"+dia_chi+"', Email = '"+email+ "', NgayLapThe = '"+ngay_lap+ "', NgayHetHan = '"+ngay_het_han+ "', TienNo = '"+tien_no+"' where MaDocGia = '" + id+"'";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
}
}
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 Cau2
{
public partial class fQuanLyDocGia : Form
{
public fQuanLyDocGia()
{
InitializeComponent();
}
private void fQuanLyDocGia_Load(object sender, EventArgs e)
{
HienThi();
}
void HienThi()
{
lsvViewDocGia.Items.Clear();
DataTable data = new DocGia().LoadData();
int i = 0;
foreach (DataRow row in data.Rows)
{
lsvViewDocGia.Items.Add(row["MaDocGia"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["HoTenDocGia"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgaySinh"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["DiaChi"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["Email"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgayLapThe"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgayHetHan"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["TienNo"].ToString());
i++;
}
}
private void btnView_Click(object sender, EventArgs e)
{
HienThi();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (new DocGia().ThemDocGia(txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
{
MessageBox.Show("Thêm thành công!");
HienThi();
}
else
MessageBox.Show("Thêm thất bại!");
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (new DocGia().XoaDocGia(int.Parse(txbID.Text)))
{
MessageBox.Show("Xóa thành công!");
HienThi();
}
else
MessageBox.Show("Xóa thất bại!");
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (new DocGia().SuaDocGia(txbID.Text,txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
{
MessageBox.Show("Sửa thành công!");
HienThi();
}
else
MessageBox.Show("Sửa thất bại!");
}
}
}
CREATE DATABASE qltv_db GO USE qltv_db GO CREATE TABLE DOCGia ( MaDocGia INT IDENTITY(1,1) PRIMARY KEY, HoTenDocGia NVARCHAR(30) NULL, NgaySinh DATE NULL, DiaChi NVARCHAR(30) NULL, Email CHAR(30) NULL, NgayLapThe DATE NULL, NgayHetHan DATE NULL, TienNo FLOAT NULL ) GO
Tên
Phiên Bản
|
Mô
Tả
|
Link
Download
|
|
Bản Dành Cho Sinh Viên
(sinh viên nên dùng bản này, đọc code
sẽ dễ hiểu hơn)
|
Homework
.NET - Beta 1.1.0
|
- Chỉ
mói update xong 7 bài tập tuần 1
|
|
Homework
.NET Beta 2.1.4
|
-
Update thêm bài tập tuần 2
|
||
Homework
dot Net Beta 3.1.2
|
-
Update thêm bài tập tuần 3
|
||
Bản Dành Cho Giáo Viên
|
Homework
dot Net Beta 3.5.6
|
- Updete tới tuần 4, Lap01 -> Lap 09
|
|
using System.Runtime.InteropServices;
[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private extern static int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
lbName.Location = new Point((pnlAvartar.Width - lbName.Width)/2-5 , lbName.Location.Y);
lbDongHo.Text = DateTime.Now.ToString("hh:mm tt");
lbDongHo.Text = DateTime.Now.ToString("hh:mm ss");
public bool IsNumber(string pValue)
{
foreach (Char c in pValue)
{
if (!Char.IsDigit(c))
return false;
}
return true;
}
[DllImport("user32.dll")]public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);sing System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
public Form1()
{
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Capture = false;
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
base.OnMouseDown(e);
}
}
}
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool isMouseDown;
int xLast;
int yLast;
public Form1()
{
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
isMouseDown = true;
xLast = e.X;
yLast = e.Y;
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isMouseDown)
{
int newY = this.Top + (e.Y - yLast);
int newX = this.Left + (e.X - xLast);
this.Location = new Point(newX, newY);
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
isMouseDown = false;
base.OnMouseUp(e);
}
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x1;
const int HTCAPTION = 0x2;
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bai3
{
public partial class fBai3 : Form
{
public fBai3()
{
InitializeComponent();
if (Test() == false)
{
btnCout.Enabled = false;
btnClear.Enabled = false;
}
}
private bool Test()
{
if ((txbInputA.Text.Length == 0 || txbInputB.Text.Length == 0) || ((!Char.IsDigit(txbInputA.Text[txbInputA.Text.Length - 1])) || (!Char.IsDigit(txbInputB.Text[txbInputB.Text.Length - 1]))))
return false;
return true;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult r;
r = MessageBox.Show("Thí chủ muốn thoát chứ?",
"THÔNG BÁO NÈ!", MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
if (r == DialogResult.No)
e.Cancel = true;
}
private void txbInputA_TextChanged(object sender, EventArgs e)
{
if (Test() == false)
{
btnCout.Enabled = false;
btnClear.Enabled = false;
}
else if (Test() == true)
{
btnCout.Enabled = true;
btnClear.Enabled = true;
}
//////////////////////////
///
Control ctr = (Control)sender;
if(ctr.Text.Length >0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1]))
{
this.erroNotify.SetError(ctr, "This is not value number!");
txbResult.Text = "Thí chủ nhấn nhầm rồi kìa!";
}
else
{
this.erroNotify.Clear();
txbResult.Text = "";
}
}
private void txbInputB_TextChanged(object sender, EventArgs e)
{
if (Test() == false)
{
btnCout.Enabled = false;
btnClear.Enabled = false;
}
else if (Test() == true)
{
btnCout.Enabled = true;
btnClear.Enabled = true;
}
////////////////
Control ctr = (Control)sender;
if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1]))
{
this.erroNotify.SetError(ctr, "This is not value number!");
txbResult.Text = "Thí chủ nhấn nhầm rồi kìa!";
}
else
{
this.erroNotify.Clear();
txbResult.Text = "";
}
}
private void btnClear_Click(object sender, EventArgs e)
{
ClearData();
}
private void ClearData()
{
txbInputA.Clear();
txbInputB.Clear();
txbResult.Clear();
txbInputA.Focus();
}
private void btnCout_Click(object sender, EventArgs e)
{
int a, b;
double c;
if (txbInputA.Text.Length == 0 && txbInputB.Text.Length == 0)
{
txbResult.Text = "Thí chủ không có gì để tính toán cả!";
}
else if (txbInputA.Text.Length == 0 || txbInputB.Text.Length == 0)
{
txbResult.Text = "Thí chủ nhập thiếu rồi kìa!";
}
else
{
a = Convert.ToInt32(txbInputA.Text);
b = Convert.ToInt32(txbInputB.Text);
if (a == 0 && b == 0)
txbResult.Text = "Phương trình có cả đống nghiệm!";
else if(a == 0 && b!= 0)
txbResult.Text = "Phương trình vô nghiệm rồi bạn!";
else if(a != 0)
{
c = (double)-b / (double)a;
c = Math.Round(c, 3);
txbResult.Text = "X = " + c.ToString();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Week1_Bai1
{
public partial class fBai1Edit : Form
{
public fBai1Edit()
{
InitializeComponent();
}
private void fBai1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult r;
r = MessageBox.Show("Thí chủ có muốn đóng chương trình không?", "Chú Ý!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
if (r == DialogResult.No)
e.Cancel = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txbName.Clear();
txbYear.Clear();
txbName.Focus();
}
private void txbName_Leave(object sender, EventArgs e)
{
Control ctr = (Control)sender;
if (ctr.Text.Trim().Length == 0)
this.errorProvider1.SetError(ctr, "Bạn đã để trống!");
else
this.errorProvider1.Clear();
}
private void txbYear_TextChanged(object sender, EventArgs e)
{
Control ctr = (Control)sender;
if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1]))
this.errorProvider1.SetError(ctr, "Đây không phải giá trị số");
else
this.errorProvider1.Clear();
}
private void btnShow_Click(object sender, EventArgs e)
{
try
{
int age = DateTime.Now.Year - Convert.ToInt32(txbYear.Text);
string s = txbName.Text;
if (s == "")
s = "Vô danh tiểu tốt!";
else
s = txbName.Text;
MessageBox.Show("Bạn tên là: " + s + "\n\nTuổi: " + age.ToString(), "Thông tin của bạn!");
}
catch
{
if (txbName.Text.Length != 0 && txbYear.Text.Length == 0)
{
MessageBox.Show("Bạn tên là: " + txbName.Text + "\n\nTuổi: 0", "Thông tin của bạn!");
}
else if(txbName.Text.Length == 0 && txbYear.Text.Length == 0)
MessageBox.Show("Bạn chưa nhập gì cả!", "THÔNG BÁO!", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bai2
{
public partial class fBai2 : Form
{
const int USD = 17861;
const int EUR = 27043;
public fBai2()
{
InitializeComponent();
}
private void fBai2_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult r;
r = MessageBox.Show("Thí chủ có muốn thoát chăng?",
"Thông báo!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1); ;
if (r == DialogResult.No)
e.Cancel = true;
}
private void txbInputChange_TextChanged(object sender, EventArgs e)
{
Control ctr = (Control)sender;
if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1]))
this.errorProvider1.SetError(ctr, "This is not value number!");
else
this.errorProvider1.Clear();
}
private void btnVNDtoUSD_Click(object sender, EventArgs e)
{
try
{
double moneyInput = Convert.ToDouble(txbInputChange.Text);
double result = moneyInput / (double)USD;
result = Math.Round(result, 2);
txbResultChange.Text = result.ToString() + " USD";
}
catch
{
txbResultChange.Text = "0 USD";
}
}
private void btnVNDtoEUR_Click(object sender, EventArgs e)
{
try
{
double moneyInput = Convert.ToDouble(txbInputChange.Text);
double result = moneyInput / (double)EUR;
result = Math.Round(result, 2);
txbResultChange.Text = result.ToString() + " EUR";
}
catch
{
txbResultChange.Text = "0 EUR";
}
}
private void btnUSDtoVND_Click(object sender, EventArgs e)
{
try
{
double moneyInput = Convert.ToDouble(txbInputChange.Text);
double result = moneyInput * USD;
result = Math.Round(result, 2);
txbResultChange.Text = result.ToString() + " VND";
}
catch
{
txbResultChange.Text = "0 VND";
}
}
private void btnEURtoVND_Click(object sender, EventArgs e)
{
try
{
double moneyInput = Convert.ToDouble(txbInputChange.Text);
double result = moneyInput * EUR;
result = Math.Round(result, 2);
txbResultChange.Text = result.ToString() + " VND";
}
catch
{
txbResultChange.Text = "0 VND";
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Week1_Bai1
{
public partial class fBai1 : Form
{
public fBai1()
{
InitializeComponent();
}
private void fBai1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult r;
r = MessageBox.Show("Thí chủ có muốn đóng chương trình không?", "Chú Ý!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
if (r == DialogResult.No)
e.Cancel = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txbName.Clear();
txbYear.Clear();
txbName.Focus();
}
private void txbName_Leave(object sender, EventArgs e)
{
Control ctr = (Control)sender;
if (ctr.Text.Trim().Length == 0)
this.errorProvider1.SetError(ctr, "Bạn đã để trống!");
else
this.errorProvider1.Clear();
}
private void txbYear_TextChanged(object sender, EventArgs e)
{
Control ctr = (Control)sender;
if (ctr.Text.Length > 0 && !Char.IsDigit(ctr.Text[ctr.Text.Length - 1]))
this.errorProvider1.SetError(ctr, "Đây không phải giá trị số");
else
this.errorProvider1.Clear();
}
private void btnShow_Click(object sender, EventArgs e)
{
int age = DateTime.Now.Year - Convert.ToInt32(txbYear.Text);
MessageBox.Show("Bạn tên là: " + txbName.Text + "\n\nTuổi: " + age.ToString());
}
}
}