回答編集履歴

2

見直しキャンペーン中

2023/08/09 10:42

投稿

TN8001
TN8001

スコア9893

test CHANGED
@@ -39,7 +39,7 @@
39
39
  btnClose.Location = new Point(100, 10);
40
40
  Controls.Add(btnClose);
41
41
 
42
- foreach(Button b in Controls.Cast<Control>().Where(x => x is Button))
42
+ foreach(Button b in Controls.OfType<Button>())
43
43
  {
44
44
  b.Size = new Size(100, 20);
45
45
  }

1

見直しキャンペーン中

2023/07/17 06:30

投稿

TN8001
TN8001

スコア9893

test CHANGED
@@ -1,137 +1,69 @@
1
1
  各ボタンは配列等で管理せず名前を付けておきたいってことなんだと解釈し、3パターン考えましたがどれもパッとしませんね。
2
2
 
3
-
4
-
5
3
  案1 一時的に配列に入れてforeach
6
-
7
- ```C#
4
+ ```cs
8
-
9
5
  Button btnStart = new Button();
10
-
11
6
  btnStart.Text = "スタート";
12
-
13
7
  btnStart.Location = new Point(10, 10);
14
8
 
9
+ Button btnNext = new Button();
10
+ btnNext.Text = "次へ";
11
+ btnNext.Location = new Point(50, 50);
15
12
 
13
+ Button btnClose = new Button();
14
+ btnClose.Text = "閉じる";
15
+ btnClose.Location = new Point(100, 10);
16
+
17
+ Button[] buttons = { btnStart, btnNext, btnClose };
18
+ foreach(Button b in buttons)
19
+ {
20
+ b.Size = new Size(100, 20);
21
+ Controls.Add(b);
22
+ }
23
+ ```
24
+
25
+ 案2 Controlsから探してforeach
26
+ ```cs
27
+ Button btnStart = new Button();
28
+ btnStart.Text = "スタート";
29
+ btnStart.Location = new Point(10, 10);
30
+ Controls.Add(btnStart);
16
31
 
17
32
  Button btnNext = new Button();
18
-
19
33
  btnNext.Text = "次へ";
20
-
21
34
  btnNext.Location = new Point(50, 50);
22
-
35
+ Controls.Add(btnNext);
23
-
24
36
 
25
37
  Button btnClose = new Button();
38
+ btnClose.Text = "閉じる";
39
+ btnClose.Location = new Point(100, 10);
40
+ Controls.Add(btnClose);
26
41
 
27
- btnClose.Text = "閉じる";
28
-
29
- btnClose.Location = new Point(100, 10);
30
-
31
-
32
-
33
- Button[] buttons = { btnStart, btnNext, btnClose };
42
+ foreach(Button b in Controls.Cast<Control>().Where(x => x is Button))
34
-
35
- foreach(Button b in buttons)
36
-
37
43
  {
38
-
39
44
  b.Size = new Size(100, 20);
40
-
41
- Controls.Add(b);
42
-
43
45
  }
44
-
45
46
  ```
46
47
 
47
-
48
-
49
- Controlsから探してforeach
48
+ 継承
50
-
51
- ```C#
49
+ ```cs
52
-
53
- Button btnStart = new Button();
54
-
55
- btnStart.Text = "スタート";
56
-
57
- btnStart.Location = new Point(10, 10);
50
+ Button btnStart = new MyButton("スタート", new Point(10, 10));
58
-
59
51
  Controls.Add(btnStart);
60
52
 
61
-
62
-
63
- Button btnNext = new Button();
64
-
65
- btnNext.Text = "次へ";
66
-
67
- btnNext.Location = new Point(50, 50);
53
+ Button btnNext = new MyButton("次へ", new Point(50, 50));
68
-
69
54
  Controls.Add(btnNext);
70
55
 
71
-
72
-
73
- Button btnClose = new Button();
74
-
75
- btnClose.Text = "閉じる";
76
-
77
- btnClose.Location = new Point(100, 10);
56
+ Button btnClose = new MyButton("閉じる", new Point(100, 10));
78
-
79
57
  Controls.Add(btnClose);
80
58
 
81
59
 
82
-
83
- foreach(Button b in Controls.Cast<Control>().Where(x => x is Button))
60
+ class MyButton : Button
84
-
85
61
  {
86
-
62
+ public MyButton(string text, Point location)
63
+ {
64
+ Text = text;
65
+ Location = location;
87
- b.Size = new Size(100, 20);
66
+ Size = new Size(100, 20);
88
-
67
+ }
89
68
  }
90
-
91
69
  ```
92
-
93
-
94
-
95
- 案3 継承
96
-
97
- ```C#
98
-
99
- Button btnStart = new MyButton("スタート", new Point(10, 10));
100
-
101
- Controls.Add(btnStart);
102
-
103
-
104
-
105
- Button btnNext = new MyButton("次へ", new Point(50, 50));
106
-
107
- Controls.Add(btnNext);
108
-
109
-
110
-
111
- Button btnClose = new MyButton("閉じる", new Point(100, 10));
112
-
113
- Controls.Add(btnClose);
114
-
115
-
116
-
117
-
118
-
119
- class MyButton : Button
120
-
121
- {
122
-
123
- public MyButton(string text, Point location)
124
-
125
- {
126
-
127
- Text = text;
128
-
129
- Location = location;
130
-
131
- Size = new Size(100, 20);
132
-
133
- }
134
-
135
- }
136
-
137
- ```