Мой код:
Код: Выделить всё
private static void addImageToPanel(imageData[] images, Panel panel)
{
Panel imagePanel;
PictureBox imageImage;
Label imageLabel;
Size imageSize = new()
{
Width = 80,
Height = 80,
};
Size imagePanelSize = new()
{
Width = 100,
Height = 100,
};
const int posX = 60;
const int posY = 140;
foreach (imageData image in images)
{
if (!panel.Controls.ContainsKey(image.key))
{
imagePanel = new()
{
Name = image.key,
Size = imagePanelSize,
Left = posX * x + margin,
Top = posY * y + margin,
Cursor = Cursors.Hand,
};
//Error here VVV toolStripClick (CS0120: An object reference is required for the non-static field, method, or property 'member')
focusPanel.Click += new EventHandler(toolStripClick);
imageLabel = new()
{
Name = image.key + "_text",
Text = image.name,
ForeColor = Color.White,
Font = new Font("Segoe UI", 8),
Dock = DockStyle.Bottom,
TextAlign = ContentAlignment.MiddleCenter,
};
imageImage = new()
{
Name = image.key + "_image",
Image = Image.FromFile("C:\\..."),
SizeMode = PictureBoxSizeMode.Zoom,
Size = fileSize,
Dock = DockStyle.Top,
};
panel.Controls.Add(imagePanel);
imagePanel.Controls.Add(imageLabel);
imagePanel.Controls.Add(imageImage);
}
}
}
Код: Выделить всё
private void toolStripClick(object sender, EventArgs e)
{
contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
}
Даже пытался сделать цикл for после добавления всех изображений, вот так:
Код: Выделить всё
for (int i = 0; i < mainPanel.Controls.Count; i++)
{
mainPanel.Controls[i].Click += new EventHandler(toolStripClick);
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... atic-panel