private void agregar_libro_btn_Click(object sender, EventArgs e) { if (OnAgregarLibro != null) { List <Libro> libros = null; try { using (Stream stream = new FileStream("Libros.bin", FileMode.Open, FileAccess.Read, FileShare.Read)) { IFormatter formatter = new BinaryFormatter(); libros = (List <Libro>)formatter.Deserialize(stream); stream.Close(); } } catch (IOException) { } AgregarLibroEventArgs libroArgs = new AgregarLibroEventArgs(); libroArgs.Titulo = this.titulo_txtbox.Text; libroArgs.Autor = this.autor_txtbox.Text; int number; libroArgs.CarreraAsociada = this.CarreraAsociada_txtBox.Text; libroArgs.FechaCreacion = this.fecha_pub_txtbox.Text; libroArgs.piso = Int32.Parse(this.numericUpDown1.Value.ToString()); libroArgs.zona = this.comboBox1.Text; libroArgs.librero = Int32.Parse(this.numericUpDown2.Value.ToString()); libroArgs.estante = Int32.Parse(this.numericUpDown3.Value.ToString()); bool result = Int32.TryParse(this.copias_txtbox.Text, out number); int real = 0; if (!result) { MessageBox.Show("Favor ingrese un numero para el campo 'Copias'"); } else { foreach (Libro l in libros) { if (libroArgs.piso == l.GetUbicacion().Piso&& libroArgs.zona.ToLower() == l.GetUbicacion().Sector&& libroArgs.librero == l.GetUbicacion().Librero&& libroArgs.estante == l.GetUbicacion().Estante) { MessageBox.Show("Ubicacion ocupada"); real = 1; break; } } if (real == 0 && Int32.Parse(this.copias_txtbox.Text) > 0) { libroArgs.Copia = Int32.Parse(this.copias_txtbox.Text); OnAgregarLibro(this, libroArgs); this.mainForm.ActualizarLibros(this.titulo_txtbox.Text); MessageBox.Show("Libro agregado con éxito!"); } else if (Int32.Parse(this.copias_txtbox.Text) <= 0) { MessageBox.Show("Ingrese un numero mayor a 0 para el campo 'Copias'"); } } } }
private void Vista_Administrador_OnAgregarLibro(object sender, AgregarLibroEventArgs e) { Ubicacion ubicacion = new Ubicacion(e.zona, e.piso, e.librero, e.estante); Libro libro = new Libro(e.CarreraAsociada, e.Copia, e.Autor, e.FechaCreacion, 0, new List <string>(), ubicacion, e.Titulo); Libros.Add(libro); using (Stream stream = new FileStream("Libros.bin", FileMode.Create, FileAccess.Write, FileShare.None)) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, Libros); stream.Close(); } }