cosas que faltaban
This commit is contained in:
113
Vista/FrmProducto.cs
Normal file
113
Vista/FrmProducto.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using Controladora;
|
||||
using Entidades;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace Vista
|
||||
{
|
||||
public partial class FrmProducto : Form
|
||||
{
|
||||
public FrmProducto()
|
||||
{
|
||||
InitializeComponent();
|
||||
CargarCategorias();
|
||||
}
|
||||
|
||||
private void CargarCategorias()
|
||||
{
|
||||
// Obtener la lista de categorías desde la controladora
|
||||
var categorias = ControladoraCategorias.Instance.Listar();
|
||||
|
||||
// Configurar el ComboBox para categorías
|
||||
comboBox1.DisplayMember = "Descripcion"; // Mostrar la propiedad Descripcion
|
||||
comboBox1.ValueMember = "Id"; // Usar la propiedad Id como valor
|
||||
|
||||
// Asignar la lista de categorías al ComboBox
|
||||
comboBox1.DataSource = categorias;
|
||||
}
|
||||
|
||||
private bool ValidarDatos()
|
||||
{
|
||||
string devolucion = "";
|
||||
|
||||
// Validar Nombre
|
||||
if (string.IsNullOrEmpty(textBox1.Text))
|
||||
{
|
||||
devolucion += "El nombre del producto no puede estar vacío.\n";
|
||||
}
|
||||
else if (textBox1.Text.Length > 100) // Limite de caracteres
|
||||
{
|
||||
devolucion += "El nombre del producto no puede superar los 100 caracteres.\n";
|
||||
}
|
||||
|
||||
// Validar Precio
|
||||
if (numericUpDown2.Value <= 0)
|
||||
{
|
||||
devolucion += "El precio debe ser mayor a cero.\n";
|
||||
}
|
||||
|
||||
// Validar ID (Si es necesario)
|
||||
if (ControladoraProductos.Instance.Listar().Any(p => p.Id == (int)numericUpDown1.Value))
|
||||
{
|
||||
devolucion += "Ya existe un producto con el mismo ID.\n";
|
||||
}
|
||||
|
||||
// Validar Categoría Seleccionada
|
||||
if (comboBox1.SelectedItem == null)
|
||||
{
|
||||
devolucion += "Debe seleccionar una categoría.\n";
|
||||
}
|
||||
|
||||
if (devolucion == "")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show(devolucion, "Errores de Validación", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (ValidarDatos())
|
||||
{
|
||||
// Crear nuevo producto
|
||||
var nuevoProducto = new Producto
|
||||
{
|
||||
Id = (int)numericUpDown1.Value,
|
||||
Nombre = textBox1.Text,
|
||||
Precio = (double)numericUpDown2.Value,
|
||||
Habilitado = checkBox1.Checked,
|
||||
Categoria = (Categoria)comboBox1.SelectedItem, // Asignar categoría seleccionada
|
||||
};
|
||||
|
||||
// Agregar el producto usando la controladora
|
||||
string mensaje = ControladoraProductos.Instance.Añadir(nuevoProducto);
|
||||
|
||||
MessageBox.Show(mensaje, "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
|
||||
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
// Puedes usar este método para manejar cambios en el ComboBox si es necesario
|
||||
// No es necesario mantener una variable separada para la categoría seleccionada
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user