c# 窗体上有一个textbox控件,一个button控件。要求每当用户单击按钮时,文本框会自动反映单机的次数

如题所述

winform还是asp.net
对于asp.net
protected void Button1_Click(object sender, EventArgs e)
{
int hits;
if (ViewState["hits"] == null)
{
hits = 0;
}
else
{
hits = (int)ViewState["hits"];
}
hits++;
ViewState["hits"] = hits;
this.TextBox1.Text = hits.ToString();
}

对于winform

public int i;
private void button1_Click(object sender, EventArgs e)
{
i++;
this.textBox1.Text = "单击了"+i.ToString()+"次";
}

private void Form1_Load(object sender, EventArgs e)
{
i = 0;
}

以上代码vs 2005 vs 2008均调试通过
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-19
用一个静态变量保存一下就行了.static int total = 0; 然后在单击事件里写上:total++;MessageBox.show(total);
第2个回答  2009-06-19
2楼正解
相似回答