Tải bản đầy đủ (.pdf) (6 trang)

Báo Cáo Và Code từ điển Anh - việt C#

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (339.38 KB, 6 trang )

BÁO CÁO BÀI TẬP LỚN
CHỦ ĐỀ TỪ ĐIỂN ANH – VIỆT
I.THÀNH VIÊN :
Nguyễn Huy Du
Viên Hồng Quang
Nguyễn Tuấn Cương
II. code từ điển anh – việt
1. Tạo Form có giao diện như sau :

namespace tudien
{
public partial class Dic : Form
{
public Dic()
{
InitializeComponent();
}
// tao danh sach tu dien co ten lst
private List<tudien> lst = new List<tudien>();
//tao phuong thuc doc file text
public void ReadFile()
{


FileStream fs = new FileStream("work.txt", FileMode.Open, FileAccess.Read,
FileShare.None);
StreamReader sr = new StreamReader(fs);
lst.Clear();
string str = "";
str = sr.ReadLine();
while (str != null)


{
string[] Arr = str.Split('|');
tudien temp = new tudien(Arr[0], Arr[1]);
lst.Add(temp);
str = sr.ReadLine();
}
//dong file text
sr.Close();
fs.Close();
}
//do file vao arayList
public void Fill()
{
lsttuta.Items.Clear();
for (int i = 0; i < lst.Count; i++)
{
lsttuta.Items.Add(lst[i].TUTA);
}
lsttuta.Sorted = true;
}
private void Form1_Load(object sender, EventArgs e)
{
ReadFile();
Fill();
}
private void txttuta_TextChanged(object sender, EventArgs e)
{
lsttuta.Items.Clear();
for (int i = 0; i < lst.Count; i++)
{

if (lst[i].TUTA.StartsWith(txttuta.Text))
{
lsttuta.Items.Add(lst[i].TUTA);
lsttuta.SelectedIndex = 0;
}
}
}
private void lsttuta_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < lst.Count; i++)
{
if (lsttuta.SelectedItem.Equals(lst[i].TUTA))
{
lbltutv.Text = "Từ: " + lst[i].TUTA + " \n có nghĩa là: " +
lst[i].TUTV;
}


}
}
private void btnthoat_Click(object sender, EventArgs e)
{
Close();
}
private void btnquanly_Click(object sender, EventArgs e)
{
Form2 s = new Form2();
s.Show();
this.Hide();
}

}
}

- Tạo 1 form quản lí có giao diện như sau:

namespace tudien
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
// tao danh sach tu dien co ten lst
private List<tudien> lst = new List<tudien>();
//tao phuong thuc doc file text
public void ReadFile1()
{
FileStream fs = new FileStream("work.txt", FileMode.Open, FileAccess.Read,
FileShare.None);
StreamReader sr = new StreamReader(fs);
lst.Clear();
string str = "";


str = sr.ReadLine();
while (str != null)
{
string[] Arr = str.Split('|');
tudien temp = new tudien(Arr[0], Arr[1]);

lst.Add(temp);
str = sr.ReadLine();
}
//dong file text
sr.Close();
fs.Close();
}
//do file vao arayList
public void Fill1()
{
lsttuta.Items.Clear();
for (int i = 0; i < lst.Count; i++)
{
lsttuta.Items.Add(lst[i].TUTA);
}
lsttuta.Sorted = true;
}
//phuong thuc viet file
private void WriteFile()
{
FileStream fs = new FileStream("work.text", FileMode.Create,
FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < lst.Count; i++)
{
sw.WriteLine(lst[i].ToString());
}
sw.Flush();
sw.Close();
fs.Close();

}
private void btnclose_Click(object sender, EventArgs e)
{
Dic s = new Dic();
s.Show();
this.Hide();
}
private void lsttuta_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < lst.Count; i++)
{
if (lsttuta.SelectedItem.Equals(lst[i].TUTA))
{
txttuta1.Text = lst[i].TUTA;
txttutv1.Text = lst[i].TUTV;
}
}
}
private void Form2_Load(object sender, EventArgs e)
{


ReadFile1();
Fill1();
}
private void btnthem_Click(object sender, EventArgs e)
{
if (txttuta.Text == "" || txttutv.Text == "")
{
MessageBox.Show("nhập đầy đủ thông tin", "thông báo",

MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
tudien a = new tudien(txttuta.Text, txttutv.Text);
lst.Add(a);
lsttuta.Items.Add(a.TUTA);
MessageBox.Show("thêm từ thành công");
}
WriteFile();
}
private void button1_Click(object sender, EventArgs e)
{
if (lsttuta.SelectedIndex == -1)
{
MessageBox.Show("chọn từ cần xoá");
}
else
{
if (MessageBox.Show("bạn có muốn xoá từ này không", "thông báo",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) ;
{
for (int i = 0; i < lst.Count; i++)
{
if (lsttuta.SelectedItem.Equals(lst[i].TUTA))
{
lst.Remove(lst[i]);
try
{
lsttuta.Items.Remove(lst[i].TUTA);

}
catch { }
WriteFile();
MessageBox.Show("xoá từ thành công");
}
}
}
Fill1();
}
}
private void btnluu_Click(object sender, EventArgs e)
{
if (lsttuta.SelectedIndex == -1)
{
MessageBox.Show("Chọn từ cần sửa !!");
}
else


{
for (int i = 0; i < lst.Count; i++)
{
if (lsttuta.SelectedItem.Equals(lst[i].TUTA))
{
lst[i].TUTA = txttuta1.Text;
lst[i].TUTV = txttutv1.Text;
WriteFile();
MessageBox.Show("đã sửa xong !!");
}
}

}
}
}
}

- Tạo 1 lớp class tử điển
-

namespace tudien
{
class tudien
{
//khai bao truong
private string tuTA;
private string tuTV;
//khai bao get , set
public string TUTA
{
get { return tuTA; }
set { tuTA = value; }
}
public string TUTV
{
get { return tuTV; }
set { tuTV = value; }
}
//khai bao Constuctor
public tudien() { }
public tudien(string tuTA, string tuTV)
{

this.TUTA = tuTA;
this.TUTV = tuTV;
}
//phuong thuc ghi de phuong thuc tra ve chuoi cua doi tuong tu dien
public override string ToString()
{
return TUTA + "|" + TUTV;
}
}
}



×