c# 在计时器滴答声中想要计数 ++ 0 到 30,然后计数 -- 一次又一次地从 30 到 0。怎么做?

0 c# winforms

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int A = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {           
            A++;
            if (A == 30)
            {
                A--;
            }
            textBox1.Text = A.ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

AKX*_*AKX 5

只需存储您前进的方向并在达到极限时翻转它:

int A = 0;
int direction = 1;

private void timer1_Tick(object sender, EventArgs e)
{           
    A += direction;

    if (A == 30 || A == 0)
    {
        direction = -direction;
    }
    textBox1.Text = A.ToString();
}
Run Code Online (Sandbox Code Playgroud)