/
DataTimeModule
/
DateTime
Обзор
Документация
Войти
/
DataTimeModule
/
DateTime
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
SQLDataDriver/ProductEditForm.cs
182 строки
7 KB
Reeltell
upload
27 май 2026, 01:50
27 май 2026, 01:50
f282598
Код
Авторство
О чём код?
namespace SQLDataDriver { public partial class ProductEditForm : Form { private readonly string article; private string photoPath = ""; private string newPhotoPath = ""; public ProductEditForm(string article) { InitializeComponent(); UiStyle.Apply(this); this.article = article; } private bool IsEdit => article != ""; private void ProductEditForm_Load(object sender, EventArgs e) { LoadComboBoxes(); if (IsEdit) { LoadProduct(); } else { articleLabel.Visible = false; articleTextBox.Visible = false; photoPath = "picture.png"; photoBox.Image = Db.GetImage(photoPath); } } private void LoadComboBoxes() { Db.Combo(categoryComboBox, "SELECT ProductCategoryId, Title FROM ProductCategories ORDER BY Title", "Title", "ProductCategoryId"); Db.Combo(manufacturerComboBox, "SELECT ManufacturerId, Title FROM Manufacturers ORDER BY Title", "Title", "ManufacturerId"); Db.Combo(providerComboBox, "SELECT ProviderId, Title FROM Providers ORDER BY Title", "Title", "ProviderId"); Db.Combo(unitComboBox, "SELECT UnitId, Title FROM Units ORDER BY Title", "Title", "UnitId"); } private void LoadProduct() { bool ok = Db.One("SELECT * FROM Products WHERE Article = @article", r => { articleTextBox.Text = article; titleTextBox.Text = Db.S(r, "Title"); descriptionTextBox.Text = Db.S(r, "Description"); priceTextBox.Text = Db.S(r, "Price"); stockTextBox.Text = Db.S(r, "StockQuantity"); discountTextBox.Text = Db.S(r, "Discount"); categoryComboBox.SelectedValue = Db.I(r, "ProductCategoryId"); manufacturerComboBox.SelectedValue = Db.I(r, "ManufacturerId"); providerComboBox.SelectedValue = Db.I(r, "ProviderId"); unitComboBox.SelectedValue = Db.I(r, "UnitId"); photoPath = Db.S(r, "PhotoPath"); photoBox.Image = Db.GetImage(photoPath); }, ("@article", article)); if (!ok) Close(); } private void saveButton_Click(object sender, EventArgs e) { if (!decimal.TryParse(priceTextBox.Text, out decimal price) || price < 0 || !int.TryParse(stockTextBox.Text, out int stock) || stock < 0 || !int.TryParse(discountTextBox.Text, out int discount) || discount < 0 || discount > 100) { Db.Error("Проверьте цену, количество и скидку."); return; } if (titleTextBox.Text.Trim() == "") { Db.Error("Введите наименование товара."); return; } string newArticle = IsEdit ? article : GetNewArticle(); string savedPhoto = SavePhoto(newArticle); string sql = IsEdit ? """ UPDATE Products SET Title=@title, UnitId=@unit, Price=@price, ProviderId=@provider, ManufacturerId=@manufacturer, ProductCategoryId=@category, Discount=@discount, StockQuantity=@stock, Description=@description, PhotoPath=@photo WHERE Article=@article """ : """ INSERT INTO Products (Article, Title, UnitId, Price, ProviderId, ManufacturerId, ProductCategoryId, Discount, StockQuantity, Description, PhotoPath) VALUES (@article, @title, @unit, @price, @provider, @manufacturer, @category, @discount, @stock, @description, @photo) """; Db.Exec(sql, ("@article", newArticle), ("@title", titleTextBox.Text.Trim()), ("@unit", unitComboBox.SelectedValue), ("@price", price), ("@provider", providerComboBox.SelectedValue), ("@manufacturer", manufacturerComboBox.SelectedValue), ("@category", categoryComboBox.SelectedValue), ("@discount", discount), ("@stock", stock), ("@description", descriptionTextBox.Text.Trim()), ("@photo", savedPhoto)); Close(); } private string GetNewArticle() { return "NEW" + Db.Scalar("SELECT COUNT(*) + 1 FROM Products"); } private void photoButton_Click(object sender, EventArgs e) { using var dialog = new OpenFileDialog(); dialog.Filter = "Изображения|*.jpg;*.jpeg;*.png"; if (dialog.ShowDialog() == DialogResult.OK) { newPhotoPath = dialog.FileName; using var image = Image.FromFile(newPhotoPath); photoBox.Image = new Bitmap(image); } } private string SavePhoto(string currentArticle) { if (newPhotoPath == "") { return photoPath; } string fileName = currentArticle + ".png"; string imagesDir = Path.Combine(AppContext.BaseDirectory, "Images"); string path = Path.Combine(imagesDir, fileName); Directory.CreateDirectory(imagesDir); using var src = Image.FromFile(newPhotoPath); using var resized = new Bitmap(300, 200); using (var g = Graphics.FromImage(resized)) { g.Clear(Color.White); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.DrawImage(src, new Rectangle(0, 0, 300, 200)); } resized.Save(path, System.Drawing.Imaging.ImageFormat.Png); if (IsEdit && !string.IsNullOrWhiteSpace(photoPath) && !string.Equals(photoPath, "picture.png", StringComparison.OrdinalIgnoreCase) && !string.Equals(photoPath, fileName, StringComparison.OrdinalIgnoreCase)) { string oldPath = Path.Combine(imagesDir, photoPath); try { if (File.Exists(oldPath)) File.Delete(oldPath); } catch { // ignore file delete issues } } return fileName; } private void cancelButton_Click(object sender, EventArgs e) { Close(); } } }