-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
289 lines (260 loc) · 10.5 KB
/
MainForm.cs
File metadata and controls
289 lines (260 loc) · 10.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace Notepad
{
public partial class MainForm : Form
{
MdiClient mdiClient = null;
public MainForm()
{
InitializeComponent();
AutoSavingTimer.Start();
for (int i = 0; i < Controls.Count; i++)
{
mdiClient = Controls[i] as MdiClient;
if (mdiClient != null)
break;
}
mdiClient.BackColor = Color.LightCyan;
foreach (FontFamily font in FontFamily.Families)
fontsToolStripComboBox.Items.Add(font.Name);
for (int i = 8; i <= 72; i++)
fontSizeToolStripComboBox.Items.Add(i);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openDialog.ShowDialog() == DialogResult.OK)
{
ShowChildForm(new TextForm(openDialog.FileName));
}
else return;
}
private void ShowChildForm(Form form)
{
foreach (Form child in MdiChildren)
{
if (child is TextForm)
child.WindowState = FormWindowState.Minimized;
}
form.MdiParent = this;
form.WindowState = FormWindowState.Maximized;
form.Show();
}
private void saveAllWithFormatToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form child in MdiChildren)
{
if (child is TextForm)
(child as TextForm).SaveFile(true);
}
}
private void saveAllWithoutFormatToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form child in MdiChildren)
{
if (child is TextForm)
(child as TextForm).SaveFile(false);
}
}
protected bool AutoSavingFormat = true;
private void AutoSavingTimer_Tick(object sender, EventArgs e)
{
foreach (Form child in MdiChildren)
{
if (child is TextForm)
(child as TextForm).SaveFile(AutoSavingFormat);
}
}
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
SettingsForm settings = new SettingsForm(AutoSavingTimer.Interval,AutoSavingFormat,menuStrip.BackColor==Color.PeachPuff);
settings.Owner = this;
settings.ShowDialog();
}
public void ChangeSettings(int interval, bool savingFormat,bool light)
{
AutoSavingTimer.Interval = interval;
AutoSavingFormat = savingFormat;
if (light)
{
menuStrip.BackColor = Color.PeachPuff;
toolStrip.BackColor = Color.LemonChiffon;
mdiClient.BackColor = Color.LightCyan;
}
else
{
menuStrip.BackColor = Color.MediumPurple;
toolStrip.BackColor = Color.PowderBlue;
mdiClient.BackColor = Color.MidnightBlue;
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Form child in MdiChildren)
{
if (child is TextForm && (child as TextForm).IsChanged)
{
DialogResult result = MessageBox.Show($"Сохранить изменения в {(child as TextForm).fileName}?", "Сохранение изменений",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
(child as TextForm).SaveFile(AutoSavingFormat);
}
else if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
StringBuilder fileNames = new StringBuilder("");
foreach (Form child in MdiChildren)
{
if (child is TextForm)
fileNames.Append((child as TextForm).fileName + ';');
}
Properties.Settings.Default.fileNames = fileNames.ToString();
Properties.Settings.Default.savingInterval = AutoSavingTimer.Interval;
Properties.Settings.Default.withFormat = AutoSavingFormat;
if (menuStrip.BackColor == Color.PeachPuff)
Properties.Settings.Default.lightColor = true;
else
Properties.Settings.Default.lightColor = false;
Properties.Settings.Default.Save();
}
private void italicsToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).MakeItalics();
}
private void boldToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).MakeBold();
}
private void underlineToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).MakeUnderline();
}
private void strikeOutToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).MakeStrikeout();
}
private void registerToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).ChangeRegister();
}
private void FontColorToolStripButton_Click(object sender, EventArgs e)
{
if (colorDialog.ShowDialog() == DialogResult.Cancel)
return;
FontColorToolStripButton.BackColor = colorDialog.Color;
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).ChangeSelectionColor(colorDialog.Color);
}
private void fontsToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).ChangeSelectedFont(fontsToolStripComboBox.SelectedItem.ToString());
fontsToolStripComboBox.Font = new Font(fontsToolStripComboBox.SelectedItem.ToString(), 11);
}
private void formatToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog.ShowDialog() == DialogResult.Cancel)
return;
foreach (Form child in MdiChildren)
{
if (child is TextForm)
(child as TextForm).ChangeFont(fontDialog.Font, fontDialog.Color);
}
}
private void saveHowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveDialog.ShowDialog() == DialogResult.OK)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).SaveFileHow(saveDialog.FileName);
else
MessageBox.Show("Выберите форму, которую необходимо сохранить, и повторите попытку!", "Ошибка");
}
else
return;
}
private void fontSizeToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).ChangeSize(int.Parse(fontSizeToolStripComboBox.SelectedItem.ToString()));
}
private void createNewWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveDialog.FileName, "");
MainForm form = new MainForm();
form.Show();
form.ShowChildForm(new TextForm(saveDialog.FileName));
}
else
return;
}
private void createInsideToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveDialog.FileName, "");
ShowChildForm(new TextForm(saveDialog.FileName));
}
else
return;
}
private void saveActiveWithFormatToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).SaveFile(true);
}
private void saveActiveWithoutFormatToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
(ActiveMdiChild as TextForm).SaveFile(false);
}
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
string[] fileNames = Properties.Settings.Default.fileNames.Split(';',StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < fileNames.Length; i++)
{
ShowChildForm(new TextForm(fileNames[i]));
}
ChangeSettings(Properties.Settings.Default.savingInterval, Properties.Settings.Default.withFormat, Properties.Settings.Default.lightColor);
Properties.Settings.Default.Save();
}
private void undoToolStripButton_Click(object sender, EventArgs e)
{
if(ActiveMdiChild is TextForm)
{
(ActiveMdiChild as TextForm).Undo();
}
}
private void redoToolStripButton_Click(object sender, EventArgs e)
{
if (ActiveMdiChild is TextForm)
{
(ActiveMdiChild as TextForm).Redo();
}
}
}
}