0% found this document useful (0 votes)
28 views

Code LTGD

This document contains code for several programs related to dice games and employee management in C#: 1. The first section contains code for a dice guessing game, including functions for selecting dice, rolling dice, tracking wins/losses, and displaying results. 2. The second section contains code for a simple dice rolling program that generates random numbers for 3 dice and displays the results. 3. The third section contains code for an employee management program, including login validation, adding/removing employees from a list, and closing the program gracefully.

Uploaded by

Duong Phan
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 views

Code LTGD

This document contains code for several programs related to dice games and employee management in C#: 1. The first section contains code for a dice guessing game, including functions for selecting dice, rolling dice, tracking wins/losses, and displaying results. 2. The second section contains code for a simple dice rolling program that generates random numbers for 3 dice and displays the results. 3. The third section contains code for an employee management program, including login validation, adding/removing employees from a list, and closing the program gracefully.

Uploaded by

Duong Phan
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/ 24

Đoán xúc xắc

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 DoanXucXac
{
public partial class Form1 : Form
{
String pathImg;
Random rand = new Random();
//nchoose: nguoi dung chon, count so lan doan, win: so lan thang
//lose: so lan thua
int nChoose, nCount, nWin, nlose;
string kq = " ";
private void bt1_Click(object sender, EventArgs e)
{
Button bt = (Button) sender;
nChoose = int.Parse(bt.Text);
picSo1.Image = Image.FromFile(pathImg + bt.Text + ".jpg");

private void btQuaySo_Click(object sender, EventArgs e)


{

}
private void QuaySo()
{
int n = rand.Next(1, 7);
picSo2.Image = Image.FromFile(pathImg + n.ToString() + ".jpg");
if(n == nChoose)
{
nWin++;
}
else
{
nlose++;
}
label1.Text = String.Format("Lan doan: {0} ", nCount);
label2.Text = String.Format("Lan thang: {0} ({1:0.##}%", nWin, (double)nWin *
100 / nCount);
label3.Text = String.Format("Lan thua: {0} ({1:0.##}%", nlose, (double)nlose
* 100 / nCount);
libKetQua.Items.Add ( String.Format("{0} . {1} (Doan {2} ra {3}).", nCount,
kq, nChoose, n));

}
public Form1()
{
InitializeComponent();
}

private void label2_Click(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{
pathImg = Application.StartupPath + @"\hinhxucxac\";
init();
}
private void init()
{
nChoose = 1;
nCount = nWin = nlose = 0;
picSo1.Image = Image.FromFile(pathImg + nChoose.ToString() + ".jpg");
picSo2.Image = null;
label1.Text = label2.Text = label3.Text = " ";
libKetQua.Items.Clear();
}
protected override bool ProcessDialogKey(Keys keyData)
{
switch(keyData)
{
case Keys.Enter:QuaySo();break;
case Keys.Escape: init();break;
}
return true;
}
}
}
////////////////////////////////////////////////////////////

Quay số

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 QuaySo
{
public partial class Form1 : Form
{
Random r = new Random();
string path; //đường dẫn đến file hình
public Form1()
{
InitializeComponent();
}
private void btQuaySo_Click(object sender, EventArgs e)
{
int so1, so2, so3;
//sinh số ngẫu nhiên cho 3 số
so1 = r.Next(1, 7);
so2 = r.Next(1, 7);
so3 = r.Next(1, 7);
pic1.Image = Image.FromFile(path + so1.ToString() + ".jpg");
pic2.Image = Image.FromFile(path + so2.ToString() + ".jpg");
pic3.Image = Image.FromFile(path + so3.ToString() + ".jpg");
lbKetQua.Text = (so1 + so2 + so3).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
//đường dẫn vào debug và vào các hình, mỗi xuyệt là đi 1 cấp thư mục
path = Application.StartupPath + @"\hinhxucxac\";

private void lbKetQua_Click(object sender, EventArgs e)


{

private void btDong_Click(object sender, EventArgs e)


{
DialogResult rs = new DialogResult();
rs = MessageBox.Show("Bạn có muốn thoát?", "Thông báo",
MessageBoxButtons.YesNo);
if (rs == DialogResult.Yes)
this.Close();
}

private void lbTen_Click(object sender, EventArgs e)


{

private void timer1_Tick(object sender, EventArgs e)


{
lbTen.Text = lbTen.Text.Substring(1) + lbTen.Text.Substring(0, 1);
//cắt số đầu tiên ra, lấy từ vị trí 1
}
}
}
////////////////////////////////////////////////////////////////
Quản lí nhân viên
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLNV
{
public partial class FrmDangNhap : Form
{
public FrmDangNhap()
{
InitializeComponent();
}

private void btDong_Click(object sender, EventArgs e)


{
//đây là button dang nhap
if (txtTenDangNhap.Text == "" || txtMatKhau.Text != "admin")
Application.Exit();
else
{
FrmMain.Tendangnhap = txtTenDangNhap.Text;
this.Close();
}
}

private void button1_Click(object sender, EventArgs e)


{
//button dong
Application.Exit();
}

private void FrmDangNhap_FormClosing(object sender, FormClosingEventArgs e)


{
if (FrmMain.Tendangnhap == "")
Application.Exit();
}
}
}
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 QLNV
{
public partial class FrmMain : Form
{
public static string Tendangnhap = "";
public bool bClose;
public FrmMain()
{
FrmDangNhap f = new FrmDangNhap();
//làm việc trên frmDangNhap xong thì mới có thể làm trên form khác
f.ShowDialog();
InitializeComponent();
}

private void btThem_Click(object sender, EventArgs e)


{
if (txtHoTen.Text == "")
return;
ListViewItem item = new ListViewItem(txtHoTen.Text);
item.SubItems.Add(dtNgaysinh.Value.ToString());
item.SubItems.Add(radioButton1.Checked ? "Nam" : "Nữ");
item.ImageIndex = radioButton1.Checked ? 0 : 1;
listNhanVien.Items.Add(item);
txtHoTen.Text = " ";
txtHoTen.Focus();
}

private void FrmMain_Load(object sender, EventArgs e)


{
lbTendangnhap.Text = Tendangnhap;
bClose = false;
}

private void btXoa_Click(object sender, EventArgs e)


{
foreach (ListViewItem item in listNhanVien.SelectedItems)
listNhanVien.Items.Remove(item);
}

private void timer1_Tick(object sender, EventArgs e)


{
this.Opacity = 0.05;
if(this.Opacity <= 0)
{
bClose = true;
Application.Exit();//nhiều form trong 1 chương trình sẽ đóng hết. Không
xài this.Close()
}
}

private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)


{
if(!bClose)
{
e.Cancel = true;
timer1.Enabled = true;
}
}
}
}
///////////////////////////////////////////////////////
BTHC5
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;
using System.IO; // thư viện làm việc với file

namespace BTHC5
{
public partial class Form1 : Form
{
int count = 0;
Point bold; //biến lưu vị trí nhấn chuột
Random rand = new Random();
string []arrfiles;
int ImgWidth = 90;// kích thước hình

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
label1.Text = "Dùng chuột hoặc các dấu mũi tên kéo hình và khung";
//lấy danh sách các file hình trong thư mục Image vào mang
arrfiles = Directory.GetFiles(Application.StartupPath + @"\Image\");
AddNewPicture();

}
private void AddNewPicture()
{
count++;
string file = arrfiles[rand.Next(arrfiles.Length)];//random trong mảng các
hình
Image img = Image.FromFile(file);
//tạo picturebox
PictureBox pic = new PictureBox();
pic.Name = count.ToString();
pic.Image = img;
pic.Location = new Point(0, 0);
pic.Width = pic.Height = ImgWidth;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
//cài sự kiện trên PictureBox
pic.MouseDown += Pic_MouseDown;
pic.MouseMove += Pic_MouseMove;
pic.MouseUp += Pic_MouseUp;
this.Controls.Add(pic); // add picturebox vào Form
//Sắp xếp lại pnBound
this.Controls.SetChildIndex(pnBound, this.Controls.Count - 1);

private void Pic_MouseUp(object sender, MouseEventArgs e)//sự kiện nhả chuột


{
PictureBox pic = (PictureBox)sender;
if (pnBound.Bounds.Contains(pic.Bounds)) // picturebox nằm trong panel,
Bound: Kiểm tra đường viền
{
//Them picturebox vào panel
pnBound.Controls.Add(pic);
//sau khi add pic vào pnBound thì gọi hàm add thêm hình vào góc trái
AddNewPicture();

}
}
private void Pic_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left) // giữ chuột trái khi di chuyển
{
PictureBox pic = (PictureBox)sender;
int dx = e.X - bold.X; //khoảng cách dịch chuyển
int dy = e.Y - bold.Y;
pic.Left += dx;
pic.Top += dy;

}
}

private void Pic_MouseDown(object sender, MouseEventArgs e)


{
bold = e.Location;
}

private void Form1_KeyDown(object sender, KeyEventArgs e)


{
Control[] arrControl = this.Controls.Find(count.ToString(), false);
PictureBox pic = (PictureBox)arrControl[0];
switch(e.KeyCode)
{
case Keys.Left://trường hợp nhấn phím mũi tên trái
if (pic.Left > 0)
pic.Left--;break;
case Keys.Right://nhấn mũi tên phải
if(pic.Right < ClientRectangle.Width)
pic.Left++; break;
case Keys.Up://nhấn lên
if (pic.Top > 0)
pic.Top--;
break;
case Keys.Down://nhấn xuống
if (pic.Bottom < ClientRectangle.Height)
pic.Top++;
break;
}
if(pnBound.Bounds.Contains(pic.Bounds))
{
pnBound.Controls.Add(pic);
AddNewPicture();

private void pnBound_MouseDown(object sender, MouseEventArgs e)


{
bold = e.Location;
}

private void pnBound_MouseMove(object sender, MouseEventArgs e)


{
if(e.Button == MouseButtons.Left)
{
//Lấy khoảng cách dịch chuyển bằng cách lấy tọa độ điểm mới - cho tạo độ
điểm ban đầu
//sau lấy vị trí hiện tại + khoảng cách dịch chuyển để ra vị trí mới

pnBound.Left += (e.X - bold.X);


pnBound.Top += (e.Y - bold.Y);

}
}
/////////////////////////////////////////////
BTHC6
Chuỗi
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 BTH06_BT1
{
public partial class Form1 : Form
{
string str = "Khoa CNTT, Đại học Mở TP.HCM";

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
timer1.Enabled = true;
lbName.Text = " CHƯƠNG TRÌNH MINH HỌA XỬ LÍ CHUỖI ";
txtS1.Text = str;

private void btInsert_Click(object sender, EventArgs e)


{
try
{
int pos = int.Parse(txtInsertPos.Text);
if (pos > 0 || pos < txtS1.Text.Length)
{
txtS1.Text = txtS1.Text.Insert(pos, txtS2.Text);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
}
}

private void btReplace_Click(object sender, EventArgs e)


{
txtS1.Text = txtS1.Text.Replace(txtS2.Text, txtS3.Text);
}

private void btDelete2_Click(object sender, EventArgs e)


{
int pos = txtS1.Text.IndexOf(txtS2.Text);
while(pos >= 0) // tron khi còn thấy s2 trong s1 thì xóa s2
{
txtS1.Text = txtS1.Text.Remove(pos, txtS2.Text.Length);
pos = txtS1.Text.IndexOf(txtS2.Text);
}
}

private void btSubstring_Click(object sender, EventArgs e)


{
int pos = int.Parse(txtStartPos.Text);
int numchar = int.Parse(txtNumChar.Text);
txtS2.Text = txtS1.Text.Substring(pos, numchar);

private void btReset_Click(object sender, EventArgs e)


{
txtS1.Text = str;
}

private void btReserve_Click(object sender, EventArgs e)


{
char[] token = { ' ', '\t' };
//tách từ s1 vào arr theo kí tuej của token
string[] arr = txtS1.Text.Split(token,
StringSplitOptions.RemoveEmptyEntries);
//đảo mảng
Array.Reverse(arr);
txtS1.Text = String.Join(" ", arr); // chuyển mảng thành chuỗi

private void btNormal_Click(object sender, EventArgs e)


{
char[] token = { ' ', '\t' };
//tách từ s1 vào arr theo kí tuej của token
string[] arr = txtS1.Text.Split(token,
StringSplitOptions.RemoveEmptyEntries);
//Duyệt từng phần tử trong mảng, chuyển hoa kí tự đầu chuyển thường các kí tự
còn lại

for(int i = 0; i < arr.Length; i++)


{
arr[i] = arr[i].Substring(0, 1).ToUpper() +
arr[i].Substring(1).ToLower();

}
txtS1.Text = String.Join(" ", arr);
}

private void timer1_Tick(object sender, EventArgs e)


{
lbName.Text = lbName.Text.Substring(1) + lbName.Text.Substring(0, 1);
}

private void txtInsertPos_TextChanged(object sender, EventArgs e)


{

}
}
}
Mảng
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 BTHC6B2
{
public partial class Form1 : Form
{
int[] arrInt; // mảng ban đầu
int[] arrTang;
int[] arrGiam;
int[] arrDao;
int[] arrChanLe;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void btTaoMang_Click(object sender, EventArgs e)


{
txtSoMangKhoiTao.Text = " ";
txtSoMangTang.Text = " ";
txtSoMangGiam.Text = " ";
txtSooMangDao.Text = " ";
txtSoMangChanLe.Text = " ";
try {
int sopt = int.Parse(txtSoPT.Text);
arrInt = new int[sopt];
Random rand = new Random();
for(int i = 0; i < sopt; i++)
{
arrInt[i] = rand.Next(0, 100);
}
ShowArray(arrInt, txtSoMangKhoiTao);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}
//Hàm hiển thị phần tử của mảng lên textbox
private void ShowArray(int[]a, TextBox textbox)
{
textbox.Text = String.Empty;//khởi tạo chuỗi rỗng
for(int i = 0; i < a.Length; i++)
textbox.Text += a[i].ToString() + " , ";// + dồn a[i] đã chuyển thành
chuỗi
textbox.Text = textbox.Text.TrimEnd();
}

private void btSapXep_Click(object sender, EventArgs e)


{
Sort();
SortSpecial();
}
private void Sort()
{
arrTang = new int[arrInt.Length];
arrGiam = new int[arrInt.Length];
arrDao = new int[arrInt.Length];
try {
//săp xếp mảng tăng
Array.Copy(arrInt, arrTang, arrInt.Length);
Array.Sort(arrTang);
ShowArray(arrTang, txtSoMangTang);
//Sắp xếp mảng giảm: đảo mảng đã có thứ tự tăng
Array.Copy(arrTang, arrGiam, arrTang.Length);
Array.Reverse(arrGiam);
ShowArray(arrGiam, txtSoMangGiam);
//Đảo mảng ban đầu
Array.Copy(arrInt, arrDao, arrInt.Length);
Array.Reverse(arrDao);
ShowArray(arrDao, txtSooMangDao);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SortSpecial()
{
arrChanLe = new int[arrInt.Length];
Array.Copy(arrInt, arrChanLe, arrInt.Length);
//dồn chẵn về bên trái, lẻ về bên phải, trái tăng, phải giảm
int i = 0, j = arrChanLe.Length;
while(i < j)
{
if(arrChanLe[i] % 2 == 0)
{
i++;
}
if (arrChanLe[i] % 2 != 0)
j--;
if(i < j)
{
//Nếu trái lẻ, phải chẵn --> đổi chỗ
if(arrChanLe[i] % 2 != 0 && arrChanLe[j] % 2 == 0 )
{
swap(ref arrChanLe[i], ref arrChanLe[j]);
i++;
j--;
}
}
}
//sau vòng lặp trên i có thể là phần tử lẻ ở vị trí đầu tiên or ptu chẵn cuối
cùng
//xác định vị trí phần tử làm ranh giới(vị trí cuối cùng)
int mid = arrChanLe[i] % 2 != 0 ? i - 1 : i;
//sắp xếp mảng chẵn tăng dần
for( i = 0; i < mid; i++)
for(j = i; j < mid + 1; j++)
{
if (arrChanLe[i] > arrChanLe[j])
swap(ref arrChanLe[i], ref arrChanLe[j]);
}
// sắp xếp lẻ giảm dần
for (i = mid + 1; i < arrChanLe.Length - 1; i++ )
for(j = i + 1; j < arrChanLe.Length; j++)
{
if (arrChanLe[i] < arrChanLe[j])
swap(ref arrChanLe[i], ref arrChanLe[j]);

}
ShowArray(arrChanLe, txtSoMangChanLe);
}
private void swap (ref int a, ref int b)
{
int tam = a;
a = b;
b = tam;
}
}
}
/////////////////////////////////////////////////////////////////
BTHC7
Cây thư mục
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;
using System.IO;//phải tự thêm

namespace BTHC7._1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
init();
}
private void init()//load toàn bộ ổ đĩa
{
string[] drives = Directory.GetLogicalDrives();//lấy ổ đĩa
foreach(string drive in drives)
{
string name = drive.Substring(0, 1) + ":\\";//lấy chữ đầu tiên (D:\\)
DriveInfo d = new DriveInfo(drive);//gán thành kiểu ổ đĩa(có dấu + ở
trước)
TreeNode node = new TreeNode(name,0,0);
if (d.IsReady == true)//kiểm tra ổ đĩa có con
node.Nodes.Add("...");//hiển thị 3 chấm sau khi bấm dấu cộng
node.Tag = drive;//cho ổ đĩa hiển thị
treeFolder.Nodes.Add(node);
}
}

private void treeFolder_BeforeExpand(object sender, TreeViewCancelEventArgs e)


{
if(e.Node.Nodes.Count > 0)
{
if(e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)//kiểm
tra có con
{
e.Node.Nodes.Clear();
string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());//lưu
tên thư muc
foreach(string dir in dirs)
{
DirectoryInfo dr = new DirectoryInfo(dir);
TreeNode node = new TreeNode(dr.Name);
try {
node.Tag = dir;
if(dr.GetDirectories().Count() > 0)
{
node.Nodes.Add(null, "...");
}
}
catch(UnauthorizedAccessException)
{
node.SelectedImageIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "error");
}
finally
{
e.Node.Nodes.Add(node);
}
}
}
}
}

private void treeFolder_AfterExpand(object sender, TreeViewEventArgs e)


{
pnThum.Controls.Clear();//trong đó có gì xóa hết
picView.Image = null;
string path = e.Node.FullPath;
string[] files = Directory.GetFiles(path);
foreach(string file in files)
{
string f = file.ToLower();
if(f.EndsWith(".jpg") || f.EndsWith(".jpeg") ||
f.EndsWith(".png") || f.EndsWith(".gif") || f.EndsWith(".bmp"))
{
PictureBox pic = new PictureBox();
pic.Image = Image.FromFile(file);
pic.SizeMode = PictureBoxSizeMode.StretchImage;
pic.Size = new Size(50, 50);
pic.Click += Pic_Click;
pnThum.Controls.Add(pic);
}
}
}

private void Pic_Click(object sender, EventArgs e)


{
PictureBox p = (PictureBox)sender;
picView.Image = p.Image;
picView.SizeMode = PictureBoxSizeMode.StretchImage;
}

}
}
QLNV
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BTH7._2
{
class Employee
{
string name;
int age;
string address;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = (value < 18 ? 18 : value); }
}
public string Address
{
get { return address; }
set { address = value; }
}
public Employee() {

}
public Employee(string name, int age, string address)
{
this.name = name;
this.age = age;
this.address = address;
}
}
}

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;
using System.IO;

namespace BTH7._2
{
public partial class Form1 : Form
{
Employee[] ds = new Employee[100];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void InsertNV(Employee nv)
{
//thêm dòng vào listV
ListViewItem item = new ListViewItem(nv.Name);
item.SubItems.Add(nv.Age.ToString());
item.SubItems.Add(nv.Address);
listV.Items.Add(item);
}
private void Form1_Load(object sender, EventArgs e)
{
//thêm cột vào listV
listV.Columns.Add("Name", 200);
listV.Columns.Add("Age", 100);
listV.Columns.Add("Address", 200);
listV.View = View.Details;

private void btInsert_Click(object sender, EventArgs e)


{
string ten = textBox1.Text;
int tuoi = int.Parse(textBox2.Text);
string diachi = textBox4.Text;
ds[i] = new Employee(ten, tuoi, diachi);
InsertNV(ds[i]);
textBox1.Clear();
textBox2.Clear();
textBox4.Clear();
textBox1.Focus();
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)


{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Empl file (*.empl) | *.empl";
DialogResult rs = new DialogResult();
rs = save.ShowDialog();
if(rs == DialogResult.OK)
{
string path = save.FileName;
StreamWriter w = new StreamWriter(@path);
//thêm @ là đường dẫn tuyệt đối
for(int j = 0; j < i; j++)
{
string s = ds[j].Name + '#' + ds[j].Age.ToString() + '#' +
ds[j].Address;
w.WriteLine(s);

}
w.Close();
}
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)


{
listV.Items.Clear();
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Empl file (*.empl) | *.empl";
DialogResult re = new DialogResult();
if(re == DialogResult.OK)
{
string path = o.Filter;
StreamReader r = new StreamReader(@path);
string text;
if(File.Exists(@path))
{
string[] kq = File.ReadAllLines(@path);
for(int i = 0; i < kq.Length; i++)
{
text = r.ReadLine();
string[] value = text.Split(new char[] { '#' });
ListViewItem item = new ListViewItem(value[0]);
item.SubItems.Add(value[1]);
item.SubItems.Add(value[2]);
listV.Items.Add(item);
}
}
}
}
}
}
BTHC8
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;
using System.Drawing.Drawing2D;

namespace BTHC8
{
public partial class FrmDrawByMouse : Form
{
public FrmDrawByMouse()
{
InitializeComponent();
}
int penWidth;//độ dài nét vẽ
Color color;//màu vẽ
//thông tin điểm vẽ cũ
Point pOld;
//Khung tạo khu vực vẽ
Bitmap bmpTemp;

private void FrmDrawByMouse_Load(object sender, EventArgs e)


{
color = Color.Red;
penWidth = 1;
bmpTemp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
}

private void FrmDrawByMouse_Paint(object sender, PaintEventArgs e)


{
e.Graphics.DrawImage(bmpTemp, 0, 0);
}

private void FrmDrawByMouse_MouseDown(object sender, MouseEventArgs e)


{
pOld = e.Location;
}
private void FrmDrawByMouse_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//đè chuotj trái kéo đi
{
Pen pen = new Pen(color, penWidth);
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
Graphics g = Graphics.FromImage(bmpTemp);
g.DrawLine(pen, pOld, e.Location);
pOld = e.Location;//cập nhật điểm mới
Invalidate();//gọi vẽ lại, gọi Paint lại
}
}
//protected override bool ProcessDialogKey(Keys keyData)
//{
// switch (keyData)
// {
// case Keys.R: color = Color.Red; break;
// case Keys.B: color = Color.Blue; break;
// case Keys.G: color = Color.Green; break;
// case Keys.Up:
// if (penWidth < 30)
// penWidth++;
// break;
// case Keys.Down:
// if (penWidth > 1)
// penWidth--;
// break;
// }
// return true;
//}

private void FrmDrawByMouse_KeyDown(object sender, KeyEventArgs e)


{
switch (e.KeyCode)
{
case Keys.R: color = Color.Red; break;
case Keys.B: color = Color.Blue; break;
case Keys.G: color = Color.Green; break;
case Keys.Up:
if (penWidth < 30)
penWidth++;
break;
case Keys.Down:
if (penWidth > 1)
penWidth--;
break;
}
}
}
}
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;
using System.Drawing.Drawing2D;

namespace BTHC8
{
public partial class FrmText : Form
{
public FrmText()
{
InitializeComponent();
}

private void FrmText_Paint(object sender, PaintEventArgs e)


{
string chuoi = "HELLO";
SolidBrush br1 = new SolidBrush(Color.Green);
Font font = new Font("Arial", 48, FontStyle.Bold);
//Định dạng nằm ở lề phải
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Far;
e.Graphics.DrawString(chuoi, font, br1, ClientRectangle, format);

Image img = Image.FromFile(Application.StartupPath + @"\hinh.png");


TextureBrush br2 = new TextureBrush(img);
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Far;
e.Graphics.DrawString(chuoi, font, br2, ClientRectangle, format);

//Nhớ thêm using namespace


HatchBrush br3 = new HatchBrush(HatchStyle.DarkHorizontal, Color.Red,
Color.Yellow);
format.FormatFlags = StringFormatFlags.DirectionVertical;
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
e.Graphics.DrawString("HELLO", font, br3, ClientRectangle, format);

LinearGradientBrush br4 = new LinearGradientBrush(new Rectangle(0, 0, 10,


10), Color.DarkBlue, Color.White, 45);
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Far;
e.Graphics.DrawString("HELLO", font, br4, ClientRectangle, format);
}
}
}
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 BTHC8
{
public partial class FrmDrawImage : Form
{
public FrmDrawImage()
{
InitializeComponent();
}

private void FrmDrawImage_Paint(object sender, PaintEventArgs e)


{
Image img = Image.FromFile(Application.StartupPath + @"\Hinh.png");
Rectangle rc1 = new Rectangle(20,20,200,100);
e.Graphics.DrawImage(img, rc1);
}
}
}
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 BTHC8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void drawByMouseToolStripMenuItem_Click(object sender, EventArgs e)


{
FrmDrawByMouse f = new FrmDrawByMouse();
f.MdiParent = this;
f.Show();
}

private void drawTestToolStripMenuItem_Click(object sender, EventArgs e)


{
FrmText f = new FrmText();
f.MdiParent = this;
f.Show();
}

private void drawImageToolStripMenuItem_Click(object sender, EventArgs e)


{
FrmDrawImage f = new FrmDrawImage();
f.MdiParent = this;
f.Show();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)


{
DialogResult result = new DialogResult();
result = MessageBox.Show("Bạn muốn thoát?", "Exit", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
Application.Exit();
}
}
}
///////////////////////////////////////////////////////////////////////
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;
using System.Threading;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path1 = Application.StartupPath + "\\1.png";
string path2 = Application.StartupPath + "\\2.png";
string path3 = Application.StartupPath + "\\3.png";
string path4 = Application.StartupPath + "\\4.png";
string path5 = Application.StartupPath + "\\5.png";
string path6 = Application.StartupPath + "\\6.png";
string path = Application.StartupPath;
int[] result = new int[3];
int Bet = 0;
Random rd = new Random();
int Money = 0;
int Reward = 0;
// Thread thr = new Thread();

private void Form1_Load(object sender, EventArgs e)


{
ptbDice1.Image = Image.FromFile(Application.StartupPath + "\\1.png");
ptbDice2.Image = new Bitmap(path2);
ptbDice3.Image = new Bitmap(path3);
txtMoney.Text = "10000";
}

private void txtMoney_TextChanged(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{
txtMoney.Text = (Int32.Parse(txtMoney.Text) + 10000).ToString();
}

private void button1_Click(object sender, EventArgs e)


{
Money = Int32.Parse(txtMoney.Text);
Reward = Int32.Parse(txtReward.Text);
if(Reward>Money)
{
MessageBox.Show("Nạp thêm rồi hãy chơi tiếp bạn :v");
}
else
{
Processing();
}
}

private void Processing()


{
int Sample = 0;
Bet = cbbItem.SelectedIndex + 1;
for (int i = 0; i < result.Length; i++)
{
result[i] = rd.Next(1, 6);
if (Bet == result[i])
{
Sample++;
}
}

ptbDice1.Image = new Bitmap(path + "\\" + result[0].ToString() + ".png");


ptbDice2.Image = new Bitmap(path + "\\" + result[1].ToString() + ".png");
ptbDice3.Image = new Bitmap(path + "\\" + result[2].ToString() + ".png");
if (Sample == 0)
{

txtMoney.Text = (Money - Reward).ToString();


MessageBox.Show("Chưa may mắn rồi, đầu tư tiếp bạn nhé !");
}
else
{
txtMoney.Text = (Money + Reward * Sample).ToString();
string reward = "Chúc mừng bạn, có " + Sample.ToString() + " " +
cbbItem.Text;
MessageBox.Show(reward);
}
}

private void ptbNai_Click(object sender, EventArgs e)


{

}
}
}
////////////////////////////////////////////////////////////////////
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
FileStream stream = null;
BinaryFormatter binary = new BinaryFormatter();
arrEmp.Clear();
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Jpeg file(*.jpg)|*.jpg" +
"|Gip file(*.gif)|*.gif" +
"|Png file(*.png)|*.png" +
"|Bitmap file(*.bmp)|*bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
stream = new FileStream(dlg.FileName, FileMode.Create,
FileAccess.Write);
}
}
catch
{

}
}

