本文介绍了如何在我的Windows窗体中设置所有datetimepiker最大值是相同的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置我的所有datetimepiker最大值在使用程序加载主窗体时相同,今天日期是我的datetimepiker的最大值。



我尝试了什么:



foreach(datetimepiker c in this.Controls)

{

if(c.GetType()== typeof(datetimepiker))

{

c.maxvalue = datetime.now.todate;

}





}

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;
}


}

推荐答案

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);


这篇关于如何在我的Windows窗体中设置所有datetimepiker最大值是相同的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:34