質問編集履歴
1
解決策を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -81,4 +81,69 @@
|
|
81
81
|
|
82
82
|
ということをやりたいのですが、MouseOnPic の引数の sender から 同じ class 内の LB にたどりつく方法がわかりません。
|
83
83
|
|
84
|
-
お手数ですが、御教授いただければ幸いです。
|
84
|
+
お手数ですが、御教授いただければ幸いです。
|
85
|
+
|
86
|
+
#解決方法
|
87
|
+
mmaeda様から教えていただいた方法で、解決しました。
|
88
|
+
その後、できるだけプログラムを簡素化しようと試みて、以下のようにしてみました。
|
89
|
+
このままコピペすると、ちゃんと動くと思います。
|
90
|
+
```C#
|
91
|
+
using System.Drawing;
|
92
|
+
using System.Windows.Forms;
|
93
|
+
|
94
|
+
namespace mytest
|
95
|
+
{
|
96
|
+
public partial class Form1 : Form
|
97
|
+
{
|
98
|
+
public static Panel PL;
|
99
|
+
|
100
|
+
public Form1()
|
101
|
+
{
|
102
|
+
InitializeComponent();
|
103
|
+
PL = new Panel();
|
104
|
+
Controls.Add(PL);
|
105
|
+
PL.Size = new Size(800, 600);
|
106
|
+
|
107
|
+
new Test(60);
|
108
|
+
new Test(120);
|
109
|
+
new Test(180);
|
110
|
+
new Test(240);
|
111
|
+
}
|
112
|
+
|
113
|
+
public static void MyMove(PictureBox pic, Label LB)
|
114
|
+
{
|
115
|
+
pic.BackColor = System.Drawing.Color.Red;
|
116
|
+
LB.BackColor = System.Drawing.Color.Yellow;
|
117
|
+
}
|
118
|
+
|
119
|
+
public static void MyLeave(PictureBox pic, Label LB)
|
120
|
+
{
|
121
|
+
pic.BackColor = Color.FromArgb(40, 0, 0, 255);
|
122
|
+
LB.BackColor = Color.FromArgb(40, 255, 0, 0);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
public class Test
|
127
|
+
{
|
128
|
+
public PictureBox Pic;
|
129
|
+
public Label LB;
|
130
|
+
|
131
|
+
public Test(int y)
|
132
|
+
{
|
133
|
+
LB = new Label();
|
134
|
+
Form1.PL.Controls.Add(LB);
|
135
|
+
LB.Size = new Size(100, 50);
|
136
|
+
LB.Location = new Point(50, y);
|
137
|
+
LB.BackColor = Color.FromArgb(40, 255, 0, 0);
|
138
|
+
|
139
|
+
Pic = new PictureBox();
|
140
|
+
Form1.PL.Controls.Add(Pic);
|
141
|
+
Pic.Size = new Size(100, 50);
|
142
|
+
Pic.BackColor = Color.FromArgb(40, 0, 0, 255);
|
143
|
+
Pic.Location = new Point(200, y);
|
144
|
+
Pic.MouseMove += (s, e) => Form1.MyMove(Pic, LB);
|
145
|
+
Pic.MouseLeave += (s, e) => Form1.MyLeave(Pic, LB);
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
```
|