我如何计算在我的windows窗体中按下了多少按钮
本文关键字:窗体 按钮 多少 windows 我的 何计算 计算 | 更新日期: 2023-09-27 18:29:07
我从一个类似的问题中找到了一段代码,但它不太适合我的实现,我不知道如何将其适应我的游戏。我有十五个按钮,我需要能够计算游戏每回合按下的按钮数量。我是一个编程知识有限的初学者。我需要能够计算按下的按钮,然后在每个玩家回合重新启动该方法。
private void label1_MouseClick(object sender, MouseEventArgs e)
{
int count++;
}
我创建了鼠标点击事件,但在尝试增加计数int
时出错。
使用此类,则可以在设计器中使用ButtonEx
而不是Button
public class ButtonEx : Button
{
public int ClickCount { get; private set; }
public ButtonEx()
{
this.Click += (s, e) => { ++this.ClickCount; };
}
public void ResetPressCount()
{
this.ClickCount = 0;
}
}
我看到你在应用程序中使用了标签而不是按钮你可以用这个标签
public class LabelEx : Label
{
public int ClickCount { get; private set; }
public LabelEx()
{
this.MouseClick += (s, e) => { ++this.ClickCount; };
}
public void ResetPressCount()
{
this.ClickCount = 0;
}
}
简单修复。//all these inside form class
//declare count as integer, you can also initialize it ( int count=startvalue;)
int count;
//if you want to understand read topic about delegates and events
private void label1_MouseClick(object sender, MouseEventArgs e)
{
++count;
}
//call reset() when you want to reset
private void reset(){
count=0;
}
同时检查
stackoverflow:c#资源,书籍
因为int count++
是无效语法。
创建增量整数值的正确方法是:;
private int count = 0;
private void label1_MouseClick(object sender, MouseEventArgs e)
{
count++;
}
要重置整数count
,您需要制作一个重置按钮或将count = 0;
包含在您想要的方法中。