0
votes

I am creating a WinForms app that uses the DateTimePicker control, my dev machine is a Windows 7 machine, the control works fine, no problems or issues. But when I deploy this WinForm to an XP machine, and try to use the DateTimePicker control it opens up the calendar, but upon selection of a date the calendar does not go away as it does on my dev machine.

Please advise on how to rectify this.

Thanks in advance.

This is all I am doing when selecting a value from the DateTimePicker control:

private void dateTimePickerStartDate_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerStartDate.Format = DateTimePickerFormat.Long;
        }

        private void dateTimePickerEndDate_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerEndDate.Format = DateTimePickerFormat.Long;
        }
1
Why set the format on value change? You only need to do that once, right? - Jodrell
I am doing this because on form load i am doing this: dateTimePickerEndDate.Format = DateTimePickerFormat.Custom; dateTimePickerEndDate.CustomFormat = " "; dateTimePickerStartDate.Format = DateTimePickerFormat.Custom; dateTimePickerStartDate.CustomFormat = " "; If i dont do that the fields will be populated with todays date and i do not want that, i want a blank field. - johnnie
Are both machines up to date? At least all the Dotnet components. - Mesh
I cant re-produce this. A DatePicker control placed on a blank form, appears to work correctly in both OSs. Can you reproduce with a simple test app? - Mesh
@johnnie "If i dont do that the fields will be populated with todays date and i do not want that, i want a blank field." -- The hack you've gone through suggests that a DateTimePicker simply doesn't support this; you can include the checkbox in the DTP to make it work as designed. That said, a smaller and more reliable hack would be to create a control derived from DateTimePicker where you override its paint handler. - user743382

1 Answers

0
votes

I finally solved this! For some reason XP does not seam to like this line of code:

dateTimePickerStartDate.Format = DateTimePickerFormat.Long;
dateTimePickerEndDate.Format = DateTimePickerFormat.Long;

So if you set your datetimepicker's format to long XP does not close the calendar on selection of a date.

To solve this set you datetimepicker's format to this (if you want to use the long format) :

dateTimePickerStartDate.Format = DateTimePickerFormat.Custom;    
dateTimePickerStartDate.CustomFormat = "dd MMMM yyyy";

dateTimePickerEndDate.Format = DateTimePickerFormat.Custom;    
dateTimePickerEndDate.CustomFormat = "dd MMMM yyyy";