如何在单击鼠标时获取按钮在数组中的位置 [英] How to get position of a button in an array on mouse clicked

查看:140
本文介绍了如何在单击鼠标时获取按钮在数组中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

创建网格的代码:

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


推荐答案

我建议扫描<$ c Click 事件处理程序中的$ c> tiles25 数组

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

这篇关于如何在单击鼠标时获取按钮在数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