Bài 3.1: Code: Giao diện chính:: using using using using using using using using using using namespace

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

LAB 5

Bài 3.1:
CODE:
Giao diện chính:
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.Globalization;

namespace LiemNguyenTrong
{

public partial class Form1 : Form


{
string[] HO = { "Lê", "Nguyễn", "Lý", "Trần", "Lâm", "Hồ", "Lai", "Huỳnh",
"La" };
string[] TENLOT = { "Quang", "Thành", "Ngọc", "Anh", "Xuân", "Bảo", "Cẩm", "Thị",
"Kim", "Thái", "Hồng" };
string[] TEN = { "Hà", "Danh", "Sơn", "Mai", "Thắng", "Kỳ", "Thành", "Lâm",
"Tâm", "Phụng", "Thắm" };

public Form1()
{
InitializeComponent();
}

private void groupBox1_Enter(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{
TaoHoTen();
}
void TaoHoTen()
{
Random r = new Random();
for (int i = 0; i < 15; i++)
{
string hoten = HO[r.Next(0, HO.Length)] + " " + TENLOT[r.Next(0,
TENLOT.Length)] + " " + TEN[r.Next(0, TEN.Length)]
;
if (listBox1.Items.Contains(hoten))
MessageBox.Show("trùng" + hoten);
listBox1.Items.Add(hoten);
}
}

private void btnThucHien_Click(object sender, EventArgs e)


{
if (rdoXoaHo.Checked)
{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (listBox1.Items[i].ToString().ToLower().StartsWith(txtNhap.Text))
listBox1.Items.RemoveAt(i);
}
}
else if (rdoXoaTen.Checked)
{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if
(listBox1.Items[i].ToString().ToLower().EndsWith(txtNhap.Text.Trim()))
listBox1.Items.RemoveAt(i);
}
}
else if(rdoXoaLot.Checked)
{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (listBox1.Items[i].ToString().ToLower().Contains("
"+txtNhap.Text.Trim()+" "))
listBox1.Items.RemoveAt(i);
}
}
else if (rdoThem.Checked)
{
listBox1.Items.Add(txtNhap.Text);
}
}

private void btnLower_Click(object sender, EventArgs e)


{
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.Items.Insert(i, listBox1.Items[i].ToString().ToLower());
listBox1.Items.RemoveAt(i + 1);
}
}

private void btnCapitalize_Click(object sender, EventArgs e)


{
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.Items.Insert(i,
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(listBox1.Items[i].ToString().ToLower()));
listBox1.Items.RemoveAt(i + 1);
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
Form2 input = new Form2();
input.inputText = listBox1.SelectedItem.ToString();
if (input.ShowDialog() == DialogResult.OK)
listBox1.Items[listBox1.SelectedIndex] = input.inputText;
}
}

private void listBox1_MouseDown(object sender, MouseEventArgs e)


{
if(e.Button == MouseButtons.Right)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.Location);
}
}

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)


{
if (listBox1.SelectedIndex >= 0)
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}

private void modifyToolStripMenuItem_Click(object sender, EventArgs e)


{
if (listBox1.SelectedIndex != -1)
{
Form2 input = new Form2();
input.inputText = listBox1.SelectedItem.ToString();
if (input.ShowDialog() == DialogResult.OK)
listBox1.Items[listBox1.SelectedIndex] = input.inputText;
}
}

private void btnUpper_Click(object sender, EventArgs e)


{
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.Items.Insert(i, listBox1.Items[i].ToString().ToUpper());
listBox1.Items.RemoveAt(i + 1);
}
}

}
}

Xử lý chuột phải trong listbox:


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 LiemNguyenTrong
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
DialogResult = DialogResult.OK;
}
public string inputText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

private void button2_Click(object sender, EventArgs e)


{
DialogResult = DialogResult.Cancel;
}
}
}
Bài 4.1:
CODE:
Giao diện chính:
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 LiemNguyenTrong
{
public partial class Form4 : Form
{
List<Word> list;
public Form4()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
this.Dispose();
}
public class AnhViet
{
public string anh, viet;
public AnhViet(string anh, string viet)
{
this.anh = anh;
this.viet = viet;
}
}

private void Form4_Load(object sender, EventArgs e)


{
csdl();
}
private void csdl()
{
list = new List<Word>(){
new Word() {tienganh="private",tiengviet="bảo mật,riêng tư " },
new Word() {tienganh="learn",tiengviet="học" },
new Word() {tienganh="jump",tiengviet="nhảy cao" },
new Word() {tienganh="profression",tiengviet="đỉnh cao,chuyên nghiệp" },
};
foreach (Word w in list)
{
listBox1.Items.Add(w.tienganh);
cboViet.Items.Add(w.tiengviet);
}
}

private void cboViet_SelectedIndexChanged(object sender, EventArgs e)


{
object selected = cboViet.SelectedItem;
string s = String.Empty;
foreach (Word w in list)
if (w.tiengviet.Equals(selected.ToString()))
s = w.tienganh + " ,";
s = s.Substring(0, s.Length - 1);
txtAnh.Text = s;
}

private void listBox1_MouseDown(object sender, MouseEventArgs e)


{
int index = listBox1.SelectedIndex;
if (index != -1)
{
foreach (Word w in list)
if (w.tienganh.Equals(listBox1.Items[index].ToString()))
{
txtViet.Text = w.tiengviet;
break;
}
}
}

private void cboAnh_TextChanged(object sender, EventArgs e)


{
listBox1.Items.Clear();
foreach (Word w in list)
if (w.tienganh.Contains(cboAnh.Text.Trim()))
listBox1.Items.Add(w.tienganh);
}

}
}
Class Word:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiemNguyenTrong
{
public class Word
{
string anh, viet;
public string tienganh
{
get
{
return anh;
}
set
{
anh = value;
}
}
public string tiengviet
{
get
{
return viet;
}
set
{
viet = value;
}
}
}
}
Bài 4.2:
CODE:

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 LiemNguyenTrong
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void btnNhap_Click(object sender, EventArgs e)


{
if (!String.IsNullOrEmpty(txtNhap.Text))
listBox1.Items.Add(txtNhap.Text);
}

private void btnTongPT_Click(object sender, EventArgs e)


{
double tong = 0;
foreach (string s in listBox1.Items)
{
tong += double.Parse(s);
}
MessageBox.Show("Tổng các phần tử trong listbox bằng " + tong.ToString());
}

private void btnXoaDauCuoi_Click(object sender, EventArgs e)


{
if (listBox1.Items.Count < 2) listBox1.Items.RemoveAt(0);
else { listBox1.Items.RemoveAt(0);
listBox1.Items.RemoveAt(listBox1.Items.Count - 1) ;
}
}

private void btnXoaChon_Click(object sender, EventArgs e)


{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
}
else
MessageBox.Show("Ở trong list đang trống ko xóa được"); }

private void btnTang_Click(object sender, EventArgs e)


{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (!String.IsNullOrEmpty(listBox1.Items[i] as String))
{
double tmp = double.Parse((listBox1.Items[i].ToString()));
tmp = tmp + 2;
listBox1.Items[i] = tmp.ToString();
}
}
}

private void btnThay_Click(object sender, EventArgs e)


{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (! String.IsNullOrEmpty(listBox1.Items[i] as String))
{
double tmp = double.Parse((listBox1.Items[i].ToString()));
tmp = Math.Pow(tmp,2);
listBox1.Items[i] = tmp.ToString();
}
}
}

private void btnChan_Click(object sender, EventArgs e)


{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (!String.IsNullOrEmpty(listBox1.Items[i] as String))
{
int tmp = int.Parse((listBox1.Items[i].ToString()));
if (tmp % 2 == 0)
listBox1.SetSelected(i, true);
}
}
}

private void btnLe_Click(object sender, EventArgs e)


{
for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
if (!String.IsNullOrEmpty(listBox1.Items[i] as String))
{
int tmp = int.Parse((listBox1.Items[i].ToString()));
if (tmp % 2 == 1)
listBox1.SetSelected(i, true);
}
}
}

private void btnKetThuc_Click(object sender, EventArgs e)


{
this.Dispose();
}
}
}

Bài 4.3:
CODE:
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 LiemNguyenTrong
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
for (char a = 'A'; a <= 'Z'; a++)
{
treeView1.Nodes.Add(a.ToString());
}
}

private void btnAdd_Click(object sender, EventArgs e)


{
TreeNodeCollection myNodes = treeView1.Nodes;
string firstName = txtFirst.Text;
string lastName = txtLast.Text;
int i = 0;
foreach (TreeNode n in myNodes)
{
if (firstName.StartsWith(n.Text) == true)
{
n.Nodes.Add(firstName + ", " + lastName);
MessageBox.Show("Thêm thành công", "Thông báo",
MessageBoxButtons.OK);
i++;
}
}
if (i == 0)
{
MessageBox.Show("Không tìm thấy node tương ứng kiểm tra lại tên", "Thông
báo", MessageBoxButtons.OK);
}
}

private void btnExit_Click(object sender, EventArgs e)


{
this.Close();
}
}
}
Bài tập quay số timer:
CODE:
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 LiemNguyenTrong
{
public partial class Form3 : Form
{
Random r = new Random();
public Form3()
{
InitializeComponent();

}
private void timer1_Tick(object sender, EventArgs e)
{
int so = 0;

so = r.Next(0, 9);
label2.Text = so.ToString();
so = r.Next(0, 9);
label3.Text = so.ToString();
so = r.Next(0, 9);
label4.Text = so.ToString();
timer1.Stop();
if (label2.Text == label3.Text && label3.Text == label4.Text && label4.Text
== label2.Text)
MessageBox.Show("Bạn đã trúng giải nhất của sổ xố miền bắc !!");
else
MessageBox.Show("Chúc Bạn May Mắn Lần Sau");
}

private void Form3_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
timer1.Start();
}

private void button2_Click(object sender, EventArgs e)


{
Application.Exit();
}
}
}

You might also like