Markus Betz
Markus Betz

Reputation: 21

How can I access to a dynamic created array of labels

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:

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?

Upvotes: 2

Views: 623

Answers (4)

S3ddi9
S3ddi9

Reputation: 2151

if your tabcontrol contains only the labels so

 private void display_weather_from_db(DateTime Weather_Startdate)
 {
 Label[] Calendar_Weekday_Day = this.TabControl1.Controls.OfType<Label>().ToArray();

 Calendar_Weekday_Day[0].Text = "Test1";
 Calendar_Weekday_Day[1].Text = "Test2";

 }

if there is more other labels you have to filter, so first

  for .....
   ... _Day[i].Size = new System.Drawing.Size(132, 14);
    Calendar_Weekday_Day[i].Text = "Montag, 01.01.1970";
    Calendar_Weekday_Day[i].Tag= "Weather";// specify your label tag
    this.TabControl1.Controls.Add(Calendar_Weekday_Day[i]);
  ....

then

 private void display_weather_from_db(DateTime Weather_Startdate)
 {
 Label[] Calendar_Weekday_Day = this.TabControl1.Controls.OfType<Label>().Where(X=>X.Tag!=null && X.Tag=="Weather").ToArray();


 Calendar_Weekday_Day[0].Text = "Test1";
 Calendar_Weekday_Day[1].Text = "Test2";

 }

Upvotes: 0

aiodintsov
aiodintsov

Reputation: 2605

Remove redeclaration

private void Form1_Shown(object sender, EventArgs e)
{
    Calendar_Weekday_Day = new Label[7]; // removed Label[] 

... the rest is the same

this will be the smallest change required but you should be paying attention to compiler warnings. it most likely warned you that you redeclared field.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

The problem is most likely scope or lack of initialization. Calendar_Weekday_Day only exists within the Form1_Shown context. If you try to access it from another method, you're not going to be able to see it (and when it's private it's still not initialized to adding new elements is going to be problematic). You have two options:

  • Change the scope (make Calendar_Weekday_Day a private property in the form's class and don't forget to initialize it)
  • Search for the control by accessing this.TabControl1.Controls

You may also be better off using private IEnumerable<Label> Calendar_WeekendDay or maybe even IList<Label> to give you a little more flexibility with accessing the controls later.

Upvotes: 0

Vlad
Vlad

Reputation: 35594

I guess you need just

Calendar_Weekday_Day = new Label[7];

instead of

Label[] Calendar_Weekday_Day = new Label[7];

in your Form_Shown. As it's written now, you are storing the list into a local variable instead of the instance field.

Upvotes: 3

Related Questions