如何在C#中为动态2D按钮数组创建单击事件处理程序

时间:2016-09-26 23:44:57

标签: c# project

我需要生成一张2D"卡"按钮动态。我如何为每个人提供一个事件处理程序并能够直接引用该按钮?

public void generateButtonsCard(Panel cardPanel)
    {
        for (int y = 0; y <= 4; y++)
        {
            for (int x = 0; x <= 4; x++)
            {
                cardButtons[x, y] = new Button();
                cardButtons[x, y].Size = new Size(80, 80);
                cardButtons[x, y].Name = "btn" + x + "" + y;
                cardButtons[x, y].Location = new Point(80 * x, 80 * y);
                cardPanel.Controls.Add(cardButtons[x, y]);

            }

        }

        RNGCard();
        cardButtons[2, 2].Text = "Free Space";

    }

3 个答案:

答案 0 :(得分:1)

试试这个,

public void generateButtonsCard(Panel cardPanel)
{
    for (int y = 0; y <= 4; y++)
    {
        for (int x = 0; x <= 4; x++)
        {
            cardButtons[x, y] = new Button();
            cardButtons[x, y].Size = new Size(80, 80);
            cardButtons[x, y].Name = "btn" + x + "" + y;
            cardButtons[x, y].Location = new Point(80 * x, 80 * y);
            cardButtons[x, y].Click += btn_Click;
            cardPanel.Controls.Add(cardButtons[x, y]);

        }

    }

    RNGCard();
    cardButtons[2, 2].Text = "Free Space";

}

private void btn_Click(object sender, EventArgs e){   
        Button b = sender as Button;
        //if (b.Name == "1")
        //{
            label1.Text = b.Name.ToString();
        //}
}

结果;

enter image description here

在Click事件中,发件人代表该按钮。您可以使用按钮的属性(如注释区域)检查随机变量。 (在结果图像中没有条件btw。)

希望有所帮助,

答案 1 :(得分:1)

如果您动态创建按钮,则应动态创建事件处理程序。创建if let arrayOfTabBarItems = self.tabBar.items as AnyObject as? NSArray,let tabBarItem = arrayOfTabBarItems[1] as? UITabBarItem { tabBarItem.isEnabled = true } 违背了良好的OO设计 - 您希望封装代码,而不是将其留在那里以便调用任何代码。

以下是:

void btn_Click(object sender, EventArgs e)

处理程序封装在public void generateButtonsCard(Panel cardPanel) { for (int y = 0; y <= 4; y++) { for (int x = 0; x <= 4; x++) { var button = new Button(); button.Size = new Size(80, 80); button.Name = "btn" + x + "" + y; button.Location = new Point(80 * x, 80 * y); button.Click += (s, e) => { /* Your event handling code here with full access to `button` above */ }; cardPanel.Controls.Add(button); cardButtons[x, y] = button; } } RNGCard(); cardButtons[2, 2].Text = "Free Space"; } 方法中,您可以完全访问处理程序中的generateButtonsCard实例。它干净整洁。

答案 2 :(得分:-2)

因此,您可以创建单个按钮单击事件处理程序,在该处理程序中,您可以根据单击按钮切换逻辑。以下是如何执行此操作的示例:

public class XYButton : Button
{
    private int xPos;
    private int yPos;

    public XYButton(int x, int y)
    {
        xPos = x;
        yPos = y;
    }

    public int GetX()
    {
        return xPos;
    }

    public int GetY()
    {
        return yPos;
    }
}

然后使用按钮的这个新扩展......

public void generateButtonsCard(Panel cardPanel)
{
    for (int y = 0; y <= 4; y++)
    {
        for (int x = 0; x <= 4; x++)
        {
            cardButtons[x, y] = new XYButton(x,y);
            cardButtons[x, y].Size = new Size(80, 80);
            cardButtons[x, y].Name = "btn" + x + "" + y;
            cardButtons[x, y].Location = new Point(80 * x, 80 * y);
            cardButtons[x, y].Click += btn_Click;
            cardPanel.Controls.Add(cardButtons[x, y]);

        }

    }

    RNGCard();
    cardButtons[2, 2].Text = "Free Space";

}

private void btn_Click(object sender, EventArgs e){
   var xyButton = sender as XYButton;
   // now you can get the x and y position from xyButton.
}