为什么计时器每次都不会将标签上的点显示为文本?

Why the timer is not showing the dots on the label as text each time less one?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Extract
{
    public partial class LoadingLabel : Label
    {
        private int TimeToCount = 300;
        private int Interval = 1000;
        private System.Windows.Forms.Timer _timer;
        private int counter = 0;

        public LoadingLabel()
        {
            InitializeComponent();

            this.Font = new Font("Arial", 14, FontStyle.Bold);

            StartCountDownTimer(Interval, true);
        }

        public void StartCountDownTimer(int Interval, bool EnableTimer)
        {
            _timer = new System.Windows.Forms.Timer
            {
                Interval = Interval,
                Enabled = false
            };

            _timer.Enabled = EnableTimer;

            _timer.Tick += (sender, args) =>
            {
                if (counter == 0)
                {
                    this.Text = ".";
                    Thread.Sleep(3);
                    counter++;
                }

                if(counter == 1)
                {
                    this.Text = "..";
                    Thread.Sleep(3);
                    counter++;
                }

                if(counter == 2)
                {
                    this.Text = "...";
                    Thread.Sleep(3);
                    counter = 0;
                }
            };
        }
    }
}

间隔设置为1000秒。

我想使用间隔,因此每秒都会添加另一个点,从一个点到三个点。 然后最后三个点从一个点重新开始。

我尝试使用 Thread.Sleep 进行测试,但它不起作用,它只显示最后三个点,仅此而已。

尝试一个简单的解决方案:

public Form1()
{
    InitializeComponent();
    Text = "";
    timer = new System.Windows.Forms.Timer
    {
       Interval = 1000
    };
    timer.Tick += Timer_Tick;
    timer.Start();
}

private void Timer_Tick(object? sender, EventArgs e)
{
    if (Text.Contains("..."))
    {
       Text = "";
    }
    else
    {         
       Text += ".";
    }
}

_timer.Tick 中,您的 if 顺序为 运行。
您可以将第二个和第三个 if 替换为 else if 或将您的代码替换为以下内容:

if (counter == 3)
{
    this.Text = "";
    counter = 0;
}
this.Text += ".";
Thread.Sleep(3);
counter++;

您也可以删除 counter 字段,因为它的值始终等于 Text.Length.