C# How can I access to a dynamic created array of labels

Posted by Markus Betz on Stack Overflow See other posts from Stack Overflow or by Markus Betz
Published on 2012-09-08T21:30:08Z Indexed on 2012/09/08 21:38 UTC
Read the original article Hit count: 251

Filed under:
|
|

I created an array of labels on runtime. Now i have a problem to access these labels from other functions.

Dynamic creation:

private void Form1_Shown(object sender, EventArgs e)
{
    Label[] Calendar_Weekday_Day = new Label[7];
    for (int i = 0; i < 7; i++)
    {
        Calendar_Weekday_Day[i] = new Label();
        Calendar_Weekday_Day[i].Location = new System.Drawing.Point(27 + (i * 137), 60);
        Calendar_Weekday_Day[i].Size = new System.Drawing.Size(132, 14);
        Calendar_Weekday_Day[i].Text = "Montag, 01.01.1970";
        this.TabControl1.Controls.Add(Calendar_Weekday_Day[i]);
    }
}

And the function where I want to access to the dynamic created array of labels:

private void display_weather_from_db(DateTime Weather_Startdate)
{
        Calendar_Weekday_Day[0].Text = "Test1";
        Calendar_Weekday_Day[1].Text = "Test2";
}

Error shown: Error 1 The name 'Calendar_Weekday_Day' does not exist in the current context Form1.cs 1523 25 Test

I tryed this, but didn't help :(

public partial class Form1 : Form
{

    private Label[] Calendar_Weekday_Day;
}

Someone an idea?

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays