dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.42k stars 984 forks source link

DateTimePicker should obey Current(UI)Culture #3886

Closed hyperquantum closed 9 months ago

hyperquantum commented 4 years ago

Problem description:

It is not currently possible to change the language used by DateTimePicker. The control ignores CurrentCulture/CurrentUICulture of the current thread and uses the language as configured by the operating system.

This problem is an issue for WinForms applications that are localized in different languages and that need to be able to run with a language that is different from the language of the operating system.

This problem is (or was) also known as KB 889834.

Expected behavior:

DateTimePicker adapts to the CurrentCulture/CurrentUICulture of the current thread.

Minimal repro:

Create a new WinForms project.

Change Program.cs as follows (add the 3 lines about culture):

private static void Main()
{
    var c = CultureInfo.CreateSpecificCulture("fr-BE");
    CultureInfo.CurrentCulture = c;
    CultureInfo.CurrentUICulture = c;

    Application.SetHighDpiMode(HighDpiMode.SystemAware);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

In the form designer, add a Label and a DateTimePicker to the form.

Initialize the label text in the form's constructor:

public Form1()
{
    InitializeComponent();

    label1.Text = DateTime.Now.ToLongDateString();
}

For me, running this program results in:

afbeelding

The DateTimePicker should use the same language as the Label but it doesn't.

JeremyKuhne commented 4 years ago

This is a limitation in the underlying control. It might be possible to come up with a solution using the callback fields in the format string. If we could come up with a solution using that we'd probably want to add a new API to allow specifying the desired localization behavior. Not a trivial amount of work.

Some other links:

https://stackoverflow.com/questions/8141437/change-datetimepicker-calendar-runtime https://stackoverflow.com/questions/40439469/date-time-picker-culture-info-in-window-forms?rq=1 https://web.archive.org/web/20110717083209/http://blogs.msdn.com/b/michkap/archive/2005/03/28/402839.aspx https://web.archive.org/web/20110718101517/http://support.microsoft.com/kb/889834

hyperquantum commented 9 months ago

@merriemcgaw Was this fixed in the underlying control? Or what was the fix exactly?

merriemcgaw commented 9 months ago

I should have marked this as not planned, my mistake. No, we can't make changes to the underlying Common Control unfortunately, so @JeremyKuhne provided ideas for options. Windows does not make changes to the Common controls due to high regression risk and compatibility requirements. As a managed wrapper around Win32 and Common Controls we are limited as to what we can change.