如何在单击鼠标时获取按钮在数组中的位置

时间:2018-07-27 13:42:58

标签: c# arrays button

我有从 array 创建按钮网格的代码。我需要在鼠标单击事件上将它们的位置放在数组中。任何想法或与其他帖子/文章的链接都将非常有帮助。

用于创建网格的代码:

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

4 个答案:

答案 0 :(得分:1)

我建议在tiles25事件处理程序中扫描Click数组

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

答案 1 :(得分:0)

您需要为单击按钮时设置事件处理程序。现在,您要做的就是创建按钮并将它们添加到给定位置的控件列表中。现在,您只需添加click事件的事件处理程序即可!

//...  
// 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;
    //...
}

然后在Tile_Click()事件处理程序内部,您可以使用任何必要的代码来获得clickedButton对象的位置。

答案 2 :(得分:0)

为每个按钮click事件注册一个事件处理程序,然后从Location对象中提取sender属性:

// 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 :(得分:0)

如果我没看错VoidWalker,您正在尝试获取源数组中该项的位置(索引),而不是屏幕上按钮的实际位置。如果前一种情况是正确的,请继续阅读,对于后者,我们上面有一些不错的答案。

您需要做的是在每个按钮上标记一个标识符,该标识符将用于推断位置。一种简单但该死的高效方法。

在创建按钮时:

            // 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]);

然后单击按钮即可轻松完成

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

这样,您可以添加多条信息,例如页面上的网格层次结构。您可以精确地确定要访问的元素而无需运行任何循环,Array.IndexOf

就是这种情况