////////////////////////////////////////////////////////////////////
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 De3
{
public partial class FrmList : Form
{
public FrmList()
{
InitializeComponent();
}

private void FrmList_Load(object sender, EventArgs e)


{
listView1.View = View.Details;
listView1.Columns.Add("Tên sách", 200);
listView1.Columns.Add("Tác giả",200, HorizontalAlignment.Center);
}

private void button1_Click(object sender, EventArgs e)


{
ListViewItem item = new ListViewItem(textBox1.Text);
item.SubItems.Add(textBox2.Text);
listView1.Items.Add(item);
textBox1.Text = textBox2.Text = " ";
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
listView1.Items.Remove(item);
}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)


{

}
}
}
//////////////////////////////////////////////////////////////////
private void FrmVe_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(200,200,300,300);
LinearGradientBrush r = new LinearGradientBrush(rect, Color.Orange,
Color.White, 45);
e.Graphics.FillRectangle(r, rect);
Font font = new Font("Arial", 48, FontStyle.Bold);
//Định dạng nằm ở lề phải
StringFormat format = new StringFormat();
//LinearGradientBrush br4 = new LinearGradientBrush(rect, Color.Pink,
Color.White, 45);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("Nguyễn Văn A", font, new SolidBrush(Color.Red), rect,
format);
}

You might also like