Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a 2D button array of 8 by 8 (64 buttons). When i click a button i want its location (ez buttarr[1,2]) showing in a text box.
I know its has something to do with the object sender, you do a temp button but cant get the code working to give me the location and not the data written in it

thanks
JP
Posted

1 solution

C#
private Button[,] CreateButtonArray(int width, int height)
{
    Button[,] buttonArray = new Button[height, width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            buttonArray[i, j] = new Button();
            buttonArray[i, j].Click += new EventHandler(AnyButton_Click);
            buttonArray[i, j].Tag = new Point(j, i);
        }
    }
    return buttonArray;
}

private void AnyButton_Click(object sender, EventArgs e)
{
    Point p = (Point)((sender as Button).Tag);
    this.textBox1.Text = string.Format("You clicked button[{0}, {1}].", p.Y, p.X);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900