这是我正在为课堂做的一个项目,我正在尝试创建一个win形式,它将有2个按钮,当按下按钮时,它将在文本框中递增,而当按钮被按下时将递减按钮被按下。我找不到能做我想做的正确的路线。有人可以帮助我吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project10TC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i++);
}
private void button2_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i--);
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:4)
答案 1 :(得分:0)
将i
变量声明为字段。此外,我会使用++i
代替i++
。否则,文本框和变量中的值不同。此外,无需使用Convert.ToString()
。
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
i = 0;
}
//...
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString;
}
}