简体   繁体   English

Winforms DateTimePicker设置默认文本和/或值

[英]Winforms DateTimePicker set default text and/or value

I have a DateTimePicker with ShowCheckBox = true on my winforms app. 我的Winforms应用程序上有一个ShowCheckBox = true的DateTimePicker。 If I do this in the forms constructor: 如果我在表单构造函数中这样做:

        DateFrom.Checked = true;
        DateFrom.Value = DateTime.Today.AddDays(-7);

Then set DateFrom.Checked = false; 然后设置DateFrom.Checked = false; in the FormShown event, it does what I would like, the text in the control defaults to 7 days before today, and the checkbox is unchecked. 在FormShown事件中,它可以执行我想要的操作,控件中的文本默认为今天前7天,并且未选中该复选框。

If I try to only set the Value, the text stays as today. 如果我尝试仅设置“值”,则文本保持为今天。 If I reset Checked = false anytime before the FormShown event, the text stays as today. 如果我在FormShown事件之前的任何时间重置Checked = false,则文本将保持今天。

Now I've moved this code to a user control, so to use the same "hack" will require even more hacking, so at this point I'm hoping someone has an easier method. 现在,我已将此代码移至用户控件,因此要使用相同的“ hack”将需要更多的hacking,因此在这一点上,我希望有人可以使用一种更简单的方法。 Maybe just another property I can set besides from Value that actually works? 也许除了我可以设置的其他属性之外,我还可以设置其他属性。 :) :)

I tried this also: 我也尝试过这个:

        DateFrom.Text = DateTime.Today.ToString(DateFrom.CustomFormat);

Instead of setting the value, or in addition to it, to no avail. 代替设置该值或除此以外,都没有用。

Typical, I tried for hours before posting my question, then right after I thought it might somehow be related to the creation of the window handle. 通常,在发布问题之前,我尝试了几个小时,然后才想到可能与创建窗口句柄有关。 So I came up with this solution. 所以我想出了这个解决方案。 Will still be happy to have something better, but this doesn't seem to bad if I have to stay with this: 仍然会很高兴有更好的东西,但是如果我不得不坚持下去,这似乎并不坏:

        DateFrom.Checked = true;
        DateFrom.Value = DateTime.Today.AddDays(-7);
        if (DateFrom.Handle == null)
            System.Threading.Thread.Sleep(0);
        DateFrom.Checked = false;

Checking Handle forces the window handle to be created, so then I'm able to uncheck the control without it defaulting to today's date for the text when the window handle is created later. Checking Handle强制创建窗口句柄,因此我可以取消选中该控件,而无需在以后创建窗口句柄时将其默认为今天的文本日期。 I just use Sleep(0) as a trick to make sure the compiler doesn't optimize the code and compile it out all together (not sure if that would even happen, but like to be sure, and condition shouldn't always be false so should never Sleep(0) anyway). 我只是使用Sleep(0)作为技巧,以确保编译器不会优化代码并将其全部编译在一起(不确定是否会发生这种情况,但请确保条件永远不为假。因此无论如何都不要睡眠(0)。

It might also be that the control is simply not redrawing properly, particularly if you have it as a usercontrol. 也可能是控件根本无法正确重绘,特别是如果您将其作为用户控件。 You might try calling Invalidate() on the control after setting the Value to see if that's the problem. 您可以在设置Value后尝试在控件上调用Invalidate(),以查看是否存在问题。

I changed your self answer and made it like this: 我更改了您的自我答案,并使其如下所示:

public ProcessFailureForm()
{
    InitializeComponent();

    //Blah blah blah

    dtFrom.HandleCreated += delegate //if you need sender or EventArgs use: delegate(object sender, EventArgs e)
    {
        dtFrom.Checked = true;
        dtFrom.Value = DateTime.Today.AddDays(-7);
        dtFrom.Checked = false;
    };
}

Update: 更新:

Actually first i think like you, but after doing this, i tried and find out that the Checked state won't affect the process... so it can be reduced to: 实际上首先我想像您一样,但是这样做之后,我尝试发现Checked状态不会影响该过程...因此可以将其简化为:

public ProcessFailureForm()
{
    InitializeComponent();

    //Blah blah blah

    dtFrom.HandleCreated += delegate //if you need sender or EventArgs use: delegate(object sender, EventArgs e)
    {
        dtFrom.Value = DateTime.Today.AddDays(-7);
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM