You are on page 1of 3

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

private void btnAdd_Click(object sender, EventArgs e)


{
//Khai báo biến
string account = txtAccount.Text;
string name = txtName.Text;
string birthday = dtpBirthday.Value.ToShortDateString();
string sex = radMale.Checked ? "Nam" : "Nữ";
string statetus = chkState.Checked ? "Hoạt động" : "Không hoạt động";

if (lstAccount.Items.Count > 0)
{
bool exitsAcc = false;
foreach (ListViewItem item in lstAccount.Items)
{
if(item.Text == account)
{
exitsAcc = true;
break;
}
}

if (!exitsAcc)
{
//Item
ListViewItem item = new ListViewItem(account);
item.SubItems.Add(name);
item.SubItems.Add(birthday);
item.SubItems.Add(sex);
item.SubItems.Add(statetus);
//Xử lý event
lstAccount.Items.Add(item);
}
else
{
MessageBox.Show("Tài khoản đã tồn tại!");
}
}
else
{
//Item
ListViewItem item = new ListViewItem(account);
item.SubItems.Add(name);
item.SubItems.Add(birthday);
item.SubItems.Add(sex);
item.SubItems.Add(statetus);
//Xử lý event
lstAccount.Items.Add(item);
}
}

private void lstAccount_SelectedIndexChanged(object sender, EventArgs e)


{
if (lstAccount.SelectedItems.Count > 0)
{
ListViewItem item = lstAccount.SelectedItems[0];
// Hiện lên thông tin
if (item != null)
{
txtAccount.Text = item.Text;
txtName.Text = item.SubItems[1].Text;
dtpBirthday.Value = DateTime.Parse(item.SubItems[2].Text);
if (item.SubItems[3].Text == "Nam")
{
radMale.Checked = true;
}
else
{
radFemale.Checked = true;
}
chkState.Checked = item.SubItems[4].Text == "Hoạt động" ?
true : false;
}
}

private void btnEdit_Click(object sender, EventArgs e)


{
try
{
//Thay đổi thông tin
string account = txtAccount.Text;
string name = txtName.Text;
string birthday = dtpBirthday.Value.ToShortDateString();
string sex = radMale.Checked ? "Nam" : "Nữ";
string statetus = chkState.Checked ? "Hoạt động" : "Không hoạt
động";

lstAccount.SelectedItems[0].SubItems[0].Text = account;
lstAccount.SelectedItems[0].SubItems[1].Text = name;
lstAccount.SelectedItems[0].SubItems[2].Text = birthday;
lstAccount.SelectedItems[0].SubItems[3].Text = sex;
lstAccount.SelectedItems[0].SubItems[4].Text = statetus;

}
catch (Exception)
{
MessageBox.Show(lstAccount.SelectedIndices[0].ToString());
}
}
}
}

You might also like