Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I created label during pageloading, But i want to insert the value into label from timer.


can any one suggest me about how to update the label value from timer


C#
  GroupBox grp = new GroupBox();
 for (int i = 1; i < 6; i++)
   {
              grp = new GroupBox();
              grp.Name = "GRP" + i;
              grp.Location = new Point(x, y);
              grp.Size = new Size(260, 272);
              lbl = new Label();
              x += 270;
              lblx = 10;
              lbly =210;
              lblname = "lblCharName";
              lbl = Labelcreate(lbl, lblname, i, lblx, lbly);
              grp.Controls.Add(lbl);
              this.Controls.Add(grp); 
}





 private Label Labelcreate(Label lbl,string lblname,int i, int x,int y)
        {
            lbl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lbl.Location = new System.Drawing.Point(x, y);          
            lbl.Name = lblname + i;
            lbl.Size = new System.Drawing.Size(116, 40);
            lbl.TabIndex = 1;
            lbl.Text = "0.00";
            lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            return lbl;
        }
Posted
Updated 17-Aug-15 0:34am
v4

You might either
  • Keep in your class references to created labels (e.g an array of references) and then use such references in the timer handler.
or
  • In the timer handler, search for the dynamic labels in the Controls collection (and then use them).
 
Share this answer
 
Where is your first code executing?
There's some very basic initialization which doesn't happen and the code therefore appears incomplete.

I've added a presumed context and initialization for you, please do that yourself in a future scenario, to save your helpers time ;)

Of cause the thread sleep and the tick number displaying should be replaced with your values. Also if you stick to the Windows forms timer you will not need the invoke bits, just in there for habit when dealing with timers and windows controls, plus you could quickly not want to use that anyway, in which case it will be required.

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 FormDriblings
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Timer _time = new System.Windows.Forms.Timer();

        public delegate void UpdateInstantiator();

        public Form1()
        {
            InitializeComponent();

            GroupBox grp = new GroupBox();
            grp.Name = "dynbox";
            int x, y, lblx, lbly;
            x = y = 0;
            for (int i = 1; i < 6; i++)
            {
                grp = new GroupBox();
                grp.Name = "GRP" + i;
                grp.Location = new Point(x, y);
                grp.Size = new Size(260, 272);
                var lbl = new Label();
                x += 270;
                lblx = 10;
                lbly = 210;
                var lblname = "lblCharName";
                lbl = Labelcreate(lbl, lblname, i, lblx, lbly);
                grp.Controls.Add(lbl);
                Controls.Add(grp);
            }
           
            _time.Interval = 1000;
            _time.Tick += _time_Tick;
            _time.Start();
        }

        private bool _timeTicking;
        void _time_Tick(object sender, EventArgs e)
        {
            if (_timeTicking) return;
            _timeTicking = true;
            try
            {
                UpdateGroups();
            }
            finally
            {
                _timeTicking = false;
            }
        }

        private void UpdateGroups()
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateInstantiator(UpdateGroups));
            }
            else
            { 
                for (int i = 1; i < 6; i++)
                {
                    var lbl = (Label) Controls.Find("lblCharName" + i, true).First();
                    string number = DateTime.Now.Ticks.ToString();
                    number = number.Substring(number.Length - 3);
                    lbl.Text = string.Format("{0}.00", number);
                    lbl.Refresh();
                    Thread.Sleep(120);
                }
                
            }
        }

        private Label Labelcreate(Label lbl, string lblname, int i, int x, int y)
        {
            lbl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lbl.Location = new System.Drawing.Point(x, y);
            lbl.Name = lblname + i;
            lbl.Size = new System.Drawing.Size(116, 40);
            lbl.TabIndex = 1;
            lbl.Text = "0.00";
            lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            return lbl;
        }
    }
}
 
Share this answer
 

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