private void tsmiSaveAs_Click(object sender, EventArgs e) { if (this.tsmTabbed.Checked) { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { TabPage selectedTab = this.tcMain.SelectedTab; foreach (Control control in selectedTab.Controls) { try { CustomRichTextBox richTextBox = (CustomRichTextBox)control; System.IO.File.WriteAllText(saveFileDialog.FileName, richTextBox.Text); Model.Content content = richTextBox.Content; content.FileName = saveFileDialog.FileName; content.offDirtyBit(); this.Text = "MyOwnTextEditor - " + content.FileName; this.tcMain.SelectedTab.Text = content.FileName; richTextBox.Select(); } catch (InvalidCastException ex) { } } } catch (Exception exception) { MessageBox.Show("There was an error writing the file"); } } } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { Form form = this.ActiveMdiChild; CustomRichTextBox richTextBox = (CustomRichTextBox)form.Controls[0]; System.IO.File.WriteAllText(saveFileDialog.FileName, richTextBox.Text); Model.Content content = richTextBox.Content; content.FileName = saveFileDialog.FileName; content.offDirtyBit(); this.Text = "MyOwnTextEditor - " + content.FileName; form.Text = content.FileName; richTextBox.Select(); } catch (NullReferenceException ex) { } } } }
private void open() { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { try { if (this.tsmTabbed.Checked) { TabPage selectedTab = this.tcMain.SelectedTab; foreach (Control control in selectedTab.Controls) { try { CustomRichTextBox richTextBox = (CustomRichTextBox)control; richTextBox.Text = System.IO.File.ReadAllText(openFileDialog.FileName); Model.Content content = richTextBox.Content; content.FileName = openFileDialog.FileName; content.offDirtyBit(); this.Text = "MyOwnTextEditor - " + content.FileName; selectedTab.Text = content.FileName; richTextBox.Select(); } catch (InvalidCastException e) { } } } else { Form form = this.MdiChildren.Last(); try { CustomRichTextBox richTextBox = (CustomRichTextBox)form.Controls[0]; richTextBox.Text = System.IO.File.ReadAllText(openFileDialog.FileName); Model.Content content = richTextBox.Content; content.FileName = openFileDialog.FileName; content.offDirtyBit(); form.Text = content.FileName; richTextBox.Select(); } catch (InvalidCastException ex) { } } } catch (Exception exception) { MessageBox.Show("There was an error reading the file"); } } }
private void tsmiSave_Click(object sender, EventArgs e) { if (this.tsmTabbed.Checked) { if (customTextRichBoxes[tcMain.SelectedIndex].Content.FileName == String.Empty) { this.tsmiSaveAs_Click(sender, e); } else { TabPage selectedTab = this.tcMain.SelectedTab; foreach (Control control in selectedTab.Controls) { try { CustomRichTextBox richTextBox = (CustomRichTextBox)control; System.IO.File.WriteAllText(this.customTextRichBoxes[this.tcMain.SelectedIndex].Content.FileName, richTextBox.Text); Model.Content content = this.customTextRichBoxes[this.tcMain.SelectedIndex].Content; content.offDirtyBit(); selectedTab.Text = content.FileName; richTextBox.Select(); } catch (InvalidCastException ex) { } } } } else { try { Form form = this.ActiveMdiChild; CustomRichTextBox richTextBox = (CustomRichTextBox)form.Controls[0]; if (richTextBox.Content.FileName == String.Empty) { this.tsmiSaveAs_Click(sender, e); } else { System.IO.File.WriteAllText(richTextBox.Content.FileName, richTextBox.Text); Model.Content content = richTextBox.Content; content.offDirtyBit(); form.Text = content.FileName; richTextBox.Select(); } } catch (NullReferenceException ex) { } } }