eselk
eselk

Reputation: 6884

Winforms DateTimePicker set default text and/or value

I have a DateTimePicker with ShowCheckBox = true on my winforms app. If I do this in the forms constructor:

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

Then set 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.

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.

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. 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.

Upvotes: 1

Views: 6255

Answers (3)

Hassan Faghihi
Hassan Faghihi

Reputation: 2021

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:

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

Upvotes: 0

BlargleMonster
BlargleMonster

Reputation: 1622

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.

Upvotes: 0

eselk
eselk

Reputation: 6884

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

Upvotes: 1

Related Questions