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)
只需存储您前进的方向并在达到极限时翻转它:
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)