-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUserControlEditFile.cs
80 lines (74 loc) · 2.15 KB
/
UserControlEditFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.IO;
using System.Windows.Forms;
namespace GitForce
{
public partial class UserControlEditFile : UserControl
{
/// <summary>
/// True if the content of a text box is modified.
/// </summary>
public bool Dirty;
public UserControlEditFile()
{
InitializeComponent();
}
/// <summary>
/// Loads specified text file to edit
/// </summary>
public bool LoadFile(string file)
{
bool result = true;
labelFileName.Text = file;
textBox.Text = "";
try
{
using (StreamReader sr = new StreamReader(file))
{
while (!sr.EndOfStream)
{
textBox.Text += sr.ReadLine() + Environment.NewLine;
}
}
}
catch (Exception)
{
textBox.Text = "(Unable to load file)";
result = false;
}
Dirty = false;
textBox.Enabled = result;
return result;
}
/// <summary>
/// Saves the edited content of the text box into a file
/// </summary>
public bool SaveFile(string file)
{
// Dont attempt to save non-loaded content
if (textBox.Enabled == false)
return false;
bool result = true;
try
{
using (StreamWriter sw = new StreamWriter(file))
sw.WriteLine(textBox.Text);
labelFileName.Text = file;
Dirty = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Edit file", MessageBoxButtons.OK, MessageBoxIcon.Error);
result = false;
}
return result;
}
/// <summary>
/// User modified the content of a text box. Mark it as dirty.
/// </summary>
private void TextBoxTextChanged(object sender, EventArgs e)
{
Dirty = true;
}
}
}