I'm trying to create around 25 buttons of a class ButtonControlswhich inherit from normal Buttonclass with nested for loops to create a square. I added special properties called coordsX and coordsY I use to know where they should be placed in a form and for some other use that's too complicated to explain here. When I create a button it's placed where it should be and write it's "coords" as it's text and it shows the right values, but when I click it, the messagebox should pop up and give me the coords of this button, but it always shows the coords of the last button created.
private void CreateBoard()
{
for (int x=0;x<BoardWidth;x++)
{
for (int y=0;y<BoardHeight;y++)
{
ButtonControls bc = new ButtonControls(x,y,PieceSize);
//bc.Name = x+"_"+y;
//bc.BackgroundImage = PieceBmp;
//bc.Size = new Size(PieceSize,PieceSize);
bc.Click += PieceButton_Click;
Point docks = new Point(20, 20);
docks.Offset(ButtonControls.coordsX, ButtonControls.coordsY);
bc.Location = docks;
Controls.Add(bc);
}
}
}
The constructor at use looks like this:
public ButtonControls (int X,int Y, int ButtonSize)
{
coordsX = X*ButtonSize;
coordsY = Y*ButtonSize;
Name = X+"."+Y;
BackColor = Color.White;
Size = new Size(ButtonSize,ButtonSize);
Font = new Font("Arial",6);
Text = coordsX + "." + coordsY;
}
public static int coordsX { get; set; }
public static int coordsY { get; set; }
And the click event like this:
private void PieceButton_Click (object sender,EventArgs e)
{
MessageBox.Show(ButtonControls.coordsX+"/"+ButtonControls.coordsY);
}
How can I make it show the coords of the button I clicked?
senderparameter. Cast it toButtonControls