質問編集履歴

1

解決策を追記

2020/01/15 10:08

投稿

TAKASE_Hiroyuki
TAKASE_Hiroyuki

スコア21

test CHANGED
File without changes
test CHANGED
@@ -165,3 +165,133 @@
165
165
 
166
166
 
167
167
  お手数ですが、御教授いただければ幸いです。
168
+
169
+
170
+
171
+ #解決方法
172
+
173
+ mmaeda様から教えていただいた方法で、解決しました。
174
+
175
+ その後、できるだけプログラムを簡素化しようと試みて、以下のようにしてみました。
176
+
177
+ このままコピペすると、ちゃんと動くと思います。
178
+
179
+ ```C#
180
+
181
+ using System.Drawing;
182
+
183
+ using System.Windows.Forms;
184
+
185
+
186
+
187
+ namespace mytest
188
+
189
+ {
190
+
191
+ public partial class Form1 : Form
192
+
193
+ {
194
+
195
+ public static Panel PL;
196
+
197
+
198
+
199
+ public Form1()
200
+
201
+ {
202
+
203
+ InitializeComponent();
204
+
205
+ PL = new Panel();
206
+
207
+ Controls.Add(PL);
208
+
209
+ PL.Size = new Size(800, 600);
210
+
211
+
212
+
213
+ new Test(60);
214
+
215
+ new Test(120);
216
+
217
+ new Test(180);
218
+
219
+ new Test(240);
220
+
221
+ }
222
+
223
+
224
+
225
+ public static void MyMove(PictureBox pic, Label LB)
226
+
227
+ {
228
+
229
+ pic.BackColor = System.Drawing.Color.Red;
230
+
231
+ LB.BackColor = System.Drawing.Color.Yellow;
232
+
233
+ }
234
+
235
+
236
+
237
+ public static void MyLeave(PictureBox pic, Label LB)
238
+
239
+ {
240
+
241
+ pic.BackColor = Color.FromArgb(40, 0, 0, 255);
242
+
243
+ LB.BackColor = Color.FromArgb(40, 255, 0, 0);
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+ public class Test
252
+
253
+ {
254
+
255
+ public PictureBox Pic;
256
+
257
+ public Label LB;
258
+
259
+
260
+
261
+ public Test(int y)
262
+
263
+ {
264
+
265
+ LB = new Label();
266
+
267
+ Form1.PL.Controls.Add(LB);
268
+
269
+ LB.Size = new Size(100, 50);
270
+
271
+ LB.Location = new Point(50, y);
272
+
273
+ LB.BackColor = Color.FromArgb(40, 255, 0, 0);
274
+
275
+
276
+
277
+ Pic = new PictureBox();
278
+
279
+ Form1.PL.Controls.Add(Pic);
280
+
281
+ Pic.Size = new Size(100, 50);
282
+
283
+ Pic.BackColor = Color.FromArgb(40, 0, 0, 255);
284
+
285
+ Pic.Location = new Point(200, y);
286
+
287
+ Pic.MouseMove += (s, e) => Form1.MyMove(Pic, LB);
288
+
289
+ Pic.MouseLeave += (s, e) => Form1.MyLeave(Pic, LB);
290
+
291
+ }
292
+
293
+ }
294
+
295
+ }
296
+
297
+ ```