-3

I have 2 foreach loops, my first one is in a method, this method starts when my main form loads, the method is the following:

private void GetDateDay()
{
    foreach (DateTimePicker Dater in this.ReminderPanel.Controls.OfType<DateTimePicker>())
    {
        label1.Text = "true";
        DateTime DaterValue = Dater.Value;
        DateList.Add(Dater.Value);
    }

    SundayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Sunday);
    MondayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Monday);
    TuesdayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Tuesday);
    WednesdayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Wednesday);
    ThursdayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Thursday);
    FridayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Friday);
    SaturdayTotal = DateList.Count(x => x.DayOfWeek == DayOfWeek.Saturday);
}

This Method isn´t working, and as you can see, I changed a label.text because I wanted to see when the loop don´t work, and i found that it work but not after the loop because the label dont change its text. My theory, Is that because i Have so much controls in my main form, and my panel´s controls load by code, the method loop returns null, because the method checks before the panels controls load, even if the method is called after the UI Load Method is called.

Panel code:.

TaskUC Task = new TaskUC ();
ReminderPanel.Controls.Add(Task);

(TaskUC Contains The DateTimePicker - so the Panel contains TaskUC which contains the DateTimePicker).

mjwills
  • 23,389
  • 6
  • 40
  • 63
SebGM2018
  • 3
  • 5
  • 2
    If you have a lot of data, things just take longer, nothing gets discarded. If your code never enters the `foreach` then I suspect you have no `DateTimePicker` controls on `ReminderPanel` **or** you do, but they're controls of a child item. – ProgrammingLlama Jul 12 '18 at 01:41
  • 1
    If your controls are indeed nested deeper than just `ReminderPanel`, then you may want to look at [this answer](https://stackoverflow.com/a/3426721/3181933) for how to get an `IEnumerable` all the way down the control hierarchy. As it stands, I can't reproduce your problem by simply placing a `DateTimePicker` on `ReminderPanel`. – ProgrammingLlama Jul 12 '18 at 01:46
  • Please include a link to a dotnetfiddle that shows the contents of your designer.cs file so we can see how and where you are adding the `DateTimePicker` controls. – mjwills Jul 12 '18 at 01:46
  • 1
    If DateTimePickers are not directly under ReminderPanel then foreach loop won't execute. – Chetan Jul 12 '18 at 02:22

1 Answers1

2

The controls collection only includes immediate children, and your date picker is embedded one level deeper. You need to iterate through all the controls contained in controls contained in the ReminderPanel.

Instead of

foreach (DateTimePicker Dater in this.ReminderPanel.Controls.OfType<DateTimePicker>())
{
    label1.Text = "true";
    DateTime DaterValue = Dater.Value
    DateList.Add(Dater.Value);
}

Try this (without the foreach loop):

DateList = this.ReminderPanel.Controls            //Get all controls on the panel
    .SelectMany( c => c.Controls )                //Get all controls contained in those controls
    .OfType<DateTimePicker>()                     //Only include DateTimePickers
    .Select( dtp => dtp.Value )                   //Get the value
    .ToList();                                    //Make it a list

This will create DateList for you without the loop.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • But what if I explicit need the foreach loop?, Because I have another method with a similar structure, that method sends notifications like the following example:. foreach(DateTimePicker Dater In ReminderPanel.Controls). { DateTime Timer = Dater.Value; DateTime Now = DateTime.Now;. if (Now.Hour == Timer.Hour && Now.Minute == Timer.Minute && Now.Second == Timer.Second). { //Send Notification}} – SebGM2018 Jul 12 '18 at 05:22
  • You can do whatever you want afterward, loop or not. The code I provided populates the list you were trying to populate. – John Wu Jul 12 '18 at 06:13