c# - 如何触发动态按钮的按钮事件?
问题描述
我有一个扫雷游戏。有一个动态创建的按钮板。我想要做的是打开整个板(基本上是对每个按钮执行点击事件)。
我曾尝试使用 PerformClick 方法,但它不起作用。首先,我使用btn.Click
event as btn.Click += new EventHandler(btn_Click)
,但btn.MouseUp
由于左/右键的使用,我最近将其更改为 event 。即使我使用了该btn.Click
事件,PerformClick 也不起作用。
public Form1()
{
InitializeComponent();
GenerateMineSweeper();
// my try of using PerfomClick
for (int i = 0; i < this.Controls.Count; ++i)
{
if (this.Controls[i] is Button)
{
Button btn = (Button)this.Controls[i];
btn.PerformClick();
}
}
}
private void GenerateMineSweeper()
{
// board is 15 x 25
Point p = new Point(0, 0);
for (int i = 1; i <= 15; ++i)
{
for (int j = 1; j <= 25; ++j)
{
Button btn = new Button();
btn.Location = p;
btn.Size = new Size(25, 25);
btn.BackColor = Color.RoyalBlue;
btn.FlatStyle = FlatStyle.Popup;
// btn.Click += new EventHandler(btn_Click);
btn.MouseUp += (s, e) =>
{
switch (e.Button)
{
case MouseButtons.Left:
btn_LeftClick(s, e);
break;
case MouseButtons.Right:
btn_RightClick(s, e);
break;
}
};
this.Controls.Add(btn);
p.X += 25;
}
p.Y += 25;
p.X = 0;
}
}
private void btn_LeftClick(object sender, EventArgs e)
{
Button btn = sender as Button;
// code
}
private void btn_RightClick(object sender, EventArgs e)
{
// code
}
PS:因为我使用 MouseUp 事件,我显然想要一个 LeftClick 事件触发器,而不是 RightClick。
编辑:我也尝试这样做:
for (int i = 0; i < this.Controls.Count; ++i)
{
if (this.Controls[i] is Button)
{
Button btn = (Button)this.Controls[i];
btn.MouseUp += btn_LeftClick;
}
}
但我得到了一个StackOverflowException
(在 mscorlib.dll 中发生了“System.StackOverflowException”类型的未处理异常)。
解决方案
这是一些示例代码,可以让您了解您尝试做的每件事
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
//Let's put 5 buttons on the form
Button btn = new Button();
btn.Location = new Point(10, i * 25);
if (i == 2)
{
btn.Name = "Bomb";
}
else
{
btn.Name = "btn" + i;
}
btn.Text = "Closed";
//Add a Click EventHandler
btn.Click += new EventHandler(btn_Click);
//Add a MouseUp MouseEventHandler
btn.MouseUp += new MouseEventHandler(btn_MouseUp);
//Add them to the form
Controls.Add(btn);
}
}
private void btn_MouseUp(object sender, MouseEventArgs e)
{
Button b = sender as Button;
//Show me which button was used when this event was triggered
//MessageBox.Show(e.Button.ToString() + " Mouse button was used.");
//Since the Right button on mouseup will not be considered a Click, we can tell it to PerformClick
if (e.Button == MouseButtons.Right)
{
b.PerformClick();
}
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn.Name == "Bomb" && gameOver == false)
{
gameOver = true;
btn.Text = "Boom";
ExplodeAll(btn);
}
else
{
btn.Text = "Opened";
}
}
bool gameOver = false;
private void ExplodeAll(Button sender)
{
foreach (Button b in this.Controls.OfType<Button>())
{
if (b.Name.StartsWith("btn"))
{
b.PerformClick();
}
}
}
编辑:添加了ExplodeAll
一种在单击炸弹时打开所有按钮的方法。单击第三个按钮将模拟炸弹打开所有按钮.PerformClick()
推荐阅读
- jasper-reports - Acrobat 阅读器无法提取嵌入字体“PWLEAX+NotoSerifCJKsc-Regular-Identity-H”
- python - 有没有把这个文本文件格式转换成二维列表的功能?
- r - R 中的 X[1:(n-2)] 究竟做了什么?
- java - 如何为 Selenium WebDriver 实现 Advanced ExtentReports Logic
- python - Scrapy 中的限制请求
- java - WireMockServer 与 MockServerClient 之间的区别
- python - 根据 pandas 中的 MULTIPLE 列中的值从 DataFrame 中选择行
- memory - 当进程到达特定内存时如何执行任务?
- jakarta-ee - 将 JWT 承载传递给受保护的 JAX-RS 资源时,Payara Micro 返回 AuthenticationStatus.SEND_FAILURE
- c++ - 通过字符将 .txt 读入向量但不忽略空格