如何在 C# 中为动态二维按钮数组创建单击事件处理程序
How to create click event handler for a dynamic 2D array of buttons in C#
我需要动态生成 2D "card" 按钮。我将如何着手给每个人一个事件处理程序并能够直接引用按钮?
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";
}
试试这个,
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();
//}
}
结果;
在 Click 事件中,sender 代表按钮。您可以使用按钮的属性(如评论区域)检查您的随机变量。 (在结果图像中没有 if 条件 btw。)
希望有所帮助,
因此您可以创建一个按钮单击事件处理程序,并且在该处理程序中您可以根据哪个按钮被单击来切换您的逻辑。以下是如何操作的示例:
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.
}
如果您要动态创建按钮,那么您应该动态创建事件处理程序。创建 void btn_Click(object sender, EventArgs e)
违背了良好的 OO 设计 - 您想要封装代码而不是将其留在那里供任何代码调用。
方法如下:
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
方法中,您可以完全访问处理程序中的 button
实例。干净整洁。
我需要动态生成 2D "card" 按钮。我将如何着手给每个人一个事件处理程序并能够直接引用按钮?
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";
}
试试这个,
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();
//}
}
结果;
在 Click 事件中,sender 代表按钮。您可以使用按钮的属性(如评论区域)检查您的随机变量。 (在结果图像中没有 if 条件 btw。)
希望有所帮助,
因此您可以创建一个按钮单击事件处理程序,并且在该处理程序中您可以根据哪个按钮被单击来切换您的逻辑。以下是如何操作的示例:
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.
}
如果您要动态创建按钮,那么您应该动态创建事件处理程序。创建 void btn_Click(object sender, EventArgs e)
违背了良好的 OO 设计 - 您想要封装代码而不是将其留在那里供任何代码调用。
方法如下:
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
方法中,您可以完全访问处理程序中的 button
实例。干净整洁。