How to get position of a button in an array on mouse clicked

岁酱吖の 提交于 2020-01-06 06:32:22

问题


I have the code that creates a grid of buttons from an array. I need to get their possitions in array on mouse clicked event. Any ideas or links to some other post/articles would be very helpful.

The code for creating the grid:

    // Creating buttons array for 5x5 grid
    Button[] tiles25 = new Button[25];

    // Generating 5x5 button grid
    void Spawn5x5Grid()
    {
        // position of the firts tile
        int x = 35, y = 55;

        // current tile index
        int count = 0;

        for (int i = 1; i < 6; i++)
        {
            for (int j = 1; j < 6; j++)
            {
                // Adding button to the array
                tiles25[count] = new Button()
                {
                    Size = new Size(24, 24),
                    Location = new Point(x, y)
                };
                // Adding buttons from array to the form
                Controls.Add(tiles25[count]);
                count++;
                x = x + 24;
            }
            x = 35;
            y = y + 24;
        }
        lblSize.Text = "5 x 5";
        currentGrid = Grids.grid5x5;
    }         

回答1:


I suggest scanning tiles25 array in the Click event handler

  ...
  Controls.Add(tiles25[count]);     

  tiles25[count].Click += (o, ee) => {
    Button button = o as Button; 

    int index = Array.IndexOf(tiles25, button);

    //TODO: Put relevant code here: "button" clicked which is at "index" position
  };

  count++;
  x = x + 24;
  ...



回答2:


Register an event handler for each of your button click events and then extract the Location property from the sender object:

// Generating 5x5 button grid
void Spawn5x5Grid()
{
    // position of the firts tile
    int x = 35, y = 55;

    // current tile index
    int count = 0;

    for (int i = 1; i < 6; i++)
    {
        for (int j = 1; j < 6; j++)
        {
            // Adding button to the array
            tiles25[count] = new Button()
            {
                Size = new Size(24, 24),
                Location = new Point(x, y)
            };
            // Adding buttons from array to the form
            Controls.Add(tiles25[count]);

            tiles25[count].Click += Tiles25_Click;
            count++;
            x = x + 24;
        }
        x = 35;
        y = y + 24;
    }
    lblSize.Text = "5 x 5";
    currentGrid = Grids.grid5x5;
}

private void Tiles25_Click(object sender, EventArgs e)
{
    var bt = sender as Button;

    MessageBox.Show("X = " + bt.Location.X + "; Y = " + bt.Location.Y);
}



回答3:


You need to set up an event handler for when the button is clicked. Right now, all you're doing is creating the buttons and adding them to the list of controls at a given position. Now, all you have to add is the event handler for the click event!

//...  
// Adding button to the array
tiles25[count] = new Button()
{
    Size = new Size(24, 24),
    Location = new Point(x, y),
};
tiles25[count] += new EventHandler(this.Tile_Click);
//...

void Tile_Click(Object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    //...
}

Then inside of the Tile_Click() event handler, you can use whatever code necessary to get the position with the clickedButton object.




回答4:


If I'm not wrong VoidWalker, you are trying to get the position(index) of the item in the source array and not the actual position of the button on screen. If the former case is true read on, for the latter we have some good answers above.

What you need to do is to mark each button with an identifier that would be used to infer the position. A simple yet damn efficient approach.

At the time of creating the button:

            // Adding button to the array
            tiles25[count] = new Button()
            {
                Size = new Size(24, 24),
                Location = new Point(x, y)
            };
            // Add the current index to the name field of the Button
            tiles25[count].Name = "Grid5-Btn" + count.ToString();
            // Adding buttons from array to the form
            Controls.Add(tiles25[count]);

Then on button click you can simply do

void Tile_Click(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
var index = int(clickedButton.Name.Split("Grid5-Btn")[0]);
   //...
}

This way you can add multiple pieces of informatio such as the hierarchy of grids on the page. You can exactly pinpoint which element to access without running any loops, which would be the case with Array.IndexOf



来源:https://stackoverflow.com/questions/51559300/how-to-get-position-of-a-button-in-an-array-on-mouse-clicked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!