回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,83 +1,83 @@
|
|
1
|
-
> コンボボックスをパネルに追加する際、定数にパネル名を入れ、それを引数として受け取り、追加するパネルを判別したいのですがうまくいきません。
|
2
|
-
|
3
|
-
やりたいこととコードがあっていないように感じます。
|
4
|
-
|
5
|
-
コンボボックスを`panel1`・`panel2`どちらに入れるか選びたいということだと思いますが、`panel1`・`panel2`は先にできていないとおかしくないですか?
|
6
|
-
|
7
|
-
radianさんのように`Find`でもいいし、`Form`の子だとわかっているならインデクサも使えます。
|
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)
|
9
|
-
|
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_)
|
11
|
-
|
12
|
-
```
|
13
|
-
using System;
|
14
|
-
using System.Drawing;
|
15
|
-
using System.Windows.Forms;
|
16
|
-
|
17
|
-
namespace Questions360326
|
18
|
-
{
|
19
|
-
class FormMain : Form
|
20
|
-
{
|
21
|
-
private const string p1 = "panel1";
|
22
|
-
private const string p2 = "panel2";
|
23
|
-
|
24
|
-
[STAThread]
|
25
|
-
public static void Main()
|
26
|
-
{
|
27
|
-
Application.EnableVisualStyles();
|
28
|
-
Application.SetCompatibleTextRenderingDefault(false);
|
29
|
-
Application.Run(new FormMain());
|
30
|
-
}
|
31
|
-
|
32
|
-
FormMain()
|
33
|
-
{
|
34
|
-
Text = "Button Click Sample";
|
35
|
-
ClientSize = new Size(600, 400);
|
36
|
-
|
37
|
-
var btn1 = new Button { Location = new Point(50, 50), Text = "Add ComboBox", };
|
38
|
-
btn1.Click += btn1_Click;
|
39
|
-
|
40
|
-
var btn2 = new Button { Location = new Point(350, 50), Text = "Add ComboBox", };
|
41
|
-
btn2.Click += btn2_Click;
|
42
|
-
|
43
|
-
var panel1 = new Panel
|
44
|
-
{
|
45
|
-
Name = p1,
|
46
|
-
Location = new Point(56, 72),
|
47
|
-
Size = new Size(264, 152),
|
48
|
-
BorderStyle = BorderStyle.Fixed3D,
|
49
|
-
};
|
50
|
-
|
51
|
-
var panel2 = new Panel
|
52
|
-
{
|
53
|
-
Name = p2,
|
54
|
-
Location = new Point(356, 72),
|
55
|
-
Size = new Size(264, 152),
|
56
|
-
BorderStyle = BorderStyle.Fixed3D,
|
57
|
-
};
|
58
|
-
|
59
|
-
Controls.AddRange(new Control[] { btn1, btn2, panel1, panel2, });
|
60
|
-
}
|
61
|
-
|
62
|
-
private void btn1_Click(object sender, EventArgs e)
|
63
|
-
{
|
64
|
-
AddComboBox(p1);
|
65
|
-
((Button)sender).Visible = false;
|
66
|
-
}
|
67
|
-
private void btn2_Click(object sender, EventArgs e)
|
68
|
-
{
|
69
|
-
AddComboBox(p2);
|
70
|
-
((Button)sender).Visible = false;
|
71
|
-
}
|
72
|
-
|
73
|
-
private void AddComboBox(string pname)
|
74
|
-
{
|
75
|
-
var comboBox = new ComboBox();
|
76
|
-
comboBox.Items.AddRange(new string[] { "A", "B", "C", "D", "E", });
|
77
|
-
|
78
|
-
var panel = Controls[pname] as Panel; // インデクサ
|
79
|
-
panel?.Controls.Add(comboBox);
|
80
|
-
}
|
81
|
-
}
|
82
|
-
}
|
1
|
+
> コンボボックスをパネルに追加する際、定数にパネル名を入れ、それを引数として受け取り、追加するパネルを判別したいのですがうまくいきません。
|
2
|
+
|
3
|
+
やりたいこととコードがあっていないように感じます。
|
4
|
+
|
5
|
+
コンボボックスを`panel1`・`panel2`どちらに入れるか選びたいということだと思いますが、`panel1`・`panel2`は先にできていないとおかしくないですか?
|
6
|
+
|
7
|
+
radianさんのように`Find`でもいいし、`Form`の子だとわかっているならインデクサも使えます。
|
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)
|
9
|
+
|
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_)
|
11
|
+
|
12
|
+
```cs
|
13
|
+
using System;
|
14
|
+
using System.Drawing;
|
15
|
+
using System.Windows.Forms;
|
16
|
+
|
17
|
+
namespace Questions360326
|
18
|
+
{
|
19
|
+
class FormMain : Form
|
20
|
+
{
|
21
|
+
private const string p1 = "panel1";
|
22
|
+
private const string p2 = "panel2";
|
23
|
+
|
24
|
+
[STAThread]
|
25
|
+
public static void Main()
|
26
|
+
{
|
27
|
+
Application.EnableVisualStyles();
|
28
|
+
Application.SetCompatibleTextRenderingDefault(false);
|
29
|
+
Application.Run(new FormMain());
|
30
|
+
}
|
31
|
+
|
32
|
+
FormMain()
|
33
|
+
{
|
34
|
+
Text = "Button Click Sample";
|
35
|
+
ClientSize = new Size(600, 400);
|
36
|
+
|
37
|
+
var btn1 = new Button { Location = new Point(50, 50), Text = "Add ComboBox", };
|
38
|
+
btn1.Click += btn1_Click;
|
39
|
+
|
40
|
+
var btn2 = new Button { Location = new Point(350, 50), Text = "Add ComboBox", };
|
41
|
+
btn2.Click += btn2_Click;
|
42
|
+
|
43
|
+
var panel1 = new Panel
|
44
|
+
{
|
45
|
+
Name = p1,
|
46
|
+
Location = new Point(56, 72),
|
47
|
+
Size = new Size(264, 152),
|
48
|
+
BorderStyle = BorderStyle.Fixed3D,
|
49
|
+
};
|
50
|
+
|
51
|
+
var panel2 = new Panel
|
52
|
+
{
|
53
|
+
Name = p2,
|
54
|
+
Location = new Point(356, 72),
|
55
|
+
Size = new Size(264, 152),
|
56
|
+
BorderStyle = BorderStyle.Fixed3D,
|
57
|
+
};
|
58
|
+
|
59
|
+
Controls.AddRange(new Control[] { btn1, btn2, panel1, panel2, });
|
60
|
+
}
|
61
|
+
|
62
|
+
private void btn1_Click(object sender, EventArgs e)
|
63
|
+
{
|
64
|
+
AddComboBox(p1);
|
65
|
+
((Button)sender).Visible = false;
|
66
|
+
}
|
67
|
+
private void btn2_Click(object sender, EventArgs e)
|
68
|
+
{
|
69
|
+
AddComboBox(p2);
|
70
|
+
((Button)sender).Visible = false;
|
71
|
+
}
|
72
|
+
|
73
|
+
private void AddComboBox(string pname)
|
74
|
+
{
|
75
|
+
var comboBox = new ComboBox();
|
76
|
+
comboBox.Items.AddRange(new string[] { "A", "B", "C", "D", "E", });
|
77
|
+
|
78
|
+
var panel = Controls[pname] as Panel; // インデクサ
|
79
|
+
panel?.Controls.Add(comboBox);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
83
|
```
|