回答編集履歴

1

見直しキャンペーン中

2023/07/21 08:37

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,499 +1,244 @@
1
1
  Zuishinさんの手順にそってざっと作ってみました。
2
-
3
2
  大体のところはデザイナでできます。書いた部分はForm1.csとToDoItem.csです。
4
3
 
5
4
 
6
-
7
-
8
-
9
- ```C#
10
-
11
- // ToDoItem.cs
5
+ ```cs:ToDoItem.cs
12
-
13
-
14
-
15
6
  using System;
16
7
 
17
-
18
-
19
8
  namespace Questions245208
20
-
21
9
  {
22
-
23
10
  public class ToDoItem
24
-
25
11
  {
26
-
27
12
  public bool Completed { get; set; }
28
-
29
13
  public DateTime Deadline { get; set; }
30
-
31
14
  public string Task { get; set; }
32
-
33
15
  }
34
-
35
16
  }
36
-
37
17
  ```
38
18
 
39
-
40
-
41
- ```C#
42
-
43
- // Form1.cs
19
+ ```cs:Form1.cs
44
-
45
-
46
-
47
20
  using System;
48
-
49
21
  using System.Collections.Generic;
50
-
51
22
  using System.IO;
52
-
53
23
  using System.Runtime.Serialization;
54
-
55
24
  using System.Windows.Forms;
56
-
57
25
  using System.Xml;
58
26
 
59
-
60
-
61
27
  namespace Questions245208
62
-
63
28
  {
64
-
65
29
  public partial class Form1 : Form
66
-
67
30
  {
68
-
69
31
  public Form1()
70
-
71
- {
32
+ {
72
-
73
33
  InitializeComponent();
74
34
 
75
-
76
-
77
35
  if(File.Exists("todo.xml"))
78
-
79
- {
36
+ {
80
-
81
37
  using(var fs = new FileStream("todo.xml", FileMode.Open))
82
-
83
38
  {
84
-
85
39
  var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
86
-
87
40
  toDoItemBindingSource.DataSource = serializer.ReadObject(fs);
88
-
89
41
  }
90
-
91
- }
42
+ }
92
-
93
- }
43
+ }
94
-
95
-
96
44
 
97
45
  private void Add_Click(object sender, EventArgs e)
98
-
99
- {
46
+ {
100
-
101
47
  var item = new ToDoItem
102
-
103
- {
48
+ {
104
-
105
49
  Task = textBox1.Text,
106
-
107
50
  Deadline = dateTimePicker1.Value,
108
-
109
51
  };
110
-
111
52
  toDoItemBindingSource.Add(item);
112
-
113
- }
53
+ }
114
-
115
-
116
54
 
117
55
  private void Delete_Click(object sender, EventArgs e)
118
-
119
- {
56
+ {
120
-
121
57
  //toDoBindingSource.RemoveCurrent();
122
58
 
123
-
124
-
125
59
  foreach(DataGridViewRow row in dataGridView1.SelectedRows)
126
-
127
- {
60
+ {
128
-
129
61
  if(!row.IsNewRow)
130
-
131
62
  {
132
-
133
63
  dataGridView1.Rows.Remove(row);
134
-
135
64
  }
136
-
137
- }
65
+ }
138
-
139
- }
66
+ }
140
-
141
-
142
67
 
143
68
  private void Save_Click(object sender, EventArgs e)
144
-
145
- {
69
+ {
146
-
147
70
  using(var xw = XmlWriter.Create("todo.xml", new XmlWriterSettings { Indent = true, }))
148
-
149
- {
71
+ {
150
-
151
72
  var serializer = new DataContractSerializer(typeof(List<ToDoItem>));
152
-
153
73
  serializer.WriteObject(xw, toDoItemBindingSource.List);
154
-
155
- }
74
+ }
156
-
157
- }
75
+ }
158
-
159
76
  }
160
-
161
77
  }
162
-
163
78
  ```
164
79
 
165
-
166
-
167
- ```C#
168
-
169
- // Form1.Designer.cs
80
+ ```cs:Form1.Designer.cs
170
-
171
-
172
-
173
81
  namespace Questions245208
174
-
175
82
  {
176
-
177
83
  partial class Form1
178
-
179
84
  {
180
-
181
85
  /// <summary>
182
-
183
86
  /// 必要なデザイナー変数です。
184
-
185
87
  /// </summary>
186
-
187
88
  private System.ComponentModel.IContainer components = null;
188
89
 
189
-
190
-
191
90
  /// <summary>
192
-
193
91
  /// 使用中のリソースをすべてクリーンアップします。
194
-
195
92
  /// </summary>
196
-
197
93
  /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
198
-
199
94
  protected override void Dispose(bool disposing)
200
-
201
- {
95
+ {
202
-
203
96
  if(disposing && (components != null))
204
-
205
- {
97
+ {
206
-
207
98
  components.Dispose();
208
-
209
- }
99
+ }
210
-
211
100
  base.Dispose(disposing);
212
-
213
- }
101
+ }
214
-
215
-
216
102
 
217
103
  #region Windows フォーム デザイナーで生成されたコード
218
104
 
219
-
220
-
221
105
  /// <summary>
222
-
223
106
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
224
-
225
107
  /// コード エディターで変更しないでください。
226
-
227
108
  /// </summary>
228
-
229
109
  private void InitializeComponent()
230
-
231
- {
110
+ {
232
-
233
111
  this.components = new System.ComponentModel.Container();
234
-
235
112
  this.dataGridView1 = new System.Windows.Forms.DataGridView();
236
-
237
113
  this.Add = new System.Windows.Forms.Button();
238
-
239
114
  this.Delete = new System.Windows.Forms.Button();
240
-
241
115
  this.Save = new System.Windows.Forms.Button();
242
-
243
116
  this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
244
-
245
117
  this.textBox1 = new System.Windows.Forms.TextBox();
246
-
247
118
  this.completedColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
248
-
249
119
  this.deadlineColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
250
-
251
120
  this.taskColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
252
-
253
121
  this.toDoItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
254
-
255
122
  ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
256
-
257
123
  ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).BeginInit();
258
-
259
124
  this.SuspendLayout();
260
-
261
- //
125
+ //
262
-
263
126
  // dataGridView1
264
-
265
- //
127
+ //
266
-
267
128
  this.dataGridView1.AutoGenerateColumns = false;
268
-
269
129
  this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
270
-
271
130
  this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
272
-
273
131
  this.completedColumn,
274
-
275
132
  this.deadlineColumn,
276
-
277
133
  this.taskColumn});
278
-
279
134
  this.dataGridView1.DataSource = this.toDoItemBindingSource;
280
-
281
135
  this.dataGridView1.Location = new System.Drawing.Point(12, 34);
282
-
283
136
  this.dataGridView1.Name = "dataGridView1";
284
-
285
137
  this.dataGridView1.RowTemplate.Height = 21;
286
-
287
138
  this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
288
-
289
139
  this.dataGridView1.Size = new System.Drawing.Size(604, 404);
290
-
291
140
  this.dataGridView1.TabIndex = 0;
292
-
293
- //
141
+ //
294
-
295
142
  // Add
296
-
297
- //
143
+ //
298
-
299
144
  this.Add.Location = new System.Drawing.Point(622, 352);
300
-
301
145
  this.Add.Name = "Add";
302
-
303
146
  this.Add.Size = new System.Drawing.Size(80, 40);
304
-
305
147
  this.Add.TabIndex = 1;
306
-
307
148
  this.Add.Text = "登録";
308
-
309
149
  this.Add.UseVisualStyleBackColor = true;
310
-
311
150
  this.Add.Click += new System.EventHandler(this.Add_Click);
312
-
313
- //
151
+ //
314
-
315
152
  // Delete
316
-
317
- //
153
+ //
318
-
319
154
  this.Delete.Location = new System.Drawing.Point(708, 352);
320
-
321
155
  this.Delete.Name = "Delete";
322
-
323
156
  this.Delete.Size = new System.Drawing.Size(80, 40);
324
-
325
157
  this.Delete.TabIndex = 2;
326
-
327
158
  this.Delete.Text = "削除";
328
-
329
159
  this.Delete.UseVisualStyleBackColor = true;
330
-
331
160
  this.Delete.Click += new System.EventHandler(this.Delete_Click);
332
-
333
- //
161
+ //
334
-
335
162
  // Save
336
-
337
- //
163
+ //
338
-
339
164
  this.Save.Location = new System.Drawing.Point(622, 398);
340
-
341
165
  this.Save.Name = "Save";
342
-
343
166
  this.Save.Size = new System.Drawing.Size(166, 40);
344
-
345
167
  this.Save.TabIndex = 3;
346
-
347
168
  this.Save.Text = "保存";
348
-
349
169
  this.Save.UseVisualStyleBackColor = true;
350
-
351
170
  this.Save.Click += new System.EventHandler(this.Save_Click);
352
-
353
- //
171
+ //
354
-
355
172
  // dateTimePicker1
356
-
357
- //
173
+ //
358
-
359
174
  this.dateTimePicker1.Location = new System.Drawing.Point(623, 62);
360
-
361
175
  this.dateTimePicker1.Name = "dateTimePicker1";
362
-
363
176
  this.dateTimePicker1.Size = new System.Drawing.Size(166, 19);
364
-
365
177
  this.dateTimePicker1.TabIndex = 4;
366
-
367
- //
178
+ //
368
-
369
179
  // textBox1
370
-
371
- //
180
+ //
372
-
373
181
  this.textBox1.Location = new System.Drawing.Point(624, 105);
374
-
375
182
  this.textBox1.Name = "textBox1";
376
-
377
183
  this.textBox1.Size = new System.Drawing.Size(165, 19);
378
-
379
184
  this.textBox1.TabIndex = 5;
380
-
381
- //
185
+ //
382
-
383
186
  // completedColumn
384
-
385
- //
187
+ //
386
-
387
188
  this.completedColumn.DataPropertyName = "Completed";
388
-
389
189
  this.completedColumn.HeaderText = "完了";
390
-
391
190
  this.completedColumn.Name = "completedColumn";
392
-
393
191
  this.completedColumn.Width = 50;
394
-
395
- //
192
+ //
396
-
397
193
  // deadlineColumn
398
-
399
- //
194
+ //
400
-
401
195
  this.deadlineColumn.DataPropertyName = "Deadline";
402
-
403
196
  this.deadlineColumn.HeaderText = "期限";
404
-
405
197
  this.deadlineColumn.Name = "deadlineColumn";
406
-
407
- //
198
+ //
408
-
409
199
  // taskColumn
410
-
411
- //
200
+ //
412
-
413
201
  this.taskColumn.DataPropertyName = "Task";
414
-
415
202
  this.taskColumn.HeaderText = "やること";
416
-
417
203
  this.taskColumn.Name = "taskColumn";
418
-
419
204
  this.taskColumn.Width = 300;
420
-
421
- //
205
+ //
422
-
423
206
  // toDoItemBindingSource
424
-
425
- //
207
+ //
426
-
427
208
  this.toDoItemBindingSource.DataSource = typeof(Questions245208.ToDoItem);
428
-
429
- //
209
+ //
430
-
431
210
  // Form1
432
-
433
- //
211
+ //
434
-
435
212
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
436
-
437
213
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
438
-
439
214
  this.ClientSize = new System.Drawing.Size(800, 450);
440
-
441
215
  this.Controls.Add(this.textBox1);
442
-
443
216
  this.Controls.Add(this.dateTimePicker1);
444
-
445
217
  this.Controls.Add(this.Save);
446
-
447
218
  this.Controls.Add(this.Delete);
448
-
449
219
  this.Controls.Add(this.Add);
450
-
451
220
  this.Controls.Add(this.dataGridView1);
452
-
453
221
  this.Name = "Form1";
454
-
455
222
  this.Text = "Form1";
456
-
457
223
  ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
458
-
459
224
  ((System.ComponentModel.ISupportInitialize)(this.toDoItemBindingSource)).EndInit();
460
-
461
225
  this.ResumeLayout(false);
462
-
463
226
  this.PerformLayout();
464
227
 
465
-
466
-
467
- }
228
+ }
468
-
469
-
470
229
 
471
230
  #endregion
472
231
 
473
-
474
-
475
232
  private System.Windows.Forms.DataGridView dataGridView1;
476
-
477
233
  private System.Windows.Forms.Button Add;
478
-
479
234
  private System.Windows.Forms.Button Delete;
480
-
481
235
  private System.Windows.Forms.Button Save;
482
-
483
236
  private System.Windows.Forms.DateTimePicker dateTimePicker1;
484
-
485
237
  private System.Windows.Forms.TextBox textBox1;
486
-
487
238
  private System.Windows.Forms.BindingSource toDoItemBindingSource;
488
-
489
239
  private System.Windows.Forms.DataGridViewCheckBoxColumn completedColumn;
490
-
491
240
  private System.Windows.Forms.DataGridViewTextBoxColumn deadlineColumn;
492
-
493
241
  private System.Windows.Forms.DataGridViewTextBoxColumn taskColumn;
494
-
495
242
  }
496
-
497
243
  }
498
-
499
244
  ```