jueves, 7 de junio de 2012

Uso de Clses y Objetos

El siguiente ejercicios es para implementar el uso de Clases , Objetos, Campos, Propiedades y Herencia...
Métodos del Formulario:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EjemploClase05062012
{
    public partial class Form1 : Form
    {
        private List<object> ListaAlumnos = new List<object>();
        private int Sw = 0;

       
        public Form1()
        {
            InitializeComponent();
            Sw = 1;
        }
       
        private void btnAgregar_Click(object sender, EventArgs e)
        {
            Alumnos  Datos;
            Datos = new Alumnos  ();

            try
            {
                Datos.Matricula = int.Parse (txtMatricula.Text);
                Datos.Nombre = txtNombre.Text;
                Datos.Apellidos = txtApellidos.Text ;
                Datos.Edad = int.Parse ( txtEdad.Text);
                Datos.Universidad = cmbUniversidad.Text;
               
                Datos.Calif1 = float.Parse(txtCalif1.Text);
                Datos.Calif2 = float.Parse(txtCalif2.Text);
                Datos.Calif3 = float.Parse(txtCalif3.Text);
                Datos.Final  = float.Parse(txtFinal.Text);

                foreach (Alumnos alu in ListaAlumnos)
                {
                    if (alu.Matricula == Datos.Matricula)
                    {
                        MessageBox.Show("El Alumno ya existe...");
                        Sw = 0;
                    }
                    else
                    {
                        Sw = 1;
                    }
                }
                if (Sw ==1)
                {
                    ListaAlumnos.Add(Datos);
                    LimpiarDatos();
                    Grid();   
                }
               
               
            }
            catch ( Exception er)
            {
                MessageBox.Show (er.Message);
               
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            dataGridView1.ClearSelection();
        }
        private void Grid()
        {
            dataGridView1.Rows.Clear();
            foreach (Alumnos alu in ListaAlumnos)
            {
                dataGridView1.Rows.Add(alu.Matricula, alu.Nombre, alu.Apellidos, alu.Edad
                    , alu.Universidad, alu.Calif1, alu.Calif2, alu.Calif3, alu.Final);
            }
        }

        private void LimpiarDatos()
        {
            txtMatricula.Clear ();
            txtNombre.Clear ();
             txtApellidos.Clear ();
            txtEdad.Clear ();
            cmbUniversidad.Text ="";

            txtCalif1.Clear ();
            txtCalif2.Clear ();
            txtCalif3.Clear ();
            txtFinal.Clear();
        }
        private void btnCargar_Click(object sender, EventArgs e)
        {
            Grid();
          
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
          
           
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            //if (Sw == 0)
            //{
                DataGridViewRow row = dataGridView1.CurrentRow;
                string varia = row.Cells[0].Value.ToString();
                int Matri = Convert.ToInt32(varia);

                int sw2 = 0;
                foreach (Alumnos alu in ListaAlumnos)
                {
                    if (alu.Matricula == Matri)
                    {
                        txtMatricula.Text = Convert.ToString(alu.Matricula);
                        txtNombre.Text = alu.Nombre;
                        txtApellidos.Text = alu.Apellidos;
                        txtEdad.Text = Convert.ToString(alu.Edad);
                        cmbUniversidad.Text = alu.Universidad;
                        txtCalif1.Text = Convert.ToString(alu.Calif1);
                        txtCalif2.Text = Convert.ToString(alu.Calif2);
                        txtCalif3.Text = Convert.ToString(alu.Calif3);
                        txtFinal.Text = Convert.ToString(alu.Final);
                        MessageBox.Show("El Alumno encontrado...\n"
                        + alu.Nombre + " " + alu.Apellidos);
                        sw2 = 1;
                    }
                }
                if (sw2 == 0)
                {
                    MessageBox.Show("El Alumno NO existe...");

                }
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Matricula = Convert .ToInt16( txtMatricula.Text);
            int sw3 = 0;
            foreach (Alumnos alu in ListaAlumnos)
            {
                if (alu.Matricula == Matricula)
                {
                    DialogResult r = MessageBox.Show("Esta seguro que desea eliminar al Alumno ?",
              " Confirmar por favor", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (r == DialogResult.Yes)
                    {
                        ListaAlumnos.Remove(alu);
                       
                        sw3 = 1;
                        LimpiarDatos();
                        Grid ();
                        goto Salto;
                    }
                    else
                    {
                        sw3 = 1;
                    }
                }
            }
        Salto:
            if (sw3 == 0)
            {
                MessageBox.Show("No existe el Alumno", " Confirmación",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Question);
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            DialogResult respuesta;

            respuesta = MessageBox.Show("¿Desea Salir? ", "Confirmar",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (respuesta == DialogResult.Yes)
            {
                //MessageBox.Show("Usted Está seguro ");
                Application.Exit();
            }
        }

        private void btnCalcular_Click(object sender, EventArgs e)
        {
            if (txtCalif1.Text == "" || txtCalif2.Text == "" || txtCalif3.Text == "")
            {
                MessageBox.Show("Falata una Calificación", " Confirmación",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Warning);
            }
            else
            {
                int a = Convert.ToInt16(txtCalif1.Text);
                int b = Convert.ToInt16(txtCalif2.Text);
                int c = Convert.ToInt16(txtCalif3.Text);

                float prom = (a + b + c) / 3;
                txtFinal.Text = Convert.ToString(prom);
            }
        }
    }
}

Implementación de la Clase Personas y Alumnos:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EjemploClase05062012
{
    class Persona
    {
        protected string nombre;
        protected string apellidos;
        protected int edad;

    }

    class Alumnos: Persona
    {
        private string universidad;
        private float calif1;
        private float calif2;
        private float calif3;
        private float final;
        private int matricula;

        public int Matricula
        {
            get
            {
                return matricula;
            }
            set
            {
                matricula = value;
            }
        }
        public string  Universidad
        {
            get {
                return universidad;
            }
            set {
                universidad = value;
            }
        }
        public float Calif1
        {
            get
            {
                return calif1;
            }
            set
            {
                calif1 = value;
              
            }
        }
        public float Calif2
        {
            get
            {
                return calif2;
            }
            set
            {
                calif2 = value;
            }
        }
        public float Calif3
        {
            get
            {
                return calif3;
            }
            set
            {
                calif3 = value;
            }
        }
        public float Final
        {
            get
            {
                return final ;
            }
            set
            {
                final  = value;
            }
        }
        public string Nombre
        {
            get
            {
                return nombre ;
            }
            set
            {
                nombre  = value;
            }
        }
        public string Apellidos
        {
            get
            {
                return apellidos ;
            }
            set
            {
                apellidos  = value;
            }
        }
        public int Edad
        {
            get
            {
                return edad ;
            }
            set
            {
                edad  = value;
            }
        }

    }
}





No hay comentarios:

Publicar un comentario