Just starting out on C# and I keep getting stuck on this particular piece code (more precisely the lblArr[i, j].Click += pnlArr_Click; line):
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;
}
}
As I am trying to find out what particular mouse button was pressed on a label in an array, I thought this method might work:
public int pnlArr_Click(object sender, MouseEventArgs e)
The error goes away if i change MouseEventArgs to EventArgs, but then this won’t work anymore:
if (e.Button == MouseButtons.Left)
Any ideas? all help is much appreciated.
The event that receive a MouseEventArgs as second argument is not the Click event,
but the MouseDown. Change your code in this way (also, to better readability, rename the event handler in pnlArr_MouseDown)