[英]button push increment number to a text box c# winform
這是我正在上課的一個項目,我正在嘗試創建一個將有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)
{
}
}
}
將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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.