Exemplo n.º 1
0
        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            //Fase 1
            //Fetch all data from the fields in the form view (MainW Window)

            string patente = txtPatente.Text;

            //SelectedIndex returns an int (cbo element position).
            //marca var doesn't support an int so a parse action is needeed (Marcas).
            Marcas marca = (Marcas)cboMarca.SelectedIndex;

            string modelo = txtModelo.Text;

            //textAnio returns a string so a parse action is needed, eg: int.Parse()
            //However an user could type a letter which will crashed "int.Parse()"
            //Finally anio is set 0, in order to validate via IF statement
            int anio = 0;

            //int.TryParse (returns TRUE or FALSE) if parse action was accomplished
            //<out> convierte en "variable de salida" a <anio>
            if (int.TryParse(txtAnio.Text, out anio) == false)
            {
                MessageBox.Show("El año debe ser in número", "ERROR");
                //return to stop the execution
                return;
            }
            // .Value returns False or True based on what the user typed
            bool nuevo = chkNuevo.IsChecked.Value;

            //transmision var is set
            Transmisiones transmision = Transmisiones.Automatica;

            if (rbtMecanica.IsChecked == true)
            {
                transmision = Transmisiones.Mecanica;
            }

            try
            {
                //Fase 2
                //Create an instance of automovil
                Automovil auto = new Automovil();
                auto.Patente     = patente;
                auto.Marca       = marca;
                auto.Modelo      = modelo;
                auto.Anio        = anio;
                auto.Nuevo       = nuevo;
                auto.Transmision = transmision;

                //Fase 3
                ////Guardar los datos en la coleccion
                if (_coleccion.GuardarAutomovil(auto))
                {
                    MessageBox.Show("Guardado correctamente");
                }
                else
                {
                    MessageBox.Show("La patente ya existe");
                }

                //show car info in DataGrid
                CargarGrilla();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }