回答編集履歴
3
修正
test
CHANGED
@@ -146,7 +146,7 @@
|
|
146
146
|
|
147
147
|
tableLayoutPanel.Controls.Remove(label);
|
148
148
|
|
149
|
-
label.Dispose
|
149
|
+
label.Dispose();
|
150
150
|
|
151
151
|
}
|
152
152
|
|
2
修正
test
CHANGED
@@ -146,6 +146,8 @@
|
|
146
146
|
|
147
147
|
tableLayoutPanel.Controls.Remove(label);
|
148
148
|
|
149
|
+
label.Disposed();
|
150
|
+
|
149
151
|
}
|
150
152
|
|
151
153
|
}
|
1
追記
test
CHANGED
@@ -3,3 +3,153 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[TableLayoutPanelコントロールを使って、コントロールを表形式で整列させる - .NET Tips (VB.NET,C#...)](https://dobon.net/vb/dotnet/control/tlbeginning.html)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
# 追記
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
サンプルです。デザイナで作る説明をするのが面倒だったので全部コードで行っていますが、実際にはデザイナを使って作ってください。
|
14
|
+
|
15
|
+
ボタンを押すとラベルが増え、ラベルをクリックするとそのラベルは消えます。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
```C#
|
20
|
+
|
21
|
+
using System;
|
22
|
+
|
23
|
+
using System.Windows.Forms;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
namespace WindowsFormsApp1
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
public partial class Form1 : Form
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
private TableLayoutPanel tableLayoutPanel;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
public Form1()
|
40
|
+
|
41
|
+
{
|
42
|
+
|
43
|
+
InitializeComponent();
|
44
|
+
|
45
|
+
var clientPanel = new Panel()
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
Dock = DockStyle.Fill,
|
50
|
+
|
51
|
+
AutoScroll = true,
|
52
|
+
|
53
|
+
Parent = this,
|
54
|
+
|
55
|
+
};
|
56
|
+
|
57
|
+
tableLayoutPanel = new TableLayoutPanel()
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
Dock = DockStyle.Top,
|
62
|
+
|
63
|
+
AutoSize = true,
|
64
|
+
|
65
|
+
ColumnCount = 5,
|
66
|
+
|
67
|
+
RowCount = 0,
|
68
|
+
|
69
|
+
GrowStyle = TableLayoutPanelGrowStyle.AddRows,
|
70
|
+
|
71
|
+
Parent = clientPanel,
|
72
|
+
|
73
|
+
};
|
74
|
+
|
75
|
+
for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / tableLayoutPanel.ColumnCount));
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
var controlPanel = new Panel()
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
Dock = DockStyle.Top,
|
88
|
+
|
89
|
+
AutoSize = true,
|
90
|
+
|
91
|
+
Parent = this,
|
92
|
+
|
93
|
+
};
|
94
|
+
|
95
|
+
var button = new Button()
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
Text = "Click",
|
100
|
+
|
101
|
+
Width = 96,
|
102
|
+
|
103
|
+
Height = 21,
|
104
|
+
|
105
|
+
Parent = controlPanel
|
106
|
+
|
107
|
+
};
|
108
|
+
|
109
|
+
button.Click += Button_Click;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
private int labelCount = 1;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
private void Button_Click(object sender, EventArgs e)
|
120
|
+
|
121
|
+
{
|
122
|
+
|
123
|
+
var label = new Label()
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
Text = $"Label{labelCount}",
|
128
|
+
|
129
|
+
Parent = tableLayoutPanel,
|
130
|
+
|
131
|
+
};
|
132
|
+
|
133
|
+
label.Click += Label_Click;
|
134
|
+
|
135
|
+
labelCount++;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
private void Label_Click(object sender, EventArgs e)
|
142
|
+
|
143
|
+
{
|
144
|
+
|
145
|
+
if (!(sender is Label label)) return;
|
146
|
+
|
147
|
+
tableLayoutPanel.Controls.Remove(label);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
```
|