下面是一个使用C#编写的按钮计数器的Windows Forms解决方案的代码示例:
using System;
using System.Windows.Forms;
namespace ButtonCounter
{
public partial class MainForm : Form
{
private int count;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
count = 0;
UpdateCountLabel();
}
private void btnClickMe_Click(object sender, EventArgs e)
{
count++;
UpdateCountLabel();
}
private void UpdateCountLabel()
{
lblCount.Text = $"Button clicked {count} times";
}
}
}
上述示例代码中的MainForm
类继承自Form
类,并包含一个按钮(btnClickMe
)和一个标签(lblCount
)。在窗体加载时,初始化计数器(count
)为0,并调用UpdateCountLabel
方法更新标签文本。当用户点击按钮时,计数器增加1,并再次调用UpdateCountLabel
方法更新标签文本。
你可以将以上代码复制到一个新的Windows Forms应用程序项目中,并将相应的控件添加到窗体设计器中,然后编译和运行该项目,即可看到按钮计数器的效果。