private void btnSearch_Click(object sender, EventArgs e) { richTextBoxLog.Text = ""; if (tbxLogName.Text != "") { List <Person> foundPersonList = school.GetPersons(tbxLogName.Text); if (foundPersonList.Count != 0) { richTextBoxLog.Text = "Found:\n\n"; foreach (Person person in foundPersonList) { richTextBoxLog.Text += person + "\n---------------------\n"; } } } }
private void btnLoad_Click(object sender, EventArgs e) { using (OpenFileDialog sfd = new OpenFileDialog()) { sfd.Title = "Load data"; sfd.FileName = "DataFile"; sfd.DefaultExt = ".dat"; sfd.Filter = "Data files (.dat)|*.dat"; sfd.AddExtension = true; if (sfd.ShowDialog() == DialogResult.OK) { try { FileStream fs = new FileStream(sfd.FileName, FileMode.Open, FileAccess.Read); try { BinaryFormatter bf = new BinaryFormatter(); school = (School)bf.Deserialize(fs); lbxPersons.Items.Clear(); foreach (Person person in school.GetPersons()) { lbxPersons.Items.Add(person); } } catch (SerializationException ex) { MessageBox.Show(ex.Message); } catch (IOException ex) { MessageBox.Show(ex.Message); } finally { if (fs != null) { fs.Close(); } } } catch (IOException) { MessageBox.Show("Error opening file"); } } sfd.Dispose(); } }