winform重复一条语句

[复制链接]
查看11 | 回复4 | 2011-3-9 14:45:59 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

int num;

num = 0;

num++;

string shu = num.ToString();

textBox1.Text = shu;
}

}
}
这条语句按一次button1,textbox1输出1.再按一次还是1,怎么改才能按1次textbox1就加1?

回复

使用道具 举报

千问 | 2011-3-9 14:45:59 | 显示全部楼层
private int num;private void button4_Click(object sender, System.EventArgs e){
num++; string shu = num.ToString(); textBox1.Text = shu;}你把num定义成全局变量,如果你那样定义,每次按button1时,你的num都初始化为0了,所以,每次按下都是1。
回复

使用道具 举报

千问 | 2011-3-9 14:45:59 | 显示全部楼层
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Wind
回复

使用道具 举报

千问 | 2011-3-9 14:45:59 | 显示全部楼层
因为你每次按下按钮的时候num都重新被赋值了.把num=0去掉private num=0;private void button1_Click(object sender, EventArgs e)
{
num++;
string shu = num.ToStrin
回复

使用道具 举报

千问 | 2011-3-9 14:45:59 | 显示全部楼层
你把int num申明在外面 作用域的问题啊如果你把int num在方法中申明每次点击按钮int num就会被重新申明
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行