@rhodanny , Based on your description, you want to stop dots counting and don't show anything in LoadingLabel when the form is load.
I make some changes on your code.
First, I add a property called Isopen to check if the timer is open.
Second, I changed base.Text = DesignMode ? "" : value;
to base.Text = !DesignMode ? "" : value;
.
Here is a code example you could refer to.
public partial class LoadingLabel : Label
{
private int Interval = 1000;
private System.Windows.Forms.Timer _timer;
private int counter = 0;
public bool IsOpen { get; set; }
public LoadingLabel()
{
//InitializeComponent();
this.Font = new Font("Arial", 14, FontStyle.Bold);
StartCountDownTimer(Interval, IsOpen);
}
public void StartCountDownTimer(int Interval, bool EnableTimer)
{
_timer = new System.Windows.Forms.Timer
{
Interval = Interval,
Enabled = false
};
_timer.Enabled = EnableTimer;
_timer.Tick += (sender, args) =>
{
base.Text += ".";
counter++;
if (counter == 4)
{
base.Text = "";
counter = 0;
}
};
}
[DefaultValue("")]
public override string Text
{
get => base.Text;
set
{
base.Text = !DesignMode ? "" : value;
Invalidate();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
loadingLabel1.StartCountDownTimer(1000, false);
}
private void button1_Click(object sender, EventArgs e)
{
loadingLabel1.IsOpen=true;
loadingLabel1.StartCountDownTimer(1000, true);
}
}
Update for solving stopping timer:
Please add a method called stop method in your LoadingLabel class.
public void Stop()
{
_timer.Enabled=false;
_timer.Stop();
_timer=null;
}
Then you can call the code in the stop button_click event.
private void button2_Click(object sender, EventArgs e)
{
loadingLabel1.Stop();
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.