Ejemplo n.º 1
0
        private void btnLuu_Click(object sender, RoutedEventArgs e)
        {
            bool conLoi = false;

            #region Kiểm tra nhập vào
            if (string.IsNullOrEmpty(txtMaTaiLieu.Text))
            {
                ChuaNhapMaTaiLieu.Visibility = Visibility.Visible;
                conLoi = true;
            }
            else
            {
                foreach (TaiLieu tai in taiLieuList.ToList())
                {
                    if (txtMaTaiLieu.Text == tai.maTaiLieu)
                    {
                        if (cmbTheLoai.SelectedIndex == 0)
                        {
                            Sach sach = new Sach();
                            sach.maTaiLieu    = txtMaTaiLieu.Text;
                            sach.tenTaiLieu   = txtTenTaiLieu.Text;
                            sach.ngayPhatHanh = dtpNgayPhatHanh.SelectedDate.Value;
                            sach.tenTacGia    = txtTenTacGia.Text;
                            sach.soTrang      = txtSoTrang.Text;
                            sach.theLoai      = cmbTheLoai.Text;
                            TaiLieu tai1 = new Sach();
                            tai1 = tai;
                            var sachthe = (Sach)tai1;
                            sachList[sachList.IndexOf(sachthe)]   = sach;
                            taiLieuList[taiLieuList.IndexOf(tai)] = sach;
                        }
                        else if (cmbTheLoai.SelectedIndex == 1)
                        {
                            TapChi tapChi = new TapChi();
                            tapChi.maTaiLieu    = txtMaTaiLieu.Text;
                            tapChi.tenTaiLieu   = txtTenTaiLieu.Text;
                            tapChi.ngayPhatHanh = dtpNgayPhatHanh.SelectedDate.Value;
                            tapChi.theLoai      = cmbTheLoai.Text;
                            tapChi.chuDe        = txtChuDe.Text;
                            tapChi.Gia          = Convert.ToDouble(txtGia.Text);
                            TaiLieu tai1 = new TapChi();
                            tai1 = tai;
                            var tapchithe = (TapChi)tai1;
                            tapChiList[tapChiList.IndexOf(tapchithe)] = tapChi;
                            taiLieuList[taiLieuList.IndexOf(tai)]     = tapChi;
                        }
                        #region Xoa
                        txtMaTaiLieu.Clear();
                        txtTenTacGia.Clear();
                        txtTenTacGia.Clear();
                        txtSoTrang.Clear();
                        txtTenTaiLieu.Clear();
                        txtChuDe.Clear();
                        txtGia.Clear();
                        dtpNgayPhatHanh.SelectedDate = null;
                        cmbTheLoai.SelectedIndex     = -1;
                        txtMaTaiLieu.Focus();
                        #endregion
                    }
                }
            }
            if (string.IsNullOrEmpty(txtTenTaiLieu.Text))
            {
                ChuaNhapMaTaiLieu.Visibility = Visibility.Visible;
                conLoi = true;
            }
            if (dtpNgayPhatHanh.SelectedDate == null)
            {
                ChuaChonNgayPhatHanh.Visibility = Visibility.Visible;
                conLoi = true;
            }
            if (cmbTheLoai.SelectedIndex == -1)
            {
                ChuaChonTheLoai.Visibility = Visibility.Visible;
                conLoi = true;
            }
            if (cmbTheLoai.SelectedIndex == 0)
            {
                if (string.IsNullOrEmpty(txtTenTacGia.Text))
                {
                    ChuaNhapTenTacGia.Visibility = Visibility.Visible;
                    conLoi = true;
                }
                if (string.IsNullOrEmpty(txtSoTrang.Text))
                {
                    ChuaNhapSoTrang.Visibility = Visibility.Visible;
                    conLoi = true;
                }
            }
            else if (cmbTheLoai.SelectedIndex == 1)
            {
                if (string.IsNullOrEmpty(txtChuDe.Text))
                {
                    ChuaNhapChuDe.Visibility = Visibility.Visible;
                    conLoi = true;
                }
                if (string.IsNullOrEmpty(txtGia.Text))
                {
                    ChuaNhapGia.Visibility = Visibility.Visible;
                    conLoi = true;
                }
            }
            #endregion
            if (!conLoi)
            {
                lvTaiLieu.Items.Clear();
                if (cmbTheLoai.SelectedIndex == 0)
                {
                    Sach sach = new Sach();
                    sach.maTaiLieu    = txtMaTaiLieu.Text;
                    sach.tenTaiLieu   = txtTenTaiLieu.Text;
                    sach.ngayPhatHanh = dtpNgayPhatHanh.SelectedDate.Value;
                    sach.tenTacGia    = txtTenTacGia.Text;
                    sach.soTrang      = txtSoTrang.Text;
                    sach.theLoai      = cmbTheLoai.Text;
                    sachList.Add(sach);
                    taiLieuList.Add(sach);
                }
                else if (cmbTheLoai.SelectedIndex == 1)
                {
                    TapChi tapChi = new TapChi();
                    tapChi.maTaiLieu    = txtMaTaiLieu.Text;
                    tapChi.tenTaiLieu   = txtTenTaiLieu.Text;
                    tapChi.ngayPhatHanh = dtpNgayPhatHanh.SelectedDate.Value;
                    tapChi.theLoai      = cmbTheLoai.Text;
                    tapChi.chuDe        = txtChuDe.Text;
                    tapChi.Gia          = Convert.ToDouble(txtGia.Text);
                    tapChiList.Add(tapChi);
                    taiLieuList.Add(tapChi);
                }
                txtMaTaiLieu.Clear();
                txtTenTacGia.Clear();
                txtTenTacGia.Clear();
                txtSoTrang.Clear();
                txtTenTaiLieu.Clear();
                txtChuDe.Clear();
                txtGia.Clear();
                dtpNgayPhatHanh.SelectedDate = null;
                cmbTheLoai.SelectedIndex     = -1;
                txtMaTaiLieu.Focus();
            }
            foreach (TaiLieu taiLieu in taiLieuList)
            {
                lvTaiLieu.Items.Add(taiLieu);
            }
        }