回答編集履歴

2

メソッド化

2020/09/27 08:30

投稿

TN8001
TN8001

score8281

test CHANGED
@@ -84,7 +84,7 @@
84
84
 
85
85
  {
86
86
 
87
- // group配下のボタンを不活性
87
+ // Panel配下のボタンを不活性
88
88
 
89
89
  foreach(var control in group.Controls)
90
90
 
@@ -98,29 +98,47 @@
98
98
 
99
99
 
100
100
 
101
+ // SubFormのクローズを購読
102
+
101
- subForm.FormClosed += Closed;
103
+ subForm.FormClosed += SubForm_FormClosed;
104
+
105
+ // PanelをTagに入れて覚えておく
106
+
107
+ subForm.Tag = group;
102
108
 
103
109
  subForm.Show();
104
110
 
105
-
111
+ }
106
-
112
+
113
+
114
+
107
- // クローズ時にgroup配下のボタンを活性に戻す
115
+ // クローズ時にPanel配下のボタンを戻す
108
-
116
+
109
- void Closed(object sender, FormClosedEventArgs e)
117
+ private static void SubForm_FormClosed(object sender, FormClosedEventArgs e)
118
+
119
+ {
120
+
121
+ // senderはSubForm自身
122
+
123
+ if(sender is Form subForm)
110
124
 
111
125
  {
112
126
 
127
+ // PanelをTagから取り出す
128
+
113
- subForm.FormClosed -= Closed;
129
+ if(subForm.Tag is Panel group)
114
-
115
-
116
-
117
- foreach(var control in group.Controls)
118
130
 
119
131
  {
120
132
 
133
+ foreach(var control in group.Controls)
134
+
135
+ {
136
+
121
- if(control is Button button)
137
+ if(control is Button button)
122
-
138
+
123
- button.Enabled = true;
139
+ button.Enabled = true;
140
+
141
+ }
124
142
 
125
143
  }
126
144
 

1

コード追記

2020/09/27 08:30

投稿

TN8001
TN8001

score8281

test CHANGED
@@ -1,3 +1,457 @@
1
1
  `Application.Run(Form1.GetInstance());`
2
2
 
3
3
  こうしてください。
4
+
5
+
6
+
7
+ ---
8
+
9
+
10
+
11
+ 今の状況というか問題点は、
12
+
13
+ 1. メインフォームとサブフォーム1-13は既にできていて、動作自体に問題はない
14
+
15
+ 2. サブフォームを排他的(どれか一つだけしか開けないよう)に改造しようとしている
16
+
17
+ 3. その際地道にやれば(コメントアウトのコード)できることはわかっている
18
+
19
+ 4. しかし13個はだるいし、もっといい方法がありそうに思える
20
+
21
+
22
+
23
+ ってことですよね?(実際2個目くらいでもうだるかったです^^;
24
+
25
+
26
+
27
+ 自分だったらこうするかなぁ?ってコードです。
28
+
29
+ `button1_Click`もまとめてもよさそうですが、行数も減りそうにないのでそのまま。
30
+
31
+
32
+
33
+ Form1.cs
34
+
35
+ ```C#
36
+
37
+ using System;
38
+
39
+ using System.Drawing;
40
+
41
+ using System.Windows.Forms;
42
+
43
+
44
+
45
+ namespace Questions293390
46
+
47
+ {
48
+
49
+ public partial class Form1 : Form
50
+
51
+ {
52
+
53
+ // シングルトンの必要はないかもしれないが、どうせSubFormからForm1をいじりたくなりますよね?
54
+
55
+ public static Form1 Instance { get; } = new Form1();
56
+
57
+
58
+
59
+ private Form1() => InitializeComponent();
60
+
61
+
62
+
63
+ private void button1_Click(object sender, EventArgs e)
64
+
65
+ => ShowSubForm(new SubForm1(), panel1);
66
+
67
+
68
+
69
+ private void button2_Click(object sender, EventArgs e)
70
+
71
+ => ShowSubForm(new SubForm2(), panel1);
72
+
73
+
74
+
75
+ private void button3_Click(object sender, EventArgs e)
76
+
77
+ => ShowSubForm(new SubForm3(), panel1);
78
+
79
+
80
+
81
+ // class Commonにあっても別に構わない
82
+
83
+ private static void ShowSubForm(Form subForm, Panel group)
84
+
85
+ {
86
+
87
+ // group配下のボタンを不活性
88
+
89
+ foreach(var control in group.Controls)
90
+
91
+ {
92
+
93
+ if(control is Button button)
94
+
95
+ button.Enabled = false;
96
+
97
+ }
98
+
99
+
100
+
101
+ subForm.FormClosed += Closed;
102
+
103
+ subForm.Show();
104
+
105
+
106
+
107
+ // クローズ時にgroup配下のボタンを活性に戻す
108
+
109
+ void Closed(object sender, FormClosedEventArgs e)
110
+
111
+ {
112
+
113
+ subForm.FormClosed -= Closed;
114
+
115
+
116
+
117
+ foreach(var control in group.Controls)
118
+
119
+ {
120
+
121
+ if(control is Button button)
122
+
123
+ button.Enabled = true;
124
+
125
+ }
126
+
127
+ }
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ // SubFormではFormClosed等のイベントを書く必要がなくて楽々という意味
136
+
137
+ // 実際は普通に作る
138
+
139
+ public class SubForm1 : Form
140
+
141
+ {
142
+
143
+ public SubForm1() { Text = "SubForm1"; ClientSize = new Size(300, 200); }
144
+
145
+ }
146
+
147
+ public class SubForm2 : Form
148
+
149
+ {
150
+
151
+ public SubForm2() { Text = "SubForm2"; ClientSize = new Size(300, 200); }
152
+
153
+ }
154
+
155
+ public class SubForm3 : Form
156
+
157
+ {
158
+
159
+ public SubForm3() { Text = "SubForm3"; ClientSize = new Size(300, 200); }
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```
166
+
167
+
168
+
169
+ Form1.Designer.cs
170
+
171
+ ```C#
172
+
173
+ namespace Questions293390
174
+
175
+ {
176
+
177
+ partial class Form1
178
+
179
+ {
180
+
181
+ /// <summary>
182
+
183
+ /// 必要なデザイナー変数です。
184
+
185
+ /// </summary>
186
+
187
+ private System.ComponentModel.IContainer components = null;
188
+
189
+
190
+
191
+ /// <summary>
192
+
193
+ /// 使用中のリソースをすべてクリーンアップします。
194
+
195
+ /// </summary>
196
+
197
+ /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
198
+
199
+ protected override void Dispose(bool disposing)
200
+
201
+ {
202
+
203
+ if(disposing && (components != null))
204
+
205
+ {
206
+
207
+ components.Dispose();
208
+
209
+ }
210
+
211
+ base.Dispose(disposing);
212
+
213
+ }
214
+
215
+
216
+
217
+ #region Windows フォーム デザイナーで生成されたコード
218
+
219
+
220
+
221
+ /// <summary>
222
+
223
+ /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
224
+
225
+ /// コード エディターで変更しないでください。
226
+
227
+ /// </summary>
228
+
229
+ private void InitializeComponent()
230
+
231
+ {
232
+
233
+ this.button1 = new System.Windows.Forms.Button();
234
+
235
+ this.button2 = new System.Windows.Forms.Button();
236
+
237
+ this.button3 = new System.Windows.Forms.Button();
238
+
239
+ this.panel1 = new System.Windows.Forms.Panel();
240
+
241
+ this.otherButton = new System.Windows.Forms.Button();
242
+
243
+ this.label1 = new System.Windows.Forms.Label();
244
+
245
+ this.panel1.SuspendLayout();
246
+
247
+ this.SuspendLayout();
248
+
249
+ //
250
+
251
+ // button1
252
+
253
+ //
254
+
255
+ this.button1.Location = new System.Drawing.Point(5, 34);
256
+
257
+ this.button1.Name = "button1";
258
+
259
+ this.button1.Size = new System.Drawing.Size(75, 23);
260
+
261
+ this.button1.TabIndex = 0;
262
+
263
+ this.button1.Text = "button1";
264
+
265
+ this.button1.UseVisualStyleBackColor = true;
266
+
267
+ this.button1.Click += new System.EventHandler(this.button1_Click);
268
+
269
+ //
270
+
271
+ // button2
272
+
273
+ //
274
+
275
+ this.button2.Location = new System.Drawing.Point(5, 63);
276
+
277
+ this.button2.Name = "button2";
278
+
279
+ this.button2.Size = new System.Drawing.Size(75, 23);
280
+
281
+ this.button2.TabIndex = 1;
282
+
283
+ this.button2.Text = "button2";
284
+
285
+ this.button2.UseVisualStyleBackColor = true;
286
+
287
+ this.button2.Click += new System.EventHandler(this.button2_Click);
288
+
289
+ //
290
+
291
+ // button3
292
+
293
+ //
294
+
295
+ this.button3.Location = new System.Drawing.Point(5, 92);
296
+
297
+ this.button3.Name = "button3";
298
+
299
+ this.button3.Size = new System.Drawing.Size(75, 23);
300
+
301
+ this.button3.TabIndex = 2;
302
+
303
+ this.button3.Text = "button3";
304
+
305
+ this.button3.UseVisualStyleBackColor = true;
306
+
307
+ this.button3.Click += new System.EventHandler(this.button3_Click);
308
+
309
+ //
310
+
311
+ // panel1
312
+
313
+ //
314
+
315
+ this.panel1.Controls.Add(this.label1);
316
+
317
+ this.panel1.Controls.Add(this.button3);
318
+
319
+ this.panel1.Controls.Add(this.button1);
320
+
321
+ this.panel1.Controls.Add(this.button2);
322
+
323
+ this.panel1.Location = new System.Drawing.Point(153, 136);
324
+
325
+ this.panel1.Name = "panel1";
326
+
327
+ this.panel1.Size = new System.Drawing.Size(112, 125);
328
+
329
+ this.panel1.TabIndex = 3;
330
+
331
+ //
332
+
333
+ // otherButton
334
+
335
+ //
336
+
337
+ this.otherButton.Location = new System.Drawing.Point(538, 54);
338
+
339
+ this.otherButton.Name = "otherButton";
340
+
341
+ this.otherButton.Size = new System.Drawing.Size(75, 23);
342
+
343
+ this.otherButton.TabIndex = 4;
344
+
345
+ this.otherButton.Text = "otherButton";
346
+
347
+ this.otherButton.UseVisualStyleBackColor = true;
348
+
349
+ //
350
+
351
+ // label1
352
+
353
+ //
354
+
355
+ this.label1.AutoSize = true;
356
+
357
+ this.label1.Location = new System.Drawing.Point(3, 10);
358
+
359
+ this.label1.Name = "label1";
360
+
361
+ this.label1.Size = new System.Drawing.Size(35, 12);
362
+
363
+ this.label1.TabIndex = 3;
364
+
365
+ this.label1.Text = "label1";
366
+
367
+ //
368
+
369
+ // Form1
370
+
371
+ //
372
+
373
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
374
+
375
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
376
+
377
+ this.ClientSize = new System.Drawing.Size(800, 450);
378
+
379
+ this.Controls.Add(this.otherButton);
380
+
381
+ this.Controls.Add(this.panel1);
382
+
383
+ this.Name = "Form1";
384
+
385
+ this.Text = "Form1";
386
+
387
+ this.panel1.ResumeLayout(false);
388
+
389
+ this.panel1.PerformLayout();
390
+
391
+ this.ResumeLayout(false);
392
+
393
+
394
+
395
+ }
396
+
397
+
398
+
399
+ #endregion
400
+
401
+ private System.Windows.Forms.Label label1;
402
+
403
+ private System.Windows.Forms.Button otherButton;
404
+
405
+ private System.Windows.Forms.Button button1;
406
+
407
+ private System.Windows.Forms.Button button2;
408
+
409
+ private System.Windows.Forms.Button button3;
410
+
411
+ private System.Windows.Forms.Panel panel1;
412
+
413
+ }
414
+
415
+ }
416
+
417
+ ```
418
+
419
+
420
+
421
+ Program.cs
422
+
423
+ ```C#
424
+
425
+ using System;
426
+
427
+ using System.Windows.Forms;
428
+
429
+
430
+
431
+ namespace Questions293390
432
+
433
+ {
434
+
435
+ static class Program
436
+
437
+ {
438
+
439
+ [STAThread]
440
+
441
+ static void Main()
442
+
443
+ {
444
+
445
+ Application.EnableVisualStyles();
446
+
447
+ Application.SetCompatibleTextRenderingDefault(false);
448
+
449
+ Application.Run(Form1.Instance);
450
+
451
+ }
452
+
453
+ }
454
+
455
+ }
456
+
457
+ ```