-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasksForm.cs
159 lines (144 loc) · 5.37 KB
/
TasksForm.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
using System.Drawing;
using System.Windows.Forms;
namespace TodoApplication
{
public class TasksForm : Form
{
private ListModel<string> model;
private ListBox TasksList;
private Button buttonRemove;
private Button buttonUndo;
private Button buttonAdd;
private TextBox textBox;
public TasksForm()
{
model = new ListModel<string>(20);
model.AddItem("Составить список дел на сегодня");
model.AddItem("Домашка по C#");
model.AddItem("Решить задачу 1519");
this.Text = "Список дел на сегодня";
this.Font = new Font("Serif", 11);
this.Size = new Size(400, 300);
TasksList = new ListBox
{
Dock = DockStyle.Fill
};
TasksList.Items.AddRange(model.Items.ToArray());
var label = new Label
{
Text = "Введите новое задание",
Dock = DockStyle.Fill
};
textBox = new TextBox
{
Dock = DockStyle.Fill,
};
buttonAdd = new Button
{
Text = "Добавить",
Dock = DockStyle.Fill
};
buttonUndo = new Button
{
Text = "Отменить",
Dock = DockStyle.Fill,
Enabled = model.CanUndo()
};
buttonRemove = new Button
{
Text = "Удалить",
Dock = DockStyle.Fill,
Enabled = false
};
var table = new TableLayoutPanel
{
Padding = new Padding(5, 10, 5, 10)
};
table.RowStyles.Clear();
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 35));
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 35));
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 33));
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
table.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 75));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
table.Controls.Add(new Panel(), 3, 3);
table.Controls.Add(TasksList, 1, 3);
table.Controls.Add(label, 1, 0);
table.Controls.Add(textBox, 1, 1);
table.Controls.Add(buttonAdd, 2, 1);
var interTable = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0)
};
interTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
interTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
interTable.Controls.Add(buttonUndo, 1, 0);
interTable.Controls.Add(buttonRemove, 0, 0);
table.Controls.Add(interTable, 1, 2);
table.LayoutSettings.SetColumnSpan(interTable, 2);
table.Dock = DockStyle.Fill;
Controls.Add(table);
textBox.KeyDown += (sender, args) =>
{
if (args.KeyCode == Keys.Enter)
{
AddTask();
args.Handled = true;
args.SuppressKeyPress = true;
}
};
buttonAdd.Click += (sender, args) => AddTask();
buttonUndo.Click += (sender, args) =>
{
if (model.CanUndo())
{
model.Undo();
TasksList.Items.Clear();
TasksList.Items.AddRange(model.Items.ToArray());
}
if (!model.CanUndo())
{
buttonUndo.Enabled = false;
}
};
buttonRemove.Click += (sender, args) =>
{
var index = TasksList.SelectedIndex;
if (index != -1)
{
model.RemoveItem(index);
TasksList.Items.Clear();
TasksList.Items.AddRange(model.Items.ToArray());
buttonRemove.Enabled = false;
if (model.CanUndo())
{
buttonUndo.Enabled = true;
}
}
};
TasksList.SelectedIndexChanged += (sender, args) =>
{
buttonRemove.Enabled = TasksList.SelectedIndex != -1;
};
}
private void AddTask()
{
model.AddItem(textBox.Text);
textBox.Text = "";
TasksList.Items.Clear();
TasksList.Items.AddRange(model.Items.ToArray());
if (model.CanUndo())
{
buttonUndo.Enabled = true;
}
}
}
}