How can I stop/start the timer in user control through form1 ?

rhodanny 166 Reputation points
2021-12-20T05:41:36.107+00:00

The user control code :

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 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) =>
            {
                base.Text += ".";

                counter++;

                if(counter == 4)
                {
                    base.Text = "";
                    counter = 0;
                }
            };
        }

        [DefaultValue("")]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = DesignMode ? "" : value;
                Invalidate();
            }
        }
    }
}

Then in the form1 constructor I want to stop I don't want to start the timer and not to show anything in the label user control :

But it does show the dots counting and also start with already one dot so first time it will count and show 4 dots then each time 3. Not sure why.

public Form1()
        {
            InitializeComponent();

            loadingLabel1.StartCountDownTimer(1000, false);

I want that first time it will not show anything and then at some point to start showing :

private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnRadarPath.Enabled = false;
            btnSatellitePath.Enabled = false;

            lblStatus.Text = "Preparing Downloads";
            loadingLabel1.StartCountDownTimer(1000, true);
        }

What I want to do is to use the StartCountDownTimer to start stop the dots counting. if false stop don't show anything and reset. if true then start at that point from the start dot by dot should be 3 dots.

The user control it self is working fine I just want to enable disable it in form1 and when setting to false it should also reset it or when setting to true to reset and start over.
but now it's showing the dots counting no matter if it's false or not either if not or if running the app.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-12-20T10:03:34.257+00:00

    @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:

    159180-15.gif


    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.

    1 person found this answer helpful.