Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display clock (digital clock which update in every second) in panel on windows form. I write following code.


C#
Panel agentPanel;
      Label lblTime;
      Timer timer1;
  private void CreateAgentPanels(int numberofPanel, string Current_Status, string Op_id)
     {
         for(int i =0; i <= numberofPanel;i++)
          {
            agentPanel = new Panel();

            lblTime = new Label();
            lblTime.Location = new Point(02, 35);
            lblTime.AutoSize = true;

            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            this.timer1.Interval = 1000;
            timer1.Start()

             agentPanel.Controls.Add(lblTime);
         }
   }

 private void timer1_Tick(object sender, EventArgs e)
   {
       lblTime.Text = DateTime.Now.ToString(HH:mm:ss);
   }


but lblTime label displays only in the last panel of loop. how to display in every panel which created in the loop.

Thanks in Advance
Posted
Updated 10-Jun-14 0:34am
v3
Comments
Kornfeld Eliyahu Peter 10-Jun-14 6:32am    
Do you mean that time updates only on the last panel?
SachinSutar 10-Jun-14 6:39am    
lbltime label shows only on last panel. not shows on all panels

Try something like this:

C#
List<Label> listOfLabels = new List<Label>();

private void CreateAgentPanels(int numberofPanel, string Current_Status, string Op_id)
        {
            Panel agentPanel;
            Label lblTime;
            for(int i =0; i <= numberofPanel;i++)
            {
                
                
                agentPanel = new Panel();
                agentPanel.BackColor = Color.Blue;
                agentPanel.AutoSize = true;
                agentPanel.Size = new Size(50, 50);
                agentPanel.Location = new Point((i * 52), 0);
                
                
                 
                lblTime = new Label();
                lblTime.Location = new Point(02, 35);
                lblTime.AutoSize = true;
                 
                

                listOfLabels.Add(lblTime);
             
                agentPanel.Controls.Add(lblTime);
                this.Controls.Add(agentPanel);
            }

            Timer timer1;
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Label l in listOfLabels)
            {
                l.Text = DateTime.Now.ToString("HH:mm:ss");
            }
        }
 
Share this answer
 
Create a List at the Form level, add the lblTime to the list in CreateAgentPanels and then in the timer1_Tick method, loop through this list and set it's text.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 10-Jun-14 6:34am    
And you may add that OP do not need multiple timers to show the current time - he can use only one for each and every label and save a lot of resources...
SachinSutar 10-Jun-14 6:46am    
I create Label list and loop through timer_Tick method. it works but there are lot of label. So i need to find out some alternately solution to that loop in timer_Tick method which assign values to all label at once.


Thanks for Help..
SachinSutar 10-Jun-14 6:57am    
lst_timelable.ForEach(x => x.Text = DateTime.Now.ToString("HH:mm:ss"));
[no name] 11-Jun-14 8:03am    
(y)
[no name] 11-Jun-14 8:03am    
Thanks, I forgot to mention that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900