记录鼠标点击次数的代码怎样写?
新建一Windows窗体,在其中添加一个按钮(用什么好?)
点击一下按钮,按钮从1开始计算,再点击则变为2,以+1递增,
另外,可以另增加一按钮 ,点击一下则将数据重置为0。
类似一些网页中的投票,这样的功能怎样实现呢?
请各位指点一下!谢谢
[解决办法]
次数显示在按钮上
- C# code
// 构造函数里this.button1.Text = "0";// 点击事件private void button1_Click(object sender, EventArgs e){ this.button1.Text = (int.Parse(this.button1.Text) + 1).ToString();}
[解决办法]
设置一个全局变量咯,
int click_count=0;
private void button1_Click(object sender, EventArgs e)
{
click_count++;
}
private void button2_Click(object sender, EventArgs e)
{
click_count=0
}
[解决办法]
- C# code
static int count = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { count++; } private void button2_Click(object sender, EventArgs e) { count = 0; } private void button3_Click(object sender, EventArgs e) { MessageBox.Show(count.ToString()); }
[解决办法]
不显示在按钮上
- C# code
// 全局变量private int clickCount = 0;// 递增按钮事件private void button1_Click(object sender, EventArgs e){ ++clickCount;}// 清空按钮事件private void button1_Click(object sender, EventArgs e){ clickCount = 0;}
[解决办法]
- C# code
public Form1() { InitializeComponent(); button1.Tag = 0; } private void button1_Click(object sender, EventArgs e) { button1.Tag = Convert.ToInt32(button1.Tag) + 1; } private void button2_Click(object sender, EventArgs e) { button1.Tag = 0; } private void button3_Click(object sender, EventArgs e) { MessageBox.Show(button1.Tag.ToString()); }