Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace WindowsFormsApplication4
  13. {
  14. public partial class Form1 : Form
  15. {
  16. Visitatori x = new Visitatori();
  17.  
  18. private string cartella;
  19. private bool modificato = false;
  20. private string Serial;
  21.  
  22. public Form1()
  23. {
  24. cartella = Environment.CurrentDirectory;
  25. Serial = Path.Combine(cartella, "Save.dat");
  26. modificato = false;
  27. InitializeComponent();
  28. }
  29.  
  30. private void Form1_Load(object sender, EventArgs e)
  31. {
  32. Deserialize();
  33. dataGridView1.DataSource = x.list;
  34. }
  35.  
  36. private void button1_Click(object sender, EventArgs e)
  37. {
  38. x.aggiungi(comboBox1.Text);
  39. x.ordina();
  40.  
  41. dataGridView1.DataSource = null;
  42. dataGridView1.DataSource = x.list;
  43.  
  44. modificato = true;
  45. }
  46.  
  47. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  48. {
  49. button1.Enabled = true;
  50. }
  51.  
  52. private void Serialize()
  53. {
  54. using (Stream stream = File.Open(Serial, FileMode.Create))
  55. {
  56. var binaryformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  57. binaryformatter.Serialize(stream, x.list);
  58. }
  59. }
  60.  
  61. private void Deserialize()
  62. {
  63. if (File.Exists(Serial))
  64. {
  65. using (Stream stream = File.Open(Serial, FileMode.Open))
  66. {
  67. var binaryformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  68. x.list = (List<Visitatore>)binaryformatter.Deserialize(stream);
  69. }
  70.  
  71. dataGridView1.Visible = true;
  72. }
  73. }
  74.  
  75. private void salvaToolStripMenuItem_Click(object sender, EventArgs e)
  76. {
  77. Serialize();
  78. modificato = false;
  79. }
  80.  
  81. private void esciToolStripMenuItem_Click(object sender, EventArgs e)
  82. {
  83. Application.Exit();
  84. }
  85.  
  86. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  87. {
  88. if (modificato == true)
  89. {
  90. DialogResult risposta = MessageBox.Show("Vuoi salvare le modifiche applicate?", "GuestBook", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  91. if (risposta.Equals(DialogResult.Yes))
  92. {
  93. Serialize();
  94. }
  95. else if (risposta.Equals(DialogResult.No))
  96. {
  97. Application.Exit();
  98. }
  99. else
  100. {
  101. e.Cancel = (risposta.Equals(DialogResult.Cancel));
  102. }
  103. }
  104. }
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement