刚从 C# 开始,我一直被这个特定的代码(更准确地说是lblArr[i, j].Click += pnlArr_Click;
行)卡住:
public void CreateLabelArray(int height, int width, int nrofShips)
{
pnlBase.Controls.Clear();
lblArr = new Label[height, width];
int xpos = 0;
int ypos = 0;
for (int j = 0; j < width; j++)
{
int column = j + 1;
for (int i = 0; i < height; i++)
{
Coordinaat pos = new Coordinaat();
pos.X = j;
pos.Y = i;
lblArr[i, j] = new Label();
lblArr[i, j].Left = xpos;
lblArr[i, j].Top = ypos;
lblArr[i, j].Width = 35;
lblArr[i, j].Height = 35;
lblArr[i, j].Tag = pos;
lblArr[i, j].Click += pnlArr_Click;
lblArr[i, j].BackColor = System.Drawing.Color.LightBlue;
lblArr[i, j].BorderStyle = BorderStyle.FixedSingle;
pnlBase.Controls.Add(lblArr[i, j]);
xpos += 0;
ypos += lblArr[i, j].Height;
}
xpos += 35;
ypos = 0;
}
}
当我试图找出在数组中的标签上按下了哪个特定的鼠标按钮时,我认为这种方法可能有效:
public int pnlArr_Click(object sender, MouseEventArgs e)
如果我将 MouseEventArgs 更改为 EventArgs,错误就会消失,但这将不再起作用:
if (e.Button == MouseButtons.Left)
有任何想法吗?非常感谢所有帮助。