Fixed resource leak when opening files within the same file.

This commit is contained in:
Lukasz Baldyga committed 2020-10-23 11:42:46 +01:00
1 parent b888088f0b
commit d081500dcb
2 files changed
+18 -10

No files matched your search

@@ -187,6 +187,7 @@ namespace Advanced_Software_Engineering
// timer1 // timer1
// //
this.timer1.Interval = 1000; this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
// //
// Text_Editor // Text_Editor
// //
@@ -39,6 +39,7 @@ namespace Advanced_Software_Engineering {
newWindow = false; newWindow = false;
okToOverwrite = true; okToOverwrite = true;
UpdateTitle(openFile.FileName); UpdateTitle(openFile.FileName);
fileStream.Close();
} }
void UpdateTitle(string fileName) { void UpdateTitle(string fileName) {
@@ -55,14 +56,15 @@ namespace Advanced_Software_Engineering {
private void saveFile() { private void saveFile() {
if (!okToOverwrite) saveFileAs(); if (!okToOverwrite) saveFileAs();
else { else {
StreamWriter fileStream = new StreamWriter(fileName);
try { try {
StreamWriter fileStream = new StreamWriter(fileName);
fileStream.Write(textBox1.Text); fileStream.Write(textBox1.Text);
unsavedChanges = false; unsavedChanges = false;
showSavedMessage();
fileStream.Close();
} catch (Exception e) { } catch (Exception e) {
new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show(); new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show();
} finally { } finally {
fileStream.Close();
UpdateTitle(); UpdateTitle();
} }
} }
@@ -83,6 +85,7 @@ namespace Advanced_Software_Engineering {
unsavedChanges = false; unsavedChanges = false;
okToOverwrite = true; okToOverwrite = true;
fileName = saveFileDialog.FileName; fileName = saveFileDialog.FileName;
showSavedMessage();
} catch (Exception e) { } catch (Exception e) {
new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show(); new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show();
} finally { } finally {
@@ -92,6 +95,11 @@ namespace Advanced_Software_Engineering {
} }
} }
private void showSavedMessage() {
label1.Text = "File saved";
timer1.Start();
}
private void updateRowCol() { private void updateRowCol() {
int line = textBox1.GetLineFromCharIndex(textBox1.SelectionStart); int line = textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
int column = textBox1.SelectionStart - textBox1.GetFirstCharIndexFromLine(line); int column = textBox1.SelectionStart - textBox1.GetFirstCharIndexFromLine(line);
@@ -112,13 +120,7 @@ namespace Advanced_Software_Engineering {
} }
private void openFile() { private void openFile() {
DialogResult result = DialogResult.Yes;
if (unsavedChanges) {
result = MessageBox.Show("You have unsaved work. Would you like to discard your work and open a new file?", "Unsaved work!", MessageBoxButtons.YesNo, MessageBoxIcon.None);
}
if (result == DialogResult.Yes) {
OpenFileDialog openFileDialog = new OpenFileDialog(); OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK) { if (openFileDialog.ShowDialog() == DialogResult.OK) {
@@ -127,11 +129,13 @@ namespace Advanced_Software_Engineering {
StreamReader fileStream = new StreamReader(openFileDialog.OpenFile()); StreamReader fileStream = new StreamReader(openFileDialog.OpenFile());
textBox1.Text = fileStream.ReadToEnd(); textBox1.Text = fileStream.ReadToEnd();
unsavedChanges = false; unsavedChanges = false;
okToOverwrite = true;
newWindow = false; newWindow = false;
UpdateTitle(openFileDialog.FileName); UpdateTitle(openFileDialog.FileName);
fileStream.Close();
} else new Text_Editor(openFileDialog).Show(); } else new Text_Editor(openFileDialog).Show();
} }
}
} }
private void RunCode() { private void RunCode() {
@@ -233,6 +237,9 @@ namespace Advanced_Software_Engineering {
private void describeToolStripMenuItem_Click(object sender, EventArgs e) => DescribeCode(); private void describeToolStripMenuItem_Click(object sender, EventArgs e) => DescribeCode();
private void timer1_Tick(object sender, EventArgs e) {
updateRowCol();
timer1.Stop();
}
} }
} }