/
Form1.cs
180 lines (161 loc) · 6.86 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace PoorExcel
{
public partial class Form1 : Form
{
Table table = new Table();
public Form1()
{
InitializeComponent();
InitializeDataGridView(Table.defaultRow,Table.defaultCol);
}
private void InitializeDataGridView(int rows,int columns)
{
dataGridView1.ColumnHeadersVisible = true;
dataGridView1.RowHeadersVisible = true;
dataGridView1.ColumnCount = columns;
for(int i = 0; i < columns; i++)
{
string columnName = NumberConverter.To26System(i);
dataGridView1.Columns[i].Name = columnName;
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
for(int i = 0; i < rows; i++)
{
dataGridView1.Rows.Add("");
dataGridView1.Rows[i].HeaderCell.Value = (i).ToString();
}
dataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
table.setTable(columns, rows);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int col = dataGridView1.SelectedCells[0].ColumnIndex;
int row = dataGridView1.SelectedCells[0].RowIndex;
string expression = Table.grid[row][col].expression;
string value = Table.grid[row][col].value;
textBox1.Text = expression;
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
int col = dataGridView1.SelectedCells[0].ColumnIndex;
int row = dataGridView1.SelectedCells[0].RowIndex;
string expression = textBox1.Text;
if (expression == "") return;
table.ChangeCellWithAllPointers(row, col, expression, dataGridView1);
dataGridView1[col, row].Value = Table.grid[row][col].value;
}
private void buttonAddRow_Click(object sender, EventArgs e)
{
DataGridViewRow row = new System.Windows.Forms.DataGridViewRow();
if (dataGridView1.Columns.Count == 0)
{
MessageBox.Show("Нема стовбців!");
return;
}
dataGridView1.Rows.Add(row);
dataGridView1.Rows[table.rowCount].HeaderCell.Value = (table.rowCount).ToString();
table.AddRow(dataGridView1);
}
private void buttonDeleteRow_Click(object sender, EventArgs e)
{
if (!table.DeleteRow(dataGridView1))
return;
dataGridView1.Rows.RemoveAt(table.rowCount);
}
private void buttonAddColumn_Click(object sender, EventArgs e)
{
string name = NumberConverter.To26System(table.colCount);
dataGridView1.Columns.Add(name, name);
table.AddColumn(dataGridView1);
}
private void buttonDeleteColumn_Click(object sender, EventArgs e)
{
if (!table.DeleteColumn(dataGridView1))
return;
dataGridView1.Columns.RemoveAt(table.colCount);
}
private void buttonOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "TableFile|*.txt";
openFileDialog.Title = "Open TableFile";
if (openFileDialog.ShowDialog() != DialogResult.OK)
return;
StreamReader sr = new StreamReader(openFileDialog.FileName);
table.Clear();
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
int row;
int column;
Int32.TryParse(sr.ReadLine(), out row);
Int32.TryParse(sr.ReadLine(), out column);
InitializeDataGridView(row, column);
table.Open(row, column, sr, dataGridView1);
sr.Close();
}
private void buttonSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "TableFile|*.txt";
saveFileDialog.Title = "save table file";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
FileStream fs = (FileStream)saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(fs);
table.Save(sw);
sw.Close();
fs.Close();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Зберегти таблицю перед виходом?", "Message", MessageBoxButtons.YesNoCancel);
if (res==DialogResult.Yes)
{
e.Cancel = false;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "TableFile|*.txt";
saveFileDialog.Title = "save table file";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
FileStream fs = (FileStream)saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(fs);
table.Save(sw);
sw.Close();
fs.Close();
}
}
if (res == DialogResult.No)
{
e.Cancel = false;
}
if (res == DialogResult.Cancel)
{
e.Cancel = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
const string helpMess = "Це програма аналог Microsoft Excel.\n" +
"Щоб все правильно рахувалось потрібно вписувати вираз в ділянку зверху\n" +
"Вираз має бути в дужках або починатися з пробілу \n" +
"Доступні такі операції +,-,/,*,>,<,=,mmax(),mmin(),not()";
MessageBox.Show(helpMess, "Допомога", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}