Jar.jar
Jar.jar

Reputation: 45

Creating multiple buttons of the same class

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?

Upvotes: 1

Views: 926

Answers (2)

Leo
Leo

Reputation: 5122

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...

Upvotes: 2

Antoine V
Antoine V

Reputation: 7204

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);
}

Upvotes: 0

Related Questions