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

C# 如何在单击鼠标时获取数组中按钮的位置,c#,arrays,button,C#,Arrays,Button,我有从数组创建按钮网格的代码。我需要在鼠标点击事件中获取它们的位置。任何关于其他帖子/文章的想法或链接都会非常有用 用于创建网格的代码: // Creating buttons array for 5x5 grid Button[] tiles25 = new Button[25]; // Generating 5x5 button grid void Spawn5x5Grid() { // position of the firts t

我有从数组创建按钮网格的代码。我需要在鼠标点击事件中获取它们的位置。任何关于其他帖子/文章的想法或链接都会非常有用

用于创建网格的代码:

    // 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;
    }         
//为5x5网格创建按钮数组
按钮[]瓷砖25=新按钮[25];
//生成5x5按钮网格
void Spawn5x5Grid()
{
//第一块瓷砖的位置
int x=35,y=55;
//当前磁砖索引
整数计数=0;
对于(int i=1;i<6;i++)
{
对于(int j=1;j<6;j++)
{
//向数组中添加按钮
tiles25[计数]=新建按钮()
{
尺寸=新尺寸(24,24),
位置=新点(x,y)
};
//将按钮从数组添加到窗体
控件。添加(tiles25[count]);
计数++;
x=x+24;
}
x=35;
y=y+24;
}
lblSize.Text=“5 x 5”;
currentGrid=Grids.grid5x5;
}         

我建议扫描
事件处理程序中的
tiles 25
数组

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

您需要为单击按钮时设置事件处理程序。现在,您所要做的就是创建按钮并将它们添加到给定位置的控件列表中。现在,您只需添加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\u Click()
事件处理程序内部,您可以使用任何必要的代码来获取clickedButton对象的位置。

为每个按钮
单击
事件注册事件处理程序,然后从
发送者
对象中提取
位置
属性:

// 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);
}
//生成5x5按钮网格
void Spawn5x5Grid()
{
//第一块瓷砖的位置
int x=35,y=55;
//当前磁砖索引
整数计数=0;
对于(int i=1;i<6;i++)
{
对于(int j=1;j<6;j++)
{
//向数组中添加按钮
tiles25[计数]=新建按钮()
{
尺寸=新尺寸(24,24),
位置=新点(x,y)
};
//将按钮从数组添加到窗体
控件。添加(tiles25[count]);
tiles25[计数]。单击+=tiles25\u单击;
计数++;
x=x+24;
}
x=35;
y=y+24;
}
lblSize.Text=“5 x 5”;
currentGrid=Grids.grid5x5;
}
私有void tiles 25_单击(对象发送方,事件参数e)
{
var bt=发送器as按钮;
MessageBox.Show(“X=“+bt.Location.X+”;Y=“+bt.Location.Y”);
}

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

您需要做的是用一个标识符标记每个按钮,该标识符将用于推断位置。一个简单但非常有效的方法

创建按钮时:

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

通过这种方式,您可以添加多条信息,例如页面上的网格层次结构。无需运行任何循环即可准确地确定要访问的元素,这与
数组的情况相同。IndexOf

因此,第一个按钮位于x=35,每个按钮的宽度均为24,并且所有按钮都没有空格。我说的对吗?嗯,您需要为按钮的名称添加一个事件处理程序,这将使单击的按钮作为
sender
参数传递给它。到目前为止你尝试了什么,你到底被困在哪里?(我在你的代码中还没有看到任何点击事件处理程序)……你的按钮后来会移动吗?@Andreaジーティーオー Yup@bassfader到目前为止,我只是在寻找解决方案。我还没试过什么特别的,谢谢!这正是我需要的。