Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 System.IO;
- namespace WindowsFormsApplication4
- {
- public partial class Form1 : Form
- {
- Visitatori x = new Visitatori();
- private string cartella;
- private bool modificato = false;
- private string Serial;
- public Form1()
- {
- cartella = Environment.CurrentDirectory;
- Serial = Path.Combine(cartella, "Save.dat");
- modificato = false;
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Deserialize();
- dataGridView1.DataSource = x.list;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- x.aggiungi(comboBox1.Text);
- x.ordina();
- dataGridView1.DataSource = null;
- dataGridView1.DataSource = x.list;
- modificato = true;
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- button1.Enabled = true;
- }
- private void Serialize()
- {
- using (Stream stream = File.Open(Serial, FileMode.Create))
- {
- var binaryformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
- binaryformatter.Serialize(stream, x.list);
- }
- }
- private void Deserialize()
- {
- if (File.Exists(Serial))
- {
- using (Stream stream = File.Open(Serial, FileMode.Open))
- {
- var binaryformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
- x.list = (List<Visitatore>)binaryformatter.Deserialize(stream);
- }
- dataGridView1.Visible = true;
- }
- }
- private void salvaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Serialize();
- modificato = false;
- }
- private void esciToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (modificato == true)
- {
- DialogResult risposta = MessageBox.Show("Vuoi salvare le modifiche applicate?", "GuestBook", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
- if (risposta.Equals(DialogResult.Yes))
- {
- Serialize();
- }
- else if (risposta.Equals(DialogResult.No))
- {
- Application.Exit();
- }
- else
- {
- e.Cancel = (risposta.Equals(DialogResult.Cancel));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement