teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/21 08:37

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,250 +1,244 @@
1
- Zuishinさんの手順にそってざっと作ってみました。
2
- 大体のところはデザイナでできます。書いた部分はForm1.csとToDoItem.csです。
3
-
4
-
5
- ```C#
6
- // ToDoItem.cs
7
-
8
- using System;
9
-
10
- namespace Questions245208
11
- {
12
- public class ToDoItem
13
- {
14
- public bool Completed { get; set; }
15
- public DateTime Deadline { get; set; }
16
- public string Task { get; set; }
17
- }
18
- }
19
- ```
20
-
21
- ```C#
22
- // Form1.cs
23
-
24
- using System;
25
- using System.Collections.Generic;
26
- using System.IO;
27
- using System.Runtime.Serialization;
28
- using System.Windows.Forms;
29
- using System.Xml;
30
-
31
- namespace Questions245208
32
- {
33
- public partial class Form1 : Form
34
- {
35
- public Form1()
36
- {
37
- InitializeComponent();
38
-
39
- if(File.Exists("todo.xml"))
40
- {
41
- using(var fs = new FileStream("todo.xml", FileMode.Open))
42
- {
43
- var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
44
- toDoItemBindingSource.DataSource = serializer.ReadObject(fs);
45
- }
46
- }
47
- }
48
-
49
- private void Add_Click(object sender, EventArgs e)
50
- {
51
- var item = new ToDoItem
52
- {
53
- Task = textBox1.Text,
54
- Deadline = dateTimePicker1.Value,
55
- };
56
- toDoItemBindingSource.Add(item);
57
- }
58
-
59
- private void Delete_Click(object sender, EventArgs e)
60
- {
61
- //toDoBindingSource.RemoveCurrent();
62
-
63
- foreach(DataGridViewRow row in dataGridView1.SelectedRows)
64
- {
65
- if(!row.IsNewRow)
66
- {
67
- dataGridView1.Rows.Remove(row);
68
- }
69
- }
70
- }
71
-
72
- private void Save_Click(object sender, EventArgs e)
73
- {
74
- using(var xw = XmlWriter.Create("todo.xml", new XmlWriterSettings { Indent = true, }))
75
- {
76
- var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
77
- serializer.WriteObject(xw, toDoItemBindingSource.List);
78
- }
79
- }
80
- }
81
- }
82
- ```
83
-
84
- ```C#
85
- // Form1.Designer.cs
86
-
87
- namespace Questions245208
88
- {
89
- partial class Form1
90
- {
91
- /// <summary>
92
- /// 必要なデザイナー変数です。
93
- /// </summary>
94
- private System.ComponentModel.IContainer components = null;
95
-
96
- /// <summary>
97
- /// 使用中のリソースをすべてクリーンアップします。
98
- /// </summary>
99
- /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
100
- protected override void Dispose(bool disposing)
101
- {
102
- if(disposing && (components != null))
103
- {
104
- components.Dispose();
105
- }
106
- base.Dispose(disposing);
107
- }
108
-
109
- #region Windows フォーム デザイナーで生成されたコード
110
-
111
- /// <summary>
112
- /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
113
- /// コード エディターで変更しないでください。
114
- /// </summary>
115
- private void InitializeComponent()
116
- {
117
- this.components = new System.ComponentModel.Container();
118
- this.dataGridView1 = new System.Windows.Forms.DataGridView();
119
- this.Add = new System.Windows.Forms.Button();
120
- this.Delete = new System.Windows.Forms.Button();
121
- this.Save = new System.Windows.Forms.Button();
122
- this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
123
- this.textBox1 = new System.Windows.Forms.TextBox();
124
- this.completedColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
125
- this.deadlineColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
126
- this.taskColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
127
- this.toDoItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
128
- ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
129
- ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).BeginInit();
130
- this.SuspendLayout();
131
- //
132
- // dataGridView1
133
- //
134
- this.dataGridView1.AutoGenerateColumns = false;
135
- this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
136
- this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
137
- this.completedColumn,
138
- this.deadlineColumn,
139
- this.taskColumn});
140
- this.dataGridView1.DataSource = this.toDoItemBindingSource;
141
- this.dataGridView1.Location = new System.Drawing.Point(12, 34);
142
- this.dataGridView1.Name = "dataGridView1";
143
- this.dataGridView1.RowTemplate.Height = 21;
144
- this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
145
- this.dataGridView1.Size = new System.Drawing.Size(604, 404);
146
- this.dataGridView1.TabIndex = 0;
147
- //
148
- // Add
149
- //
150
- this.Add.Location = new System.Drawing.Point(622, 352);
151
- this.Add.Name = "Add";
152
- this.Add.Size = new System.Drawing.Size(80, 40);
153
- this.Add.TabIndex = 1;
154
- this.Add.Text = "登録";
155
- this.Add.UseVisualStyleBackColor = true;
156
- this.Add.Click += new System.EventHandler(this.Add_Click);
157
- //
158
- // Delete
159
- //
160
- this.Delete.Location = new System.Drawing.Point(708, 352);
161
- this.Delete.Name = "Delete";
162
- this.Delete.Size = new System.Drawing.Size(80, 40);
163
- this.Delete.TabIndex = 2;
164
- this.Delete.Text = "削除";
165
- this.Delete.UseVisualStyleBackColor = true;
166
- this.Delete.Click += new System.EventHandler(this.Delete_Click);
167
- //
168
- // Save
169
- //
170
- this.Save.Location = new System.Drawing.Point(622, 398);
171
- this.Save.Name = "Save";
172
- this.Save.Size = new System.Drawing.Size(166, 40);
173
- this.Save.TabIndex = 3;
174
- this.Save.Text = "保存";
175
- this.Save.UseVisualStyleBackColor = true;
176
- this.Save.Click += new System.EventHandler(this.Save_Click);
177
- //
178
- // dateTimePicker1
179
- //
180
- this.dateTimePicker1.Location = new System.Drawing.Point(623, 62);
181
- this.dateTimePicker1.Name = "dateTimePicker1";
182
- this.dateTimePicker1.Size = new System.Drawing.Size(166, 19);
183
- this.dateTimePicker1.TabIndex = 4;
184
- //
185
- // textBox1
186
- //
187
- this.textBox1.Location = new System.Drawing.Point(624, 105);
188
- this.textBox1.Name = "textBox1";
189
- this.textBox1.Size = new System.Drawing.Size(165, 19);
190
- this.textBox1.TabIndex = 5;
191
- //
192
- // completedColumn
193
- //
194
- this.completedColumn.DataPropertyName = "Completed";
195
- this.completedColumn.HeaderText = "完了";
196
- this.completedColumn.Name = "completedColumn";
197
- this.completedColumn.Width = 50;
198
- //
199
- // deadlineColumn
200
- //
201
- this.deadlineColumn.DataPropertyName = "Deadline";
202
- this.deadlineColumn.HeaderText = "期限";
203
- this.deadlineColumn.Name = "deadlineColumn";
204
- //
205
- // taskColumn
206
- //
207
- this.taskColumn.DataPropertyName = "Task";
208
- this.taskColumn.HeaderText = "やること";
209
- this.taskColumn.Name = "taskColumn";
210
- this.taskColumn.Width = 300;
211
- //
212
- // toDoItemBindingSource
213
- //
214
- this.toDoItemBindingSource.DataSource = typeof(Questions245208.ToDoItem);
215
- //
216
- // Form1
217
- //
218
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
219
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
220
- this.ClientSize = new System.Drawing.Size(800, 450);
221
- this.Controls.Add(this.textBox1);
222
- this.Controls.Add(this.dateTimePicker1);
223
- this.Controls.Add(this.Save);
224
- this.Controls.Add(this.Delete);
225
- this.Controls.Add(this.Add);
226
- this.Controls.Add(this.dataGridView1);
227
- this.Name = "Form1";
228
- this.Text = "Form1";
229
- ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
230
- ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).EndInit();
231
- this.ResumeLayout(false);
232
- this.PerformLayout();
233
-
234
- }
235
-
236
- #endregion
237
-
238
- private System.Windows.Forms.DataGridView dataGridView1;
239
- private System.Windows.Forms.Button Add;
240
- private System.Windows.Forms.Button Delete;
241
- private System.Windows.Forms.Button Save;
242
- private System.Windows.Forms.DateTimePicker dateTimePicker1;
243
- private System.Windows.Forms.TextBox textBox1;
244
- private System.Windows.Forms.BindingSource toDoItemBindingSource;
245
- private System.Windows.Forms.DataGridViewCheckBoxColumn completedColumn;
246
- private System.Windows.Forms.DataGridViewTextBoxColumn deadlineColumn;
247
- private System.Windows.Forms.DataGridViewTextBoxColumn taskColumn;
248
- }
249
- }
1
+ Zuishinさんの手順にそってざっと作ってみました。
2
+ 大体のところはデザイナでできます。書いた部分はForm1.csとToDoItem.csです。
3
+
4
+
5
+ ```cs:ToDoItem.cs
6
+ using System;
7
+
8
+ namespace Questions245208
9
+ {
10
+ public class ToDoItem
11
+ {
12
+ public bool Completed { get; set; }
13
+ public DateTime Deadline { get; set; }
14
+ public string Task { get; set; }
15
+ }
16
+ }
17
+ ```
18
+
19
+ ```cs:Form1.cs
20
+ using System;
21
+ using System.Collections.Generic;
22
+ using System.IO;
23
+ using System.Runtime.Serialization;
24
+ using System.Windows.Forms;
25
+ using System.Xml;
26
+
27
+ namespace Questions245208
28
+ {
29
+ public partial class Form1 : Form
30
+ {
31
+ public Form1()
32
+ {
33
+ InitializeComponent();
34
+
35
+ if(File.Exists("todo.xml"))
36
+ {
37
+ using(var fs = new FileStream("todo.xml", FileMode.Open))
38
+ {
39
+ var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
40
+ toDoItemBindingSource.DataSource = serializer.ReadObject(fs);
41
+ }
42
+ }
43
+ }
44
+
45
+ private void Add_Click(object sender, EventArgs e)
46
+ {
47
+ var item = new ToDoItem
48
+ {
49
+ Task = textBox1.Text,
50
+ Deadline = dateTimePicker1.Value,
51
+ };
52
+ toDoItemBindingSource.Add(item);
53
+ }
54
+
55
+ private void Delete_Click(object sender, EventArgs e)
56
+ {
57
+ //toDoBindingSource.RemoveCurrent();
58
+
59
+ foreach(DataGridViewRow row in dataGridView1.SelectedRows)
60
+ {
61
+ if(!row.IsNewRow)
62
+ {
63
+ dataGridView1.Rows.Remove(row);
64
+ }
65
+ }
66
+ }
67
+
68
+ private void Save_Click(object sender, EventArgs e)
69
+ {
70
+ using(var xw = XmlWriter.Create("todo.xml", new XmlWriterSettings { Indent = true, }))
71
+ {
72
+ var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
73
+ serializer.WriteObject(xw, toDoItemBindingSource.List);
74
+ }
75
+ }
76
+ }
77
+ }
78
+ ```
79
+
80
+ ```cs:Form1.Designer.cs
81
+ namespace Questions245208
82
+ {
83
+ partial class Form1
84
+ {
85
+ /// <summary>
86
+ /// 必要なデザイナー変数です。
87
+ /// </summary>
88
+ private System.ComponentModel.IContainer components = null;
89
+
90
+ /// <summary>
91
+ /// 使用中のリソースをすべてクリーンアップします。
92
+ /// </summary>
93
+ /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
94
+ protected override void Dispose(bool disposing)
95
+ {
96
+ if(disposing && (components != null))
97
+ {
98
+ components.Dispose();
99
+ }
100
+ base.Dispose(disposing);
101
+ }
102
+
103
+ #region Windows フォーム デザイナーで生成されたコード
104
+
105
+ /// <summary>
106
+ /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
107
+ /// コード エディターで変更しないでください。
108
+ /// </summary>
109
+ private void InitializeComponent()
110
+ {
111
+ this.components = new System.ComponentModel.Container();
112
+ this.dataGridView1 = new System.Windows.Forms.DataGridView();
113
+ this.Add = new System.Windows.Forms.Button();
114
+ this.Delete = new System.Windows.Forms.Button();
115
+ this.Save = new System.Windows.Forms.Button();
116
+ this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
117
+ this.textBox1 = new System.Windows.Forms.TextBox();
118
+ this.completedColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
119
+ this.deadlineColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
120
+ this.taskColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
121
+ this.toDoItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
122
+ ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
123
+ ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).BeginInit();
124
+ this.SuspendLayout();
125
+ //
126
+ // dataGridView1
127
+ //
128
+ this.dataGridView1.AutoGenerateColumns = false;
129
+ this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
130
+ this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
131
+ this.completedColumn,
132
+ this.deadlineColumn,
133
+ this.taskColumn});
134
+ this.dataGridView1.DataSource = this.toDoItemBindingSource;
135
+ this.dataGridView1.Location = new System.Drawing.Point(12, 34);
136
+ this.dataGridView1.Name = "dataGridView1";
137
+ this.dataGridView1.RowTemplate.Height = 21;
138
+ this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
139
+ this.dataGridView1.Size = new System.Drawing.Size(604, 404);
140
+ this.dataGridView1.TabIndex = 0;
141
+ //
142
+ // Add
143
+ //
144
+ this.Add.Location = new System.Drawing.Point(622, 352);
145
+ this.Add.Name = "Add";
146
+ this.Add.Size = new System.Drawing.Size(80, 40);
147
+ this.Add.TabIndex = 1;
148
+ this.Add.Text = "登録";
149
+ this.Add.UseVisualStyleBackColor = true;
150
+ this.Add.Click += new System.EventHandler(this.Add_Click);
151
+ //
152
+ // Delete
153
+ //
154
+ this.Delete.Location = new System.Drawing.Point(708, 352);
155
+ this.Delete.Name = "Delete";
156
+ this.Delete.Size = new System.Drawing.Size(80, 40);
157
+ this.Delete.TabIndex = 2;
158
+ this.Delete.Text = "削除";
159
+ this.Delete.UseVisualStyleBackColor = true;
160
+ this.Delete.Click += new System.EventHandler(this.Delete_Click);
161
+ //
162
+ // Save
163
+ //
164
+ this.Save.Location = new System.Drawing.Point(622, 398);
165
+ this.Save.Name = "Save";
166
+ this.Save.Size = new System.Drawing.Size(166, 40);
167
+ this.Save.TabIndex = 3;
168
+ this.Save.Text = "保存";
169
+ this.Save.UseVisualStyleBackColor = true;
170
+ this.Save.Click += new System.EventHandler(this.Save_Click);
171
+ //
172
+ // dateTimePicker1
173
+ //
174
+ this.dateTimePicker1.Location = new System.Drawing.Point(623, 62);
175
+ this.dateTimePicker1.Name = "dateTimePicker1";
176
+ this.dateTimePicker1.Size = new System.Drawing.Size(166, 19);
177
+ this.dateTimePicker1.TabIndex = 4;
178
+ //
179
+ // textBox1
180
+ //
181
+ this.textBox1.Location = new System.Drawing.Point(624, 105);
182
+ this.textBox1.Name = "textBox1";
183
+ this.textBox1.Size = new System.Drawing.Size(165, 19);
184
+ this.textBox1.TabIndex = 5;
185
+ //
186
+ // completedColumn
187
+ //
188
+ this.completedColumn.DataPropertyName = "Completed";
189
+ this.completedColumn.HeaderText = "完了";
190
+ this.completedColumn.Name = "completedColumn";
191
+ this.completedColumn.Width = 50;
192
+ //
193
+ // deadlineColumn
194
+ //
195
+ this.deadlineColumn.DataPropertyName = "Deadline";
196
+ this.deadlineColumn.HeaderText = "期限";
197
+ this.deadlineColumn.Name = "deadlineColumn";
198
+ //
199
+ // taskColumn
200
+ //
201
+ this.taskColumn.DataPropertyName = "Task";
202
+ this.taskColumn.HeaderText = "やること";
203
+ this.taskColumn.Name = "taskColumn";
204
+ this.taskColumn.Width = 300;
205
+ //
206
+ // toDoItemBindingSource
207
+ //
208
+ this.toDoItemBindingSource.DataSource = typeof(Questions245208.ToDoItem);
209
+ //
210
+ // Form1
211
+ //
212
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
213
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
214
+ this.ClientSize = new System.Drawing.Size(800, 450);
215
+ this.Controls.Add(this.textBox1);
216
+ this.Controls.Add(this.dateTimePicker1);
217
+ this.Controls.Add(this.Save);
218
+ this.Controls.Add(this.Delete);
219
+ this.Controls.Add(this.Add);
220
+ this.Controls.Add(this.dataGridView1);
221
+ this.Name = "Form1";
222
+ this.Text = "Form1";
223
+ ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
224
+ ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).EndInit();
225
+ this.ResumeLayout(false);
226
+ this.PerformLayout();
227
+
228
+ }
229
+
230
+ #endregion
231
+
232
+ private System.Windows.Forms.DataGridView dataGridView1;
233
+ private System.Windows.Forms.Button Add;
234
+ private System.Windows.Forms.Button Delete;
235
+ private System.Windows.Forms.Button Save;
236
+ private System.Windows.Forms.DateTimePicker dateTimePicker1;
237
+ private System.Windows.Forms.TextBox textBox1;
238
+ private System.Windows.Forms.BindingSource toDoItemBindingSource;
239
+ private System.Windows.Forms.DataGridViewCheckBoxColumn completedColumn;
240
+ private System.Windows.Forms.DataGridViewTextBoxColumn deadlineColumn;
241
+ private System.Windows.Forms.DataGridViewTextBoxColumn taskColumn;
242
+ }
243
+ }
250
244
  ```