private void btnDelete_Click(object sender, EventArgs e) { try { for (int i = 0; i < listInformation.Items.Count; i++) { if (listInformation.Items[i].Selected) { listInformation.Items[i].Remove(); } } SanPham sanpham = SPController.getSanPham(this.ID.ToString()); SPController.DeleteSanPham(Convert.ToInt32(sanpham.ID)); } catch { txtmasp.Clear(); txttensp.Clear(); txtgiasp.Clear(); this.errorProvider1.Clear(); listInformation.Items.Clear(); return; } }
private void btnAdd_Click(object sender, EventArgs e) { if (this.txtmasp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txtmasp, "Nhập mã sản phẩm"); return; } else if (SPController.getSanPham(this.txtmasp.Text) != null) { this.errorProvider1.SetError(this.txtmasp, "Mã Sản Phẩm Đã Tồn Tại"); return; } else if (this.txttensp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txttensp, "Nhập tên Sản Phẩm"); return; } else if (this.txtgiasp.Text.Trim().Length <= 0) { this.errorProvider1.SetError(this.txtgiasp, "Nhập giá sản phẩm"); return; } else if (Convert.ToDateTime(dtpngayin.Value.ToString()) <= Convert.ToDateTime(DateTime.Today.ToString())) { this.errorProvider1.SetError(this.dtpngayin, "Ngày in phải nhỏ hơn hoặc bằng ngày hiện tại"); return; } else { SanPham sanpham = new SanPham(); sanpham.MaSanPham = this.txtmasp.Text.Trim(); sanpham.TenSanPham = this.txttensp.Text.Trim(); sanpham.GiaSanPham = this.txtgiasp.Text.Trim(); sanpham.NgayIn = this.dtpngayin.Value; ListViewItem information = new ListViewItem(this.ID.ToString()); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txtmasp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txttensp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, txtgiasp.Text)); information.SubItems.Add(new ListViewItem.ListViewSubItem(information, dtpngayin.Value.ToString())); this.listInformation.Items.Add(information); Bitmap bm = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); this.pictureBox1.DrawToBitmap(bm, this.pictureBox1.ClientRectangle); this.pictureBox1.CreateGraphics().Clear(Color.White); bm = new Bitmap(this.pictureBox1.Width + 2, this.pictureBox1.Height + 2); if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } //In ma 3D QRCoder.QRCodeGenerator qr = new QRCoder.QRCodeGenerator(); var data = qr.CreateQrCode(txttensp.Text, QRCoder.QRCodeGenerator.ECCLevel.H); var code = new QRCoder.QRCode(data); pictureBox1.Image = code.GetGraphic(5); MemoryStream stream = new MemoryStream(); code.GetGraphic(5).Save(stream, ImageFormat.Jpeg); sanpham.ImgQCode = stream.ToArray(); //In ma 2D string barcode = txttensp.Text; try { Zen.Barcode.Code128BarcodeDraw br = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; pictureBox2.Image = br.Draw(barcode, 40); MemoryStream stream1 = new MemoryStream(); br.Draw(barcode, 40).Save(stream1, ImageFormat.Jpeg); sanpham.Img1D = stream1.ToArray(); } catch (Exception ex) { } if (SPController.AddSanPham(sanpham) == false) { MessageBox.Show("Lỗi thêm user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } txttensp.Text = string.Empty; txtmasp.Text = string.Empty; txtgiasp.Text = string.Empty; dtpngayin.Value = DateTime.Now; this.errorProvider1.Clear(); DisPlayInformation(); } }