Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i was try to set my all datetimepiker max value is same when load main form using program,today date is max value for my datetimepiker.

What I have tried:

foreach (datetimepiker c in this.Controls)
{
if (c.GetType() == typeof(datetimepiker))
{
c.maxvalue =datetime.now.todate;
}


}
Posted
Comments
Andy Lanng 15-Feb-16 6:16am    
this.Controls will only refer to the first layer of controls (like children in xml). If the datetimepicker is within another control (like panel) then this won't find them.

1 solution

Try something like this:
C#
private static void InitializeDatePickers(Control parent, DateTime maxDate)
{
    if (parent == null) return;
    
    foreach (Control child in parent.Controls)
    {
        var datePicker = child as DateTimePicker;
        if (datePicker != null)
        {
            datePicker.MaxValue = maxDate;
        }
        else if (child.HasChildren)
        {
            InitializeDatePickers(child, maxDate);
        }
    }
}

...

InitializeDatePickers(this, DateTime.Today);
 
Share this answer
 
Comments
Arth01 15-Feb-16 11:52am    
thanks for solution..

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