簡體   English   中英

按鈕將增量數字推到文本框c#winform

[英]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之間區別

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM