Мой код:
Код: Выделить всё
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')
imagePanel.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);
}
Редактировать:
Неважно, я только что обнаружил, что удаление статики из «addImageToPanel» исправляет это, частично потому, что оно не работает на панели, но работает на метке...
Есть ли способ заставить это работать на панели? Или есть обходной путь?
Edit2:
Заменено «focusPanel» на «imagePanel». (Моя ошибка при копировании. Это была и есть imagePanel, а не focusPanel.)
Подробнее здесь: https://stackoverflow.com/questions/790 ... atic-panel
Мобильная версия