回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,165 +1,83 @@
|
|
1
1
|
> コンボボックスをパネルに追加する際、定数にパネル名を入れ、それを引数として受け取り、追加するパネルを判別したいのですがうまくいきません。
|
2
|
-
|
3
|
-
|
4
2
|
|
5
3
|
やりたいこととコードがあっていないように感じます。
|
6
4
|
|
7
|
-
|
8
|
-
|
9
5
|
コンボボックスを`panel1`・`panel2`どちらに入れるか選びたいということだと思いますが、`panel1`・`panel2`は先にできていないとおかしくないですか?
|
10
6
|
|
11
|
-
|
12
|
-
|
13
7
|
radianさんのように`Find`でもいいし、`Form`の子だとわかっているならインデクサも使えます。
|
14
|
-
|
15
8
|
[Control.ControlCollection.Find(String, Boolean) メソッド (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.control.controlcollection.find)
|
16
|
-
|
17
|
-
|
18
9
|
|
19
10
|
[Control.ControlCollection.Item[] プロパティ (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.control.controlcollection.item#System_Windows_Forms_Control_ControlCollection_Item_System_String_)
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
```
|
12
|
+
```cs
|
24
|
-
|
25
13
|
using System;
|
26
|
-
|
27
14
|
using System.Drawing;
|
28
|
-
|
29
15
|
using System.Windows.Forms;
|
30
16
|
|
31
|
-
|
32
|
-
|
33
17
|
namespace Questions360326
|
34
|
-
|
35
18
|
{
|
36
|
-
|
37
19
|
class FormMain : Form
|
38
|
-
|
39
20
|
{
|
40
|
-
|
41
21
|
private const string p1 = "panel1";
|
42
|
-
|
43
22
|
private const string p2 = "panel2";
|
44
23
|
|
45
|
-
|
46
|
-
|
47
24
|
[STAThread]
|
48
|
-
|
49
25
|
public static void Main()
|
50
|
-
|
51
26
|
{
|
52
|
-
|
53
27
|
Application.EnableVisualStyles();
|
54
|
-
|
55
28
|
Application.SetCompatibleTextRenderingDefault(false);
|
56
|
-
|
57
29
|
Application.Run(new FormMain());
|
58
|
-
|
59
30
|
}
|
60
31
|
|
61
|
-
|
62
|
-
|
63
32
|
FormMain()
|
64
|
-
|
65
33
|
{
|
66
|
-
|
67
34
|
Text = "Button Click Sample";
|
68
|
-
|
69
35
|
ClientSize = new Size(600, 400);
|
70
36
|
|
71
|
-
|
72
|
-
|
73
37
|
var btn1 = new Button { Location = new Point(50, 50), Text = "Add ComboBox", };
|
74
|
-
|
75
38
|
btn1.Click += btn1_Click;
|
76
39
|
|
77
|
-
|
78
|
-
|
79
40
|
var btn2 = new Button { Location = new Point(350, 50), Text = "Add ComboBox", };
|
80
|
-
|
81
41
|
btn2.Click += btn2_Click;
|
82
42
|
|
83
|
-
|
84
|
-
|
85
43
|
var panel1 = new Panel
|
86
|
-
|
87
44
|
{
|
88
|
-
|
89
45
|
Name = p1,
|
90
|
-
|
91
46
|
Location = new Point(56, 72),
|
92
|
-
|
93
47
|
Size = new Size(264, 152),
|
94
|
-
|
95
48
|
BorderStyle = BorderStyle.Fixed3D,
|
96
|
-
|
97
49
|
};
|
98
50
|
|
99
|
-
|
100
|
-
|
101
51
|
var panel2 = new Panel
|
102
|
-
|
103
52
|
{
|
104
|
-
|
105
53
|
Name = p2,
|
106
|
-
|
107
54
|
Location = new Point(356, 72),
|
108
|
-
|
109
55
|
Size = new Size(264, 152),
|
110
|
-
|
111
56
|
BorderStyle = BorderStyle.Fixed3D,
|
112
|
-
|
113
57
|
};
|
114
58
|
|
115
|
-
|
116
|
-
|
117
59
|
Controls.AddRange(new Control[] { btn1, btn2, panel1, panel2, });
|
118
|
-
|
119
60
|
}
|
120
61
|
|
121
|
-
|
122
|
-
|
123
62
|
private void btn1_Click(object sender, EventArgs e)
|
124
|
-
|
125
63
|
{
|
126
|
-
|
127
64
|
AddComboBox(p1);
|
128
|
-
|
129
65
|
((Button)sender).Visible = false;
|
130
|
-
|
66
|
+
}
|
67
|
+
private void btn2_Click(object sender, EventArgs e)
|
68
|
+
{
|
69
|
+
AddComboBox(p2);
|
70
|
+
((Button)sender).Visible = false;
|
131
71
|
}
|
132
72
|
|
133
|
-
private void
|
73
|
+
private void AddComboBox(string pname)
|
134
|
-
|
135
74
|
{
|
136
|
-
|
137
|
-
AddComboBox(p2);
|
138
|
-
|
139
|
-
((Button)sender).Visible = false;
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
private void AddComboBox(string pname)
|
146
|
-
|
147
|
-
{
|
148
|
-
|
149
75
|
var comboBox = new ComboBox();
|
150
|
-
|
151
76
|
comboBox.Items.AddRange(new string[] { "A", "B", "C", "D", "E", });
|
152
77
|
|
153
|
-
|
154
|
-
|
155
78
|
var panel = Controls[pname] as Panel; // インデクサ
|
156
|
-
|
157
79
|
panel?.Controls.Add(comboBox);
|
158
|
-
|
159
80
|
}
|
160
|
-
|
161
81
|
}
|
162
|
-
|
163
82
|
}
|
164
|
-
|
165
83
|
```
|