【问题标题】:Why the timer is not showing the dots on the label as text each time less one?为什么计时器每次都没有将标签上的点显示为文本?
【发布时间】:2022-01-21 16:58:04
【问题描述】:
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 进行测试,但它不起作用,它只显示最后三个点,仅此而已。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

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

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

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

    【讨论】:

    • 为什么当我将控件从工具箱拖到 form1 设计器时,它会显示标签名称 loadingLabel1 和它附近的点?如何删除名称 loadingLabel1 并仅显示点?我尝试在控件的构造函数中使 this.Text = "";但它也没有改变任何东西我也试过 this.Name = "";并且在form1设计器中拖动它时也没有删除控件名称。
    • 我试过了,但是当从工具箱中拖入 form1 设计器时,它仍然显示 loadingLabel1:public LoadingLabel() { InitializeComponent(); this.Font = new Font("Arial", 14, FontStyle.Bold); this.Text = ""; this.Name = ""; StartCountDownTimer(间隔,真); }
    • @SherylLarson 这是另一个问题,不是吗?你能在设计器的属性面板中清除标签的Text吗?
    【解决方案2】:

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

    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 += ".";
        }
    }
    

    【讨论】:

    • 为什么当我将控件从工具箱拖到 form1 设计器时,它会显示标签名称 loadingLabel1 和它附近的点?如何删除名称 loadingLabel1 并仅显示点?我尝试在控件的构造函数中使 this.Text = "";但它也没有改变任何东西我也试过 this.Name = "";并且在form1设计器中拖动它时也没有删除控件名称。
    • 我试过了,但是当从工具箱中拖入 form1 设计器时,它仍然显示 loadingLabel1:public LoadingLabel() { InitializeComponent(); this.Font = new Font("Arial", 14, FontStyle.Bold); this.Text = ""; this.Name = ""; StartCountDownTimer(间隔,真); }
    • 清除Text属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2018-06-08
    相关资源
    最近更新 更多