Código
using System; using System.Collections; 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; namespace Fase3_LeoPaez { public partial class Form1 : Form { public Form1() { InitializeComponent(); Inhabilitar(); } private void registarToolStripMenuItem_Click(object sender, EventArgs e) { if (txtCedula.Text == "") { eperror.SetError(txtCedula, "Debe ingresar un numero de cedula"); txtCedula.Focus(); return; } eperror.SetError(txtCedula, ""); decimal num; if (!Decimal.TryParse(txtCedula.Text, out num)) { eperror.SetError(txtCedula, "Debe ingresar un valor numerico"); txtCedula.Focus(); return; } eperror.SetError(txtCedula, ""); if(Existe(txtCedula.Text)) { eperror.SetError(txtCedula, "Esta cedula ya ha sido registrada"); txtCedula.Focus(); return; } if (txtNombre.Text == "") { eperror.SetError(txtNombre, "Debe ingresar un Nombre"); txtNombre.Focus(); return; } eperror.SetError(txtNombre, ""); if (txtCorreo.Text == "") { eperror.SetError(txtCorreo, "Debe ingresar un correo"); txtCorreo.Focus(); return; } eperror.SetError(txtCorreo, ""); decimal numero; if (!Decimal.TryParse(txtEdad.Text, out numero)) { eperror.SetError(txtEdad, "Debe ingresar un valor numerico"); txtEdad.Focus(); return; } eperror.SetError(txtEdad, ""); miPersona.Cedula = Decimal.Parse(txtCedula.Text); miPersona.Nombre = txtNombre.Text; miPersona.Email = txtCorreo.Text; miPersona.Edad = Decimal.Parse(txtEdad.Text); Personas.Add(miPersona); dgvDatos.DataSource = null; dgvDatos.DataSource = Personas; Limpiar(); txtCedula.Focus(); } private bool Existe(string Cedula) { foreach (Persona_Leo Persona in Personas) { if (Persona.Cedula.ToString() == Cedula) return true; } return false; } public void Limpiar() { txtCedula.Clear(); txtNombre.Clear(); txtCorreo.Clear(); txtEdad.Clear(); } public void Inhabilitar() { eliminarToolStripMenuItem.Enabled = false; } private void salirToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void eliminarToolStripMenuItem_Click(object sender, EventArgs e) { if (Personas.Count != 0) { MessageBox.Show("Se elimino el registro", "Atención"); Limpiar(); } else { MessageBox.Show("La lista esta vacia", "Atención"); Limpiar(); } } private void buscarToolStripMenuItem_Click(object sender, EventArgs e) { if (txtCedula.Text == "") { eperror.SetError(txtCedula, "Debe ingresar una cedula"); txtCedula.Focus(); return; } eperror.SetError(txtCedula, ""); Persona_Leo miPersona = Obtener(txtCedula.Text); if (miPersona == null) { eperror.SetError(txtCedula, "El Trabajador no existe"); txtCedula.Focus(); return; } eperror.SetError(txtCedula, ""); txtCedula.Text = miPersona.Cedula.ToString(); txtNombre.Text = miPersona.Nombre; txtCorreo.Text = miPersona.Email; txtEdad.Text = miPersona.Edad.ToString(); eliminarToolStripMenuItem.Enabled = true; } private Persona_Leo Obtener(String Cedula) { foreach (Persona_Leo miPersona in Personas) { if (miPersona.Cedula.ToString() == Cedula) return miPersona; } return null; } private void limpiarToolStripMenuItem_Click(object sender, EventArgs e) { Limpiar(); } } }