-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
403 lines (309 loc) · 12.5 KB
/
Form1.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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;
namespace textEditor
{
public partial class Form1 : Form
{
public static String openedFilePath;
public static String openedFileName = "doc1.txt";
public bool fileChanged = false;
public static Form1 form1;
public Form1()
{
InitializeComponent();
mainTextArea.AllowDrop = true;
mainTextArea.DragDrop += new DragEventHandler(mainTextArea_DragDrop);
form1 = this;
}
void mainTextArea_DragDrop(object sender, DragEventArgs e)
{
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) { // check so file is dropped
string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
if (files.Length == 1) { // check so max 1 file
if (Control.ModifierKeys == Keys.Control) // if control held down - add content to end of file
{
mainTextArea.Text += File.ReadAllText(files[0]);
//openedFileName = Path.GetFileName(files[0]);
//openedFilePath = files[0];
//this.Text = openedFileName;
} else if (Control.ModifierKeys == Keys.Shift) // if shift held down - add to position of cursor
{
mainTextArea.SelectedText += File.ReadAllText(files[0]);
} else // if nothing is pressed replace page with new file (check for save first)
{
checkFileChanged();
// Checking if file needs saving
if (fileChanged == true || this.Text == "doc1.txt*")
{
Form2 f2 = new Form2(form1);
f2.ShowDialog();
if (fileChanged == false && Form2.closeCancel == false)
{
this.Text = "doc1.txt";
mainTextArea.Text = File.ReadAllText(files[0]);
}
// checks if user clicked on cancel drag + drop action
if (Form2.closeCancel == true)
{
Form2.closeCancel = false;
return;
}
}
else
{
this.Text = "doc1.txt";
mainTextArea.Text = File.ReadAllText(files[0]);
}
}
}
else
{
MessageBox.Show("Please input 1 file at a time");
}
}
}
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
checkFileChanged();
// If there is no changes or the document is empty, start new blank page.
if (fileChanged == false && this.Text != "doc1.txt*")
{
fileChanged = false;
mainTextArea.Clear();
this.Text = "doc1.txt";
openedFileName = "doc1.txt";
openedFilePath = "";
}
else if (this.Text != "doc1.txt")
{
Form2 f2 = new Form2(form1);
f2.ShowDialog(); // Shows Form2
/*dynamic result = MessageBox.Show("Do you want to save changes to your text?", "Text Editor",
MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
saveToolStripButton.PerformClick();
}
if (result == DialogResult.No)
{
mainTextArea.Clear();
fileChanged = false;
this.Text = "doc1.txt";
}
} */
}
}
private void openToolStripButton_Click(object sender, EventArgs e)
{
checkFileChanged();
if ((fileChanged == true || this.Text == "doc1.txt*") && this.Text != "doc1.txt")
{
Form2 f2 = new Form2(form1);
f2.ShowDialog();
if (fileChanged == false && Form2.closeCancel == false)
{
openToolStripButton.PerformClick();
} else if (Form2.closeCancel == true)
{
Form2.closeCancel = false;
}
{
}
}
else
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openfile.Title = "Open file";
if (openfile.ShowDialog() == DialogResult.OK)
{
mainTextArea.Clear();
using (StreamReader sr = new StreamReader(openfile.FileName))
{
this.Text = openfile.SafeFileName;
openedFileName = this.Text;
openedFilePath = openfile.FileName;
mainTextArea.Text = sr.ReadToEnd();
fileChanged = false;
sr.Close();
}
}
}
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
if (this.Text == "doc1.txt*" || this.Text == "doc1.txt")
{
saveAsToolStripMenuItem.PerformClick();
} else
{
File.WriteAllText(openedFilePath, mainTextArea.Text);
this.Text = openedFileName;
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
savefile.Title = "Save file as";
if (savefile.ShowDialog() == DialogResult.OK)
{
StreamWriter txt = new StreamWriter(savefile.FileName);
FileInfo t = new FileInfo(savefile.FileName); // in order to get name without full path
txt.Write(mainTextArea.Text);
fileChanged = false;
this.Text = t.Name;
openedFileName = this.Text;
openedFilePath = savefile.FileName;
txt.Close();
}
}
private void checkFileChanged()
{
// Checking if a file is open, if so - checking for changes in the file.
if (this.Text != "doc1.txt" && this.Text !="doc1.txt*")
{
using (StreamReader sr = new StreamReader(openedFilePath))
{
if (mainTextArea.Text != sr.ReadToEnd())
{
fileChanged = true;
} else
{
fileChanged = false;
}
sr.Close();
}
}
}
private void mainTextArea_TextChanged(object sender, EventArgs e)
{
checkFileChanged();
if (fileChanged == true)
{
this.Text = openedFileName + "*";
}
else if (fileChanged == false && (this.Text != "doc1.txt" && this.Text != "doc1.txt*"))
{
this.Text = openedFileName;
}
else if (!String.IsNullOrWhiteSpace(mainTextArea.Text) && this.Text == "doc1.txt")
{
this.Text = "doc1.txt*";
}
if (String.IsNullOrWhiteSpace(mainTextArea.Text))
{
this.Text = "doc1.txt";
}
charWithSpace.Text = "Char with space: " + mainTextArea.Text.Count();
charNoSpace.Text = "Char no space: " + mainTextArea.Text.Count(c => !Char.IsWhiteSpace(c)).ToString();
amountOfWords.Text = "Words: " + (mainTextArea.Text.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length).ToString();
int rowAmount = mainTextArea.GetLineFromCharIndex(mainTextArea.TextLength) + 1; // to avoid setting wrapping to false;
amountOfRows.Text = "Rows: " + rowAmount;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
checkFileChanged();
if ((fileChanged == true || this.Text == "doc1.txt*") && this.Text != "doc1.txt")
{
Form2 f2 = new Form2(form1);
f2.ShowDialog();
if (Form2.closeCancel == true)
{
e.Cancel = true;
Form2.closeCancel = false;
return;
}
}
}
private void fileToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
{
fileToolStripMenuItem.ForeColor = Color.White;
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
fileToolStripMenuItem.ForeColor = Color.Black;
}
private void fileToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
fileToolStripMenuItem.ForeColor = Color.Black;
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
editToolStripMenuItem.ForeColor = Color.Black;
}
private void editToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
editToolStripMenuItem.ForeColor = Color.Black;
}
private void editToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
{
editToolStripMenuItem.ForeColor = Color.White;
}
private void Form1_Load(object sender, EventArgs e)
{
mainTextArea.AllowDrop = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
newToolStripButton.PerformClick();
}
// used to make method accesable in other form
public void savePerformClick()
{
saveToolStripMenuItem.PerformClick();
}
// used to make method accesable in other form
public void dontSaveClick()
{
mainTextArea.Clear();
fileChanged = false;
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.Paste();
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.Undo();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.Redo();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
mainTextArea.SelectAll();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
newToolStripButton.PerformClick();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openToolStripButton.PerformClick();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveToolStripButton.PerformClick();
}
}
}