Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
180 lines (165 loc) · 5.29 KB
/
Copy pathForm1.cs
File metadata and controls
180 lines (165 loc) · 5.29 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
namespace Notepad
{
public partial class Form1 : Form
{
private string path;
public Form1()
{
InitializeComponent();
path = null;
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (path != null)
{
// Save with the appropriate format
SaveFileWithFormat(path);
rtb_body.Clear();
path = null;
}
else
{
if (saveFD.ShowDialog() == DialogResult.OK)
{
// Save file
SaveFileWithFormat(saveFD.FileName);
rtb_body.Clear();
}
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFD.ShowDialog() == DialogResult.OK)
{
// Save file
SaveFileWithFormat(saveFD.FileName);
rtb_body.Clear();
path = null;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Open file
if (rtb_body.Text != "")
{
if (MessageBox.Show("Do you want to save changes?", "Notepad", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
}
LoadFileWithFormat(openFileDialog1.FileName);
path = openFileDialog1.FileName;
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (rtb_body.Text != "")
{
if (MessageBox.Show("Do you want to save changes?", "Notepad", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
else
{
rtb_body.Clear();
path = null;
}
}
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (rtb_body.Text != "")
{
if (MessageBox.Show("Do you want to save changes?", "Notepad", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
this.Close();
}
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontD.ShowDialog() == DialogResult.OK)
{
if (rtb_body.SelectedText != "")
{
rtb_body.SelectionFont = fontD.Font;
}
else
{
rtb_body.Font = fontD.Font;
}
}
}
// Helper method to save file with appropriate format
private void SaveFileWithFormat(string fileName)
{
if (fileName.EndsWith(".rtf"))
{
rtb_body.SaveFile(fileName, RichTextBoxStreamType.RichText);
}
else
{
rtb_body.SaveFile(fileName, RichTextBoxStreamType.PlainText);
}
}
// Helper method to load file with appropriate format
private void LoadFileWithFormat(string fileName)
{
if (fileName.EndsWith(".rtf"))
{
// Load file with rich text format
rtb_body.LoadFile(fileName, RichTextBoxStreamType.RichText);
}
else
{
// Load file with plain text format
rtb_body.LoadFile(fileName, RichTextBoxStreamType.PlainText);
}
}
private void fontColorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorD.ShowDialog() == DialogResult.OK)
{
if (rtb_body.SelectedText != "")
{
rtb_body.SelectionColor = colorD.Color;
}
else
{
rtb_body.ForeColor = colorD.Color;
}
}
}
private void backgroundColorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorD.ShowDialog() == DialogResult.OK)
{
if (rtb_body.SelectedText != "")
{
rtb_body.SelectionBackColor = colorD.Color;
}
else
{
rtb_body.BackColor = colorD.Color;
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (rtb_body.Text != "")
{
if (MessageBox.Show("Do you want to save changes?", "Notepad", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
else
{
this.Close();
}
}
}
}
}