public Model() { _dataManagement = new DataManagement(DataManagement.GetSystemDirection() + @"\CommodityInformation.xlsx", 1); _dataManagement.LoadProduct(_products); _dataManagement.CloseExcel(); FindAllTypeInProducts(); }
//顯示商品的圖片和介紹 private void ShowImageAndIntroduction(object sender, DataGridViewCellEventArgs e) { Image image = System.Drawing.Bitmap.FromFile(DataManagement.GetSystemDirection() + @"\picture\" + _products[e.RowIndex].Name + @".jpg"); _introduction.Text = _products[e.RowIndex].Introduction; _image.Image = image; }
//新加一個Button private void AddButton(TableLayoutPanel layoutName, int id) { Product product = _model.GetProductById(id); ProductButton button = new ProductButton(id); layoutName.Controls.Add(button, 0, 0); button.Dock = System.Windows.Forms.DockStyle.Fill; button.Name = product.Name; button.Tag = product.Type; button.Image = Image.FromFile(DataManagement.GetSystemDirection() + @"\picture\" + product.Name + @".jpg"); button.Click += new System.EventHandler(this.ClickProduct); button.BackgroundImageLayout = ImageLayout.Stretch; }
//畫DeleteButton的Icon private void PaintGridCell(object sender, DataGridViewCellPaintingEventArgs e) { const int TWO = 2; if (e.RowIndex < 0) { return; } //I supposed your button column is at index 0 if (e.ColumnIndex == 0) { Image img = Image.FromFile(DataManagement.GetSystemDirection() + @"\picture\" + @"DeleteImage.jpg"); e.Paint(e.CellBounds, DataGridViewPaintParts.All); var w = img.Width; var h = img.Height; var x = e.CellBounds.Left + (e.CellBounds.Width - w) / TWO; var y = e.CellBounds.Top + (e.CellBounds.Height - h) / TWO; e.Graphics.DrawImage(img, new Rectangle(x, y, w, h)); e.Handled = true; } }