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 進行測驗,但它不起作用它只顯示最后三個點,就是這樣。
uj5u.com熱心網友回復:
在_timer.Tick您的ifs 中按順序運行。
你可以取代第2和第3if與Selse if或替換為以下代碼:
if (counter == 3)
{
this.Text = "";
counter = 0;
}
this.Text = ".";
Thread.Sleep(3);
counter ;
您也可以消除該counter欄位,因為它的值始終等于Text.Length.
uj5u.com熱心網友回復:
嘗試一個簡單的解決方案:
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 = ".";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389781.html
上一篇:通過Web在WinForm中列印
