/** * Click handler for the File -> Open MainMenu item. Implements opening a file using the built in * OpenFileDialog provided by .NET. The deserialized file is loaded into a FileSystemDocument object, and * then our MainForm's properties are set using the FileSystemDocument's properties. */ private void openToolStripMenuItem_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { if (dlg.ShowDialog() != DialogResult.OK) { return; } using (Stream stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read)) { IFormatter formatter = new BinaryFormatter(); Documents.Drivers.FileSystemDocument document = (Documents.Drivers.FileSystemDocument)formatter.Deserialize(stream); document.FilePath = dlg.FileName; this.TextBox.Text = document.Text; this.TextBox.BackColor = document.BackColor; this.TextBox.ForeColor = document.TextColor; this.TextBox.Font = document.Font; this.Location = document.DocumentLocation; this.Text = document.DocumentTitle; // Set this instance of the application's document properties to the ones just retrieved from // deserialization - needed for detecting if a file has been saved before in File->Save this.document = document; this.StatusLabel.Text = "Opened file: " + this.document.DocumentTitle; } } }
/** * Click handler for the File -> Save MainMenu item. Implements saving a file using the built in * SaveFileDialog provided by .NET. The FileSystemDocument properties are set, and then once set we pass * the stream and the document to Serialize to serialize the document's members. */ private void saveToolStripMenuItem_Click(object sender, EventArgs e) { // If the document is new, i.e. has never been saved before if (string.IsNullOrEmpty(this.document.DocumentTitle) || string.IsNullOrWhiteSpace(this.document.DocumentTitle)) { using (SaveFileDialog dlg = new SaveFileDialog()) { if (dlg.ShowDialog() != DialogResult.OK) { return; } using (Stream stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write)) { IFormatter formatter = new BinaryFormatter(); Documents.Drivers.FileSystemDocument document = new Documents.Drivers.FileSystemDocument(); document.Text = this.TextBox.Text; document.BackColor = this.TextBox.BackColor; document.TextColor = this.TextBox.ForeColor; document.Font = this.TextBox.Font; document.DocumentLocation = this.Location; document.DocumentTitle = Path.GetFileName(dlg.FileName); document.DocumentSize = this.Size; document.FilePath = dlg.FileName; formatter.Serialize(stream, document); this.document = document; changedText = false; this.StatusLabel.Text = "Saved"; } } this.Text = this.document.DocumentTitle; } // If the document not new, and has been saved before else { using (Stream stream = new FileStream(this.document.FilePath, FileMode.Create, FileAccess.Write)) { IFormatter formatter = new BinaryFormatter(); this.document.Text = this.TextBox.Text; this.document.BackColor = this.TextBox.BackColor; this.document.TextColor = this.TextBox.ForeColor; this.document.Font = this.TextBox.Font; this.document.DocumentSize = this.Size; this.document.DocumentLocation = this.Location; this.document.DocumentTitle = Path.GetFileName(document.FilePath); formatter.Serialize(stream, document); } changedText = false; this.Text = this.document.DocumentTitle; this.StatusLabel.Text = "Saved"; } }
/** * Click handler for the File -> New MainMenu item. Implements creating a new file/document by creating a * new instance of the FileSystemDocument class and setting this application's document to the newly * created document. The current application insance's TextBox and Text (title bar) are reset. Setting this * application insance's document to the newly created document also resets the FilePath associated with the * document. */ private void newToolStripMenuItem_Click(object sender, EventArgs e) { Documents.Drivers.FileSystemDocument document = new Documents.Drivers.FileSystemDocument(); this.document = document; this.TextBox.Text = this.document.Text; this.Text = "Notepad--"; this.StatusLabel.Text = "New file"; this.TextBox.BackColor = this.document.BackColor; this.TextBox.ForeColor = this.document.TextColor; this.TextBox.Font = this.document.Font; }