/
DaNN26
/
SQLProvider
Обзор
Документация
Войти
/
DaNN26
/
SQLProvider
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ProductCardControl.cs
106 строк
4 KB
DaNN
Добавьте файлы проекта.
27 май 2026, 12:50
27 май 2026, 12:50
4fe37b9
Код
Авторство
О чём код?
using System; using System.Data; using System.Drawing; using System.Globalization; using System.Windows.Forms; namespace Demo { internal partial class ProductCardControl : UserControl { public event EventHandler ProductSelected; public int ProductId { get; private set; } public ProductCardControl() { InitializeComponent(); } public ProductCardControl(DataRow row, bool canEdit) : this() { Bind(row); if (canEdit) { Cursor = Cursors.Hand; AddClickHandler(this); } } private void Bind(DataRow row) { ProductId = Convert.ToInt32(row["ProductId"]); var stock = Convert.ToInt32(row["Quantity"]); var sale = Convert.ToInt32(row["Discount"]); var price = Convert.ToDecimal(row["Price"]); var finalPrice = price - price * sale / 100m; PaintByRules(stock, sale); photoPictureBox.Image = AppHelper.LoadImage(row["PhotoPath"].ToString()); titleInfoLabel.Text = row["CategoryName"] + " | " + row["ProductName"]; descriptionValueLabel.Text = row["Description"].ToString(); manufacturerValueLabel.Text = row["ManufacturerName"].ToString(); supplierValueLabel.Text = row["SupplierName"].ToString(); unitValueLabel.Text = row["UnitName"].ToString(); quantityValueLabel.Text = stock.ToString(); discountPercentLabel.Text = sale + "%"; oldPriceLabel.Text = price.ToString("0.00", CultureInfo.InvariantCulture) + " руб."; newPriceLabel.Text = finalPrice.ToString("0.00", CultureInfo.InvariantCulture) + " руб."; normalPriceLabel.Text = price.ToString("0.00", CultureInfo.InvariantCulture) + " руб."; oldPriceLabel.Visible = sale > 0; newPriceLabel.Visible = sale > 0; normalPriceLabel.Visible = sale == 0; } private void PaintByRules(int stock, int sale) { var cardColor = sale > 15 ? ColorTranslator.FromHtml("#2E8B57") : Color.White; var stockColor = stock == 0 && sale <= 15 ? Color.LightBlue : cardColor; PaintAll(cardColor); quantityTitleLabel.BackColor = stockColor; quantityValueLabel.BackColor = stockColor; discountPercentLabel.ForeColor = Color.Black; } private void PaintAll(Color color) { var controls = new Control[] { this, photoPanel, infoPanel, discountPanel, titleInfoLabel, descriptionTitleLabel, descriptionValueLabel, manufacturerTitleLabel, manufacturerValueLabel, supplierTitleLabel, supplierValueLabel, unitTitleLabel, unitValueLabel, quantityTitleLabel, quantityValueLabel, priceTitleLabel, oldPriceLabel, newPriceLabel, normalPriceLabel, discountTitleLabel, discountPercentLabel }; foreach (var control in controls) { control.BackColor = color; } } private void AddClickHandler(Control control) { control.Click += Card_Click; foreach (Control child in control.Controls) { AddClickHandler(child); } } private void Card_Click(object sender, EventArgs e) { if (ProductSelected != null) { ProductSelected(this, EventArgs.Empty); } } } }