Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi all

My problem:
I want to create a label dynamically.

Example:In every 10 second i want to create a label with different name.
means:
Label lblTest = new Label();

for 1st time lblTest.Text = "1 username";
after 10 second i want to create another label and assign the text as "2 hi hello"
after 10 second again create another label and assign the text as "3 username password why"
and so on


How to create a label so that it will store all the data but in different labels dynamically and how to retrieve all the labels after each 50 second?

Means :In first 50 second it will show 5 label ,after next 50 second it will show 10 labels and so on.

How to achieve this?

Please suggest

Thanks in advance.
Posted

You have tagged both ASP.NET & Winforms here.
Read about Timer control/Class. It would help you in what you need! (Would not ask why such a odd behaviour!)

ASP.NET Timer control: Timer control[^]
Winforms: Timer Class[^]
 
Share this answer
 
Comments
Nelek 18-Apr-12 16:54pm    
I think you failed in voting, there is no votes of 5 so far
Hi ,
Regarding to the First solution you should do that also for web
C#
int countTimes = 0;
   protected void Timer1_Tick(object sender, EventArgs e)
   {
       if (ViewState["countTimes"] == null)
       {
           countTimes = 1;
       }
       else
       {
           countTimes = Convert.ToInt32(ViewState["countTimes"]);
       }
       for (int i = 0; i < countTimes; i++)
       {
           Label lbl = new Label();
           txtSkill.ID = "lbl" + i;
           Form.Controls.Add(lbl);
       }
       countTimes = countTimes + 1;
       ViewState.Add("countTimes", countTimes);
   }


for windows
C#
int i = 1;
     private void timer1_Tick(object sender, EventArgs e)
     {
         Button button1 = new Button();

         button1.Location = new System.Drawing.Point(105 + 20 * i, 92 + 20 * i);
         button1.Name = "button" + i;
         button1.Size = new System.Drawing.Size(75, 23);
         button1.TabIndex = 0 + i;
         button1.Text = "button" + i;
         button1.UseVisualStyleBackColor = true;
         this.Controls.Add(button1);
         i++;
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         timer1.Start();
     }


Best Regards
M.Mitwalli
 
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