-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
985 lines (833 loc) · 37.6 KB
/
Copy pathForm1.cs
File metadata and controls
985 lines (833 loc) · 37.6 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Drawing.Drawing2D;
namespace MyExcel
{
public partial class Form1 : Form
{
List<DataGridView> gridovi = new List<DataGridView>();
int broj_gridova = 0;
string imeFilea;
List<Celije> ListaCelija = new List<Celije>();
public Funkcije fje = new Funkcije();
public bool loading = false;
public DataGridView tGrid; // trenutno aktivni grid
Celije tCell; // trenutno aktivni skup celija
public DataGridViewCell celijaIzForme;
public bool otvorenIzbor = false;
public bool otvorenaFormula = false;
public List<DataGridViewCell> roze = new List<DataGridViewCell>();
bool smijemZagasiti = false;
MyExcel.Form2 funkcije;
public Form1()
{
InitializeComponent();
gridovi.Add(new DataGridView());
tGrid = gridovi[0];
tabControl1.KeyDown += new KeyEventHandler(delete);
tabControl1.TabPages[0].Controls.Add(gridovi[0]);
tabControl1.Selected += new TabControlEventHandler(promjenaTaba);
gridovi[0].BorderStyle = BorderStyle.None;
tabControl1.TabPages[0].BorderStyle = BorderStyle.None;
gridovi[0].CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellClick);
gridovi[0].CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellValueChanged);
gridovi[0].CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellEndEdit);
gridovi[0].SelectionChanged += new EventHandler(this.tablica_SelectionChanged);
gridovi[0].CellEnter += new DataGridViewCellEventHandler(this.tablica_CellEnter);
gridovi[0].RowsAdded += new DataGridViewRowsAddedEventHandler(tablica_RowsAdded);
Celije noviTab = new Celije();
ListaCelija.Add(noviTab);
tCell = ListaCelija[0];
gridovi[0].Dock = DockStyle.Fill;
tabControl1.TabPages[0].Text = "Sheet1";
gridovi[0].RowHeadersWidth = 60;
gridovi[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
for (int i = 65; i <= 90; i++)
{
DataGridViewColumn newCol = new DataGridViewColumn();
newCol.HeaderText = Convert.ToChar(i).ToString();
newCol.Visible = true;
newCol.Width = 100;
DataGridViewCell cell = new DataGridViewTextBoxCell();
newCol.CellTemplate = cell;
gridovi[0].Columns.Add(newCol);
}
string[] red = { };
for (int i = 1; i < 100; i++)
{
gridovi[0].Rows.Add(red);
gridovi[0].Rows[i - 1].HeaderCell.Value = i.ToString();
}
broj_gridova++;
gridovi[0].TabIndex = 0;
gridovi[0].CurrentCell = gridovi[0][0, 0];
toolStripTextBox1.Anchor = AnchorStyles.Right;
toolStripTextBox1.Dock = DockStyle.Fill;
}
public void delete(object o, KeyEventArgs e)
{
//loading = true;
if ((e.KeyValue == (int)Keys.Delete))
{
foreach (DataGridViewCell c in tGrid.SelectedCells)
{
KeyValuePair<int, int> index = new KeyValuePair<int, int>(c.RowIndex, c.ColumnIndex);
if (tCell.sveCelije.ContainsKey(index))
{
tCell.sveCelije[index].Sadrzaj = "";
tCell.sveCelije[index].Formula = "";
c.Value = "";
tablica_CellValueChanged(c, new DataGridViewCellEventArgs(c.ColumnIndex, c.RowIndex));
}
}
}
//loading = false;
}
public void promjenaTaba(object o, EventArgs e)
{
TabControl tab = (TabControl)o;
if (tab.SelectedIndex >= 0)
{
tGrid = gridovi[tab.SelectedIndex];
tCell = ListaCelija[tab.SelectedIndex];
}
}
//
private void toolStripButton2_Click(object sender, EventArgs e)
{
int indexTaba = tabControl1.SelectedIndex;
string s = "";
foreach (KeyValuePair<KeyValuePair<int, int>, Cell> c in ListaCelija[indexTaba].sveCelije)
{
s += c.Value.Sadrzaj + " ";
}
MessageBox.Show(s);
}
private void tablica_CellClick(object sender, DataGridViewCellEventArgs e)
{
//ako kliknuta celija nije prazna, ispisuje se i njen sadrzaj i formula, inace samo koordinate
//int indexTaba = tabControl1.SelectedIndex;
KeyValuePair<int, int> index = new KeyValuePair<int, int>(e.RowIndex, e.ColumnIndex);
if (tCell.sveCelije.ContainsKey(index))
{
if (tCell.sveCelije[index].Formula != "")
toolStripTextBox1.Text = tCell.sveCelije[index].Formula;
else
toolStripTextBox1.Text = tCell.sveCelije[index].Sadrzaj;
if (tCell.sveCelije[index].Formula != "")
statusLabel.Text = "Koordinate celije: (" + e.RowIndex.ToString() + ", " +
e.ColumnIndex.ToString() + "); Sadrzaj celije: " +
tCell.sveCelije[index].Sadrzaj +
"; Formula: " + tCell.sveCelije[index].Formula;
else if (tCell.sveCelije[index].Sadrzaj != "")
statusLabel.Text = "Koordinate celije: (" + e.RowIndex.ToString() + ", " +
e.ColumnIndex.ToString() + "); Sadrzaj celije: " +
tCell.sveCelije[index].Sadrzaj;
else statusLabel.Text = "Koordinate celije: (" + e.RowIndex.ToString() +
", " + e.ColumnIndex.ToString() + ")";
}
else
{
statusLabel.Text = "Koordinate celije: (" + e.RowIndex.ToString() + ", " + e.ColumnIndex.ToString() + ")";
toolStripTextBox1.Text = "";
}
}
private void tablica_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
smijemZagasiti = false;
KeyValuePair<int, int> index = new KeyValuePair<int, int>(e.RowIndex, e.ColumnIndex);
//int indexTaba = tabControl1.SelectedIndex;
if (tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
{
return;
}
//ako se radi o formuli
if (tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()[0] == '=')
{
if (!tCell.sveCelije.ContainsKey(index) && e.RowIndex != -1 && e.ColumnIndex != -1)
{
tCell.Dodaj(e.RowIndex, e.ColumnIndex);
}
//spremam podatke upisane u celiju
//string s = tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//tCell.sveCelije[index].Sadrzaj = s;
toolStripTextBox1.Text = tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//////////////////////////////////////////////////////////////toolStripButton1_Click(null, null);
if (toolStripTextBox1.Text == "")
{
return;
}
int redak, stupac;
//int indexTaba = tabControl1.SelectedIndex;
if (otvorenaFormula)
{
stupac = celijaIzForme.ColumnIndex;
redak = celijaIzForme.RowIndex;
//otvorenaFormula = false;
}
else
{
stupac = e.ColumnIndex;
redak = e.RowIndex;
}
KeyValuePair<int, int> koordinate = new KeyValuePair<int, int>(redak, stupac);
if (!tCell.sveCelije.ContainsKey(koordinate) && redak != -1 && stupac != -1)
{
tCell.Dodaj(redak, stupac);
}
Cell celija = tCell.sveCelije[koordinate];
string formula = toolStripTextBox1.Text;
if (formula.StartsWith("=") == false)
{
tCell.sveCelije[koordinate].Sadrzaj = formula;
if (otvorenaFormula)
{
celijaIzForme.Value = formula;
}
else
{
tGrid.SelectedCells[0].Value = formula;
}
return;
}
formula = formula.ToUpper();
celija.Formula = formula;
/////////////////////////////
//Console.WriteLine(tmp.Pop().ToString());
try
{
celija.evaluateFormula(tCell, fje);
}
catch
{
MessageBox.Show("Neispravna formula!");
}
//celija.sadrzaj = tmp.Pop().ToString();
if (otvorenaFormula)
{
celijaIzForme.Value = celija.Sadrzaj;
otvorenaFormula = false;
}
else
{
tGrid[stupac, redak].Value = celija.Sadrzaj;
}
toolStripTextBox1.Clear();
}
//inace, ako se radi o obicnim brojevima
else
{
//stvori novu celiju ako vec ne postoji
//KeyValuePair<int, int> index = new KeyValuePair<int, int>(e.RowIndex, e.ColumnIndex);
if (!tCell.sveCelije.ContainsKey(index) && e.RowIndex != -1 && e.ColumnIndex != -1)
{
tCell.Dodaj(e.RowIndex, e.ColumnIndex);
}
//spremam podatke upisane u celiju
string s = tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
tCell.sveCelije[index].Sadrzaj = s; //DodajVrijednost(e.RowIndex, e.ColumnIndex, s);
//tCell.sveCelije[index].Formula = "";
}
//poravnjanje brojeva i teksta
if (tCell.sveCelije[index].Numerical)
{
//KeyValuePair<int, int> index = new KeyValuePair<int, int>(e.RowIndex, e.ColumnIndex);
tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.BottomRight;
}
else
{
tGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.BottomLeft;
}
foreach (Cell c in tCell.sveCelije[index].uFormuli)
{
try {
c.evaluateFormula(tCell, fje);
tGrid.Rows[c.red].Cells[c.stupac].Value = c.Sadrzaj;
}
catch
{
MessageBox.Show("Neispravna formula!");
}
}
}
private void tablica_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
//kad se doda novi redak, ispisi redni broj u header
//int indexTaba = tabControl1.SelectedIndex;
tGrid.Rows[tGrid.RowCount - 1].HeaderCell.Value = tGrid.RowCount.ToString();
}
private void tablica_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//int indexTaba = tabControl1.SelectedIndex;
tGrid.Columns[e.ColumnIndex].HeaderCell.Style.BackColor = Color.SlateBlue;
for (int i = 0; i < tGrid.RowCount; i++)
for (int j = 0; j < tGrid.ColumnCount; j++)
if (j == e.ColumnIndex)
tGrid.Rows[i].Cells[j].Style.BackColor = Color.LightSteelBlue;
else
tGrid.Rows[i].Cells[j].Style.BackColor = Color.White;
}
private void tablica_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//int indexTaba = tabControl1.SelectedIndex;
tGrid.Rows[e.RowIndex].HeaderCell.Style.BackColor = Color.SlateBlue;
for (int i = 0; i < tGrid.ColumnCount; i++)
for (int j = 0; j < tGrid.RowCount; j++)
if (j == e.RowIndex)
tGrid.Rows[j].Cells[i].Style.BackColor = Color.LightSteelBlue;
else
tGrid.Rows[j].Cells[i].Style.BackColor = Color.White;
}
public void toolStripButton1_Click(object sender, EventArgs e) // GO! Izracunaj formulu
{
// klik na kvacicu (ili enter) nakon unosa formule u textbox
if (toolStripTextBox1.Text == "")
{
return;
}
int redak, stupac;
//int indexTaba = tabControl1.SelectedIndex;
if (otvorenaFormula)
{
stupac = celijaIzForme.ColumnIndex;
redak = celijaIzForme.RowIndex;
//otvorenaFormula = false;
}
else
{
stupac = tGrid.SelectedCells[0].ColumnIndex;
redak = tGrid.SelectedCells[0].RowIndex;
}
KeyValuePair<int, int> koordinate = new KeyValuePair<int, int>(redak, stupac);
if (!tCell.sveCelije.ContainsKey(koordinate) && redak != -1 && stupac != -1)
{
tCell.Dodaj(redak, stupac);
}
Cell celija = tCell.sveCelije[koordinate];
string formula = toolStripTextBox1.Text;
if (formula.StartsWith("=") == false)
{
tCell.sveCelije[koordinate].Sadrzaj = formula;
if (otvorenaFormula)
{
celijaIzForme.Value = formula;
}
else
{
tGrid.SelectedCells[0].Value = formula;
}
return;
}
formula = formula.ToUpper();
celija.Formula = formula;
/////////////////////////////
//Console.WriteLine(tmp.Pop().ToString());
try
{
celija.evaluateFormula(tCell, fje);
}
catch
{
MessageBox.Show("Neispravna formula!");
}
//celija.sadrzaj = tmp.Pop().ToString();
if (otvorenaFormula)
{
celijaIzForme.Value = celija.Sadrzaj;
otvorenaFormula = false;
}
else
{
tGrid.SelectedCells[0].Value = celija.Sadrzaj;
}
}
private void tablica_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewCell c in roze)
{
c.Style.BackColor = Color.White;
}
List<double> argument = new List<double>();
//int indexTaba = tabControl1.SelectedIndex;
//double rez;
for (int c = 0; c < tGrid.SelectedCells.Count; c++ )
{
KeyValuePair<int, int> index =
new KeyValuePair<int, int>(tGrid.SelectedCells[c].RowIndex,
tGrid.SelectedCells[c].ColumnIndex);
if (tCell.sveCelije.ContainsKey(index))
{
if (tCell.sveCelije[index].Numerical)
argument.Add(Double.Parse(tCell.sveCelije[index].Sadrzaj));
}
}
LabelSuma.Text = fje.SveFunkcije["sum"](argument).ToString();
if (otvorenIzbor)
{
int index = funkcije.textBox1.Text.IndexOf("(");
string arg = "";
if (index > 0)
{
foreach(DataGridViewCell c in tGrid.SelectedCells)
{
arg += Char.ConvertFromUtf32(c.ColumnIndex + 65) + (c.RowIndex + 1).ToString() + ";";
}
arg = arg.TrimEnd(';');
Match m = Regex.Match(funkcije.textBox1.Text, @"\(.*\)");
funkcije.textBox1.Text = funkcije.textBox1.Text.Replace(m.Value, "("+arg+")");
}
}
}
private void toolStripButton4_Click(object sender, EventArgs e) //novi tab
{
string s = "Sheet" + (broj_gridova + 1);
TabPage newPage = new TabPage(s);
tabControl1.TabPages.Add(newPage);
gridovi.Add(new DataGridView());
gridovi[broj_gridova].RowHeadersWidth = 60;
tabControl1.TabPages[broj_gridova].Controls.Add(gridovi[broj_gridova]);
gridovi[broj_gridova].BorderStyle = BorderStyle.None;
gridovi[broj_gridova].CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellClick);
gridovi[broj_gridova].CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellEndEdit);
gridovi[broj_gridova].CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.tablica_CellValueChanged);
gridovi[broj_gridova].SelectionChanged += new EventHandler(this.tablica_SelectionChanged);
gridovi[broj_gridova].CellEnter += new DataGridViewCellEventHandler(this.tablica_CellEnter);
gridovi[broj_gridova].RowsAdded += new DataGridViewRowsAddedEventHandler(tablica_RowsAdded);
Celije noviTab = new Celije();
ListaCelija.Add(noviTab);
gridovi[broj_gridova].Dock = DockStyle.Fill;
for (int i = 65; i <= 90; i++)
{
DataGridViewColumn newCol = new DataGridViewColumn();
newCol.HeaderText = Convert.ToChar(i).ToString();
newCol.Visible = true;
newCol.Width = 100;
DataGridViewCell cell = new DataGridViewTextBoxCell();
newCol.CellTemplate = cell;
gridovi[broj_gridova].Columns.Add(newCol);
}
string[] red = { };
for (int i = 1; i < 100; i++)
{
gridovi[broj_gridova].Rows.Add(red);
gridovi[broj_gridova].Rows[i - 1].HeaderCell.Value = i.ToString();
}
broj_gridova++;
}
private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e) //enter u textboxu
{
if (!(e.KeyChar == (char)Keys.Enter))
return;
toolStripButton1_Click(null, null);
//int indexTaba = tabControl1.SelectedIndex;
KeyValuePair<int, int> index = new KeyValuePair<int, int> (tGrid.SelectedCells[0].RowIndex, tGrid.SelectedCells[0].ColumnIndex);
statusLabel.Text = "Koordinate celije: (" + tGrid.SelectedCells[0].ColumnIndex.ToString() + ", " +
tGrid.SelectedCells[0].ColumnIndex.ToString() + "); Sadrzaj celije: " +
tCell.sveCelije[index].Sadrzaj +
"; Formula: " + tCell.sveCelije[index].Formula;
toolStripTextBox1.Text = "";
int r = tGrid.SelectedCells[0].RowIndex;
int s = tGrid.SelectedCells[0].ColumnIndex;
tGrid.ClearSelection();
tGrid.Rows[r + 1].Cells[s].Selected = true;
}
private void tablica_CellValueChanged(object sender, DataGridViewCellEventArgs e) //ne radi
{
KeyValuePair<int, int> index = new KeyValuePair<int, int>(e.RowIndex, e.ColumnIndex);
if (tCell.sveCelije.Count != 0 && tCell.sveCelije.ContainsKey(index) && !loading)
{
foreach (Cell c in tCell.sveCelije[index].uFormuli)
{
try
{
c.evaluateFormula(tCell, fje);
tGrid.Rows[c.red].Cells[c.stupac].Value = c.Sadrzaj;
if (c.Numerical)
{
tGrid.Rows[c.red].Cells[c.stupac].Style.Alignment = DataGridViewContentAlignment.BottomRight;
}
else
{
tGrid.Rows[c.red].Cells[c.stupac].Style.Alignment = DataGridViewContentAlignment.BottomLeft;
}
}
catch
{
MessageBox.Show("Neispravna formula!");
}
}
}
}
private void tablica_CellEnter(object sender, DataGridViewCellEventArgs e)
{
tablica_CellClick(null, e);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (!otvorenIzbor)
{
otvorenIzbor = true;
otvorenaFormula = true;
funkcije = new MyExcel.Form2();
funkcije.excel = this;
funkcije.Show();
}
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
//klik na Font u izborniku
//primijeni odabrano formatiranje na odabrane celije
//int indexTaba = tabControl1.SelectedIndex;
fontDialog1.ShowColor = true;
fontDialog1.Font = tGrid.SelectedCells[0].Style.Font;
fontDialog1.Color = tGrid.SelectedCells[0].Style.ForeColor;
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
for (int i = 0; i < tGrid.SelectedCells.Count; i++)
{
tGrid.SelectedCells[i].Style.Font = fontDialog1.Font;
tGrid.SelectedCells[i].Style.ForeColor = fontDialog1.Color;
}
}
}
// dovrsiti praznjenje
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
//klik na New u izborniku
loading = true;
//pitaj korisnika zeli li spremiti promjene prije izlaza iz trenutne tablice
MyExcel.Form3 izlaz = new MyExcel.Form3();
izlaz.excel = this;
DialogResult rez = izlaz.ShowDialog();
if (rez == DialogResult.Yes)
{
//napravi saveAs ili save pa izadji
if (imeFilea == null) saveAsToolStripMenuItem_Click(null, null);
else saveToolStripMenuItem_Click(null, null);
}
if (rez == DialogResult.Cancel)
{
loading = false;
return;
}
//otvori novu praznu tablicu
while (broj_gridova > 1)
{
int tmp = broj_gridova - 1;
ListaCelija.RemoveAt(tmp);
tabControl1.TabPages.RemoveAt(tmp);
//gridovi.Remove(gridovi[t.SelectedIndex]);
gridovi.RemoveAt(tmp);
broj_gridova--;
}
//isprazni tablicu prvog taba
for (int j = 0; j < gridovi[0].Rows.Count; j++)
for (int k = 0; k < gridovi[0].Columns.Count; k++)
{
gridovi[0].Rows[j].Cells[k].Value = null;
}
//toolStripButton4_Click(null, null);
ListaCelija[0].sveCelije.Clear(); //sve Cell ostaju ili nestaju?!!
//vratiti fokus na (0,0)
toolStripTextBox1.Text = "";
statusLabel.Text = "Koordinate celije: (0, 0)";
imeFilea = "";
gridovi[0].ClearSelection();
gridovi[0].CurrentCell = gridovi[0][0, 0];
loading = false;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//klik na Open u izborniku
//pitaj korisnika zeli li spremiti promjene prije izlaza iz trenutne tablice
//otvori novu praznu tablicu
newToolStripMenuItem_Click(null, null);
loading = true;
//otvori open dialog
//procitaj i prepisi tablicu iz xml-a
openFileDialog1.Filter = "Extensible Markup Language|*.xml";
broj_gridova = 1;
bool pass = false;
int red = 0, stupac = 0;
string sadrzaj = "", formula = "", ovisnosti = "";
bool numerical = false;
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
imeFilea = openFileDialog1.FileName;
//XmlTextReader reader = new XmlTextReader(imeFilea);
XmlReader reader = XmlReader.Create(imeFilea);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "grid")
{
if (pass)
{
toolStripButton4_Click(null, null);
}
pass = true;
}
if (reader.Name == "celija")
{
XmlReader cell = reader.ReadSubtree();
while (cell.Read())
{
if (cell.NodeType == XmlNodeType.Element)
{
if (cell.Name == "red")
{
red = Convert.ToInt32(cell.ReadString());
}
if (cell.Name == "stupac")
{
stupac = Convert.ToInt32(cell.ReadString());
}
if (cell.Name == "sadrzaj")
{
sadrzaj = cell.ReadString();
}
if (cell.Name == "formula")
{
formula = cell.ReadString();
}
if (cell.Name == "numerical")
{
numerical = Convert.ToBoolean(cell.ReadString());
}
if (cell.Name == "ovisnosti")
{
ovisnosti = cell.ReadString();
}
}
}
KeyValuePair<int, int> i = new KeyValuePair<int, int>(red, stupac);
if (ListaCelija[broj_gridova - 1].sveCelije.ContainsKey(i) == false)
{
ListaCelija[broj_gridova - 1].Dodaj(red, stupac);
}
ListaCelija[broj_gridova - 1].sveCelije[i].Sadrzaj = sadrzaj;
ListaCelija[broj_gridova - 1].sveCelije[i].Formula = formula;
gridovi[broj_gridova - 1].Rows[red].Cells[stupac].Value = sadrzaj;
ListaCelija[broj_gridova - 1].sveCelije[i].PostaviOvisnosti(ListaCelija[broj_gridova - 1], ovisnosti);
if (ListaCelija[broj_gridova - 1].sveCelije[i].Numerical)
{
gridovi[broj_gridova - 1].Rows[red].Cells[stupac].Style.Alignment = DataGridViewContentAlignment.BottomRight;
}
else
{
gridovi[broj_gridova - 1].Rows[red].Cells[stupac].Style.Alignment = DataGridViewContentAlignment.BottomLeft;
}
}
}
}
reader.Close();
}
loading = false;
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
//klik na Save As u izborniku
//pitaj korisnika gdje zeli spremiti tablicu
//spremi tablicu u xml
saveFileDialog1.Filter = "Extensible Markup Language|*.xml";
saveFileDialog1.Title = "Save As";
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
//ako je za ime file-a upisano nesto razlicito od praznog stringa, spremi tablicu u xml
if (saveFileDialog1.FileName != "")
{
imeFilea = saveFileDialog1.FileName;
saveToolStripMenuItem_Click(null, null);
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
//klik na Save u izborniku
//spremi tablicu u xml naziva imeFilea
if (imeFilea == null || imeFilea == "")
{
saveAsToolStripMenuItem_Click(null, null);
return;
}
XmlTextWriter xmlw = new XmlTextWriter(imeFilea, null);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("tablica");
for (int i = 0; i < broj_gridova; i++)
{
xmlw.WriteStartElement("grid");
foreach (KeyValuePair<KeyValuePair<int, int>, Cell> par in ListaCelija[i].sveCelije)
{
Cell c = par.Value;
xmlw.WriteStartElement("celija");
xmlw.WriteStartElement("red");
xmlw.WriteString(c.red.ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("stupac");
xmlw.WriteString(c.stupac.ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("sadrzaj");
xmlw.WriteString(c.Sadrzaj);
xmlw.WriteEndElement();
xmlw.WriteStartElement("formula");
xmlw.WriteString(c.Formula);
xmlw.WriteEndElement();
xmlw.WriteStartElement("numerical");
xmlw.WriteString(c.Numerical.ToString());
xmlw.WriteEndElement();
string s="";
foreach (Cell a in c.uFormuli)
{
s += a.ID+";";
}
s = s.TrimEnd(';');
xmlw.WriteStartElement("ovisnosti");
xmlw.WriteString(s);
xmlw.WriteEndElement();
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Close();
smijemZagasiti = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
// klik na Exit u izborniku
// pitaj korisnika zeli li spremiti promjene prije izlaza
// izadji iz programa
MyExcel.Form3 izlaz = new MyExcel.Form3();
izlaz.excel = this;
DialogResult rez = izlaz.ShowDialog();
if (rez == DialogResult.Yes)
{
//napravi saveAs ili save pa izadji
if (imeFilea == null)
{
saveAsToolStripMenuItem_Click(null, null);
}
else
{
saveToolStripMenuItem_Click(null, null);
}
}
else if (rez == DialogResult.No)
{
//samo izadji
smijemZagasiti = true;
Form1.ActiveForm.Close();
}
if (smijemZagasiti)
{
Form1.ActiveForm.Close();
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
bool numTest = false;
//int tab = tabControl1.SelectedIndex;
foreach (DataGridViewCell c in tGrid.SelectedCells)
{
KeyValuePair<int, int> index = new KeyValuePair<int, int>(c.RowIndex, c.ColumnIndex);
if (tCell.sveCelije.ContainsKey(index) && tCell.sveCelije[index].Numerical)
{
numTest = true;
break;
}
}
if (numTest)
{
Grafovi Slika = new Grafovi(this, tGrid, tCell);
Slika.drawHistogram();
}
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
bool numTest = false;
//int tab = tabControl1.SelectedIndex;
foreach (DataGridViewCell c in tGrid.SelectedCells)
{
KeyValuePair<int, int> index = new KeyValuePair<int, int>(c.RowIndex, c.ColumnIndex);
if (tCell.sveCelije.ContainsKey(index) && tCell.sveCelije[index].Numerical)
{
numTest = true;
break;
}
}
if (numTest)
{
Grafovi Slika = new Grafovi(this, tGrid, tCell);
Slika.drawPieChart();
}
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
bool numTest = false;
//int tab = tabControl1.SelectedIndex;
foreach (DataGridViewCell c in tGrid.SelectedCells)
{
KeyValuePair<int, int> index = new KeyValuePair<int, int>(c.RowIndex,c.ColumnIndex);
if (tCell.sveCelije.ContainsKey(index) && tCell.sveCelije[index].Numerical)
{
numTest = true;
break;
}
}
if (numTest)
{
Grafovi Slika = new Grafovi(this, tGrid, tCell);
Slika.drawLineChart();
}
}
private void toolStripTextBox1_Click(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 about = new Form5();
about.ShowDialog();
}
private void tabControl1_MouseDoubleClick(object sender, MouseEventArgs e)
{
TabControl t = (TabControl)sender;
//MessageBox.Show(t.TabPages[t.SelectedIndex].Text);
if (t.SelectedIndex != 0)
{
int tmp = t.SelectedIndex;
ListaCelija.RemoveAt(tmp);
tabControl1.TabPages.RemoveAt(tmp);
//gridovi.Remove(gridovi[t.SelectedIndex]);
gridovi.RemoveAt(tmp);
broj_gridova--;
}
//tGrid = gridovi[tabControl1.SelectedIndex];
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (smijemZagasiti == false)
{
exitToolStripMenuItem_Click(null, null);
e.Cancel = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
gridovi[0].CurrentCell = gridovi[0][0, 0];
gridovi[0].BeginEdit(true);
}
}
}