1

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?

1
  • Use the sender parameter. Cast it to ButtonControls Commented Sep 14, 2018 at 12:34

2 Answers 2

2

The problem is that your coordinates are static.

Remove the static ie.

public int coordsX { get; set; }
public int coordsY { get; set; }

Then use the sender instance rather than the class to access these properties in the click event.

private void PieceButton_Click (object sender,EventArgs e)
{
     var button = (ButtonControls)sender;
     MessageBox.Show(button.coordsX+"/"+ button.coordsY);
}

As a side note, your class name ButtonControls is not great - why is it plural? ButtonControl is better but it still does not describe the difference from the class it inherits from... PieceButton is even better (I don't know the context so it is just a guess) but personally I would just call it Piece...

Sign up to request clarification or add additional context in comments.

Comments

0

Cast your sender to the BottonControls to get the button clicked

Then, to access your property coordsX , coordsY, you must remove static keyword

public int coordsX { get; set; }
public int coordsY { get; set; }

private void PieceButton_Click (object sender,EventArgs e)
{
     var button = (ButtonControls)sender;
     MessageBox.Show(button.coordsX+"/"+ button.coordsY);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.